Accessing test database in (Selenium based) functional tests for a Pyramid + sqlalchemy application
<p>I have managed to create functional tests using Splinter (Selenium) and <code>StoppableWSGIServer</code>. Here's my code:</p>

<pre><code>...

engine = engine_from_config(settings, prefix='sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.create_all(engine)

...

class FunctionalTest(...):
    ...

    def setUp(self):
        ...
        self.server = http.StopableWSGIServer.create(app)
        self.server.wait()
        self.browser = splinter.Browser("chrome")

    def tearDown(self):
        ...
        self.browser.quit()
        self.server.shutdown()
</code></pre>

<p>Where <code>app</code> is created using <code>Configurator.make_wsgi_app</code>.</p>

<p>When running test cases using my <code>FunctionalTest</code>, the browsers shows up, the server starts the database works, the tables are created. However, the test server can't access rows created in the test cases, even though both are initialized using the same settings file.</p>

<p>I have tried mocking <code>DBSession</code> and <code>engine</code> in my <code>models.py</code> and <code>views.py</code> and this way both <code>DBSession</code> and <code>Base.metadata.bind</code> have the very same <code>id()</code> in both my test cases and my view functions. (So, to my understanding, are the very same objects) Yet, querying for rows created in the test cases returns <code>[]</code> in the view, and the tests fail. I have called <code>DBSession.flush()</code> after creating the rows and <code>DBSession</code> is defined in <code>models.py</code> like this:</p>

<pre><code>DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
</code></pre>

<p>How do I make the test server see rows created in the test case code?</p>

<p>This seems to do the trick:</p>

<pre><code>import trasaction
transaction.commit()
</code></pre>

<p>However I'm not absolutely certain about my implementation, so I'm still awaiting answers. Also, this way I have to <code>DBSession.drop_all</code> before each test case.</p>

python selenium sqlalchemy pyramid functional-testing