On Thu, Jun 24, 2021 at 10:34 AM Zsolt Ero <[email protected]> wrote:
>
> Hi,
>
> I have my webtest based testing set up like the following.
>
> @pytest.fixture(scope="session")
> def app():
> testing_config = {
> 'sqlalchemy.url': 'postgresql+psycopg2://x@/x',
> 'redis.sessions.secret': 'x', # random string
> 'redis.sessions.db': 1,
> 'tm.annotate_user': 'no',
> }
>
> return main({"testing": True}, **testing_config)
>
>
> @pytest.fixture
> def testapp(app):
> return TestApp(app)
>
> main() is a config.make_wsgi_app().
>
> My questions is, how can I have direct database connection - normally a
> request.dbsession in the app - in the testing fixtures? I'd like to run some
> DB queries in the setup/teardown process.
>
> I can access testapp.app or "app" from the fixtures, that works. What I don't
> know is how can I get a request object from there.
>
> In command line scripts I use
>
> request_dummy = Request.blank('/', base_url=base_url)
> env = bootstrap(config_uri, request=request_dummy)
> request = env['request']
>
> Do I need to do something similar here?
I mock as much as feasible, so I have fake DB Session and Query
classes with a list of fake results to return in order. When I want a
real database connection, I have PyTest fixtures that read the INI
path from a command-line option (pytest_addoption(),
pyramid.paster.getappsettings()), and resolve it to a SQLAlchemy
Session that always rolls back (PyTest request.addfinalizer()). That's
a PyTest's request, not a Pyramid request.
The only time I have to do more is when testing views that call
Pyramid 'request.route_path()'. For those I use something like this:
"""
settings = {...}
with pyramid.testing.testConfig(settings=settings) as config:
config.add_route(...)
request = pyramid.testing.DummyRequest(registry=config.registry)
request.sa_session_main = ... # Emulate property in my real
request subclass. (SQLAlchemy session.)
a = the_view(request)
assert a["my_url"] = expected_url
"""
If necessary I'll use a real Pyramid Request.blank() or my real
Request subclass, but this is sufficient for a lot of cases.
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3DuqYYEvK9uyH7hK3UGsUFOCqZOyr3ivp1KjFmsEsJv%3Dchg%40mail.gmail.com.