-
Notifications
You must be signed in to change notification settings - Fork 349
Support database access for "session" level fixtures. #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Which Django version are you using? There seems to be a workaround in a (possibly duplicate issue): #105 (comment) You could also use a signal via
See also #220 (comment) |
Aha! I had actually tried using It looks like this will allow me to use the DB in a session-level fixture, and will solve my problem. Is there any interest in making that accessible in a more user friendly way? Is this workaround unsupported and prone to break in the future? |
This is something which is worked on and that will be supported by a public API. I tagged this issue with "db-configuration" which is issues related to having more options when it comes to configuring the database. I will update this issue with more information once those changes gets into master! |
Here's a fixture to enable session level fixtures for db that I use. It's similar to the documented way but also does rollback at the end of the test session. from django.db import transaction
@pytest.fixture(scope="session")
def initial_test_data(django_db_setup, django_db_blocker):
with django_db_blocker.unblock():
# Wrap in try + atomic block to do non crashing rollback
try:
with transaction.atomic():
yield
raise Exception
except Exception:
pass
@pytest.fixture(scope="session", autouse=True)
def some_initial_data(initial_test_data):
# Data added here will stay in db for the whole test session |
Our test suite uses a custom TestRunner which has a
run_suite
method like so:The fixtures it loads are indeed so common it makes sense just to load them for every test case. For example we host multiple
Site
s off of our app and one of the fixtures loads a subset of thoseSite
objects. Foreign keys toSite
exist in almost all our apps so it makes sense to just load a few before the whole suite.Although in theory I should be able to rename the fixtures to
initial_data.json
and have them load, that is for whatever strange reason not working. That is when I found there is no way to duct-tape over it in py.test, because the only timedb
access is allowed is at the function level.Are there any plans to include a session-level fixture for db access?
The text was updated successfully, but these errors were encountered: