-
Notifications
You must be signed in to change notification settings - Fork 345
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
Restores the expected behavior of certain fixtures, while fixing a bug related to fixtures now tearing down properly #807
base: main
Are you sure you want to change the base?
Conversation
I am not really sure if that should be changed. While it might not be a typical use case it would be good for them to be kept separate, if that is possible (i.e. that it would raise
That makes sense in general already. |
been executed, suggesting that ``_django_db_blocker`` should request | ||
``django_db_setup``, especially since it is possible for ``_django_db_blocker`` to | ||
be needed when ``django_db_setup`` wouldn't normally have been run (e.g. if a test | ||
isn't marked with ``pytest.mark.django_db``). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
especially since it is possible for
_django_db_blocker
to
be needed whendjango_db_setup
wouldn't normally have been run (e.g. if a test
isn't marked withpytest.mark.django_db
).
This now implicitly "marks" the test then, doesn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it's almost the same. The only difference being that the DB is still blocked if you don't use the mark.
Doesn't this show an issue in pytest then also, if the following does not work in the same way anymore? diff --git i/pytest_django/fixtures.py w/pytest_django/fixtures.py
index b2cc825..f1a3abd 100644
--- i/pytest_django/fixtures.py
+++ w/pytest_django/fixtures.py
@@ -108,7 +108,9 @@ def django_db_setup(
**setup_databases_args
)
- def teardown_database():
+ yield
+
+ if not django_db_keepdb:
with django_db_blocker.unblock():
try:
teardown_databases(db_cfg, verbosity=request.config.option.verbose)
@@ -119,9 +121,6 @@ def teardown_database():
)
)
- if not django_db_keepdb:
- request.addfinalizer(teardown_database)
-
def _django_db_fixture_helper(
request, django_db_blocker, transactional=False, reset_sequences=False |
@blueyed Thanks for checking this out!
It was my understanding that That said,
So I went back and undid my changes to these fixtures to get the exact problems they were having, but it turns out they work fine the old way (I even rebuilt my venv to make sure), so it must have been something about my old venv or the state the code was in at the moment I hit that issue. So I guess we can just chalk these changes to I can still see going through I could actually fix it up so that the |
It appears "just" to be in the wrong order here?! (tox suffix and xdist suffix) I.e. the fixtures might be used not in the order being used as arguments. |
Ah, great catch! I'll add in a fix for it to make them deterministic |
I think it's upset about the coverage because I added a yield statement to get it to not complain about the other yield statement haha. But I'm not really sure how to read these coverage reports. It looks like it's saying I increased coverage in several areas, but if that's the case, it shouldn't be failing. |
No idea about "coverage changes", but your patch is not covered: + if test_name.endswith("_{}".format(suffix)):
+ continue With regard to that I still think this is only a workaround for a bug/issue in pytest then: it should never create duplicate suffixes in the first place. |
Ah, of course! This ran with pytest 5.3.4, which doesn't have my change in pytest! I'll pin 5.3.3 for this branch temporarily, but while I'm confident I'll hit that line, I'm not 100% positive. The issue we saw in pytest-dev/pytest#6492 may have been related to the number of tests being run or the fact that they had set the database name to be something specific. Or some combination. I'll have to mess around with it just to be sure. What I think was happening in general, and why I believe this to be a pytest-django issue, is that every single test (in The problem, is that because every worker maintains the same runtime, but still runs all fixtures for every individual test group passed to them (which depends on how load was distributed by pytest-xdist), The change you mentioned not being covered wasn't being hit because the change from pytest-dev/pytest#6438 wasn't applied in this run, so the fixture was never executed for that worker more than once, which means there's no way for the suffix to have already been added to the test database name for that runtime. |
c78e6f2
to
b93f4dc
Compare
Edit: all is good now |
I'm narrowing down the problem, although it's a slow process. Luckily, I've been able to finally reproduce it on my end so I can test a lot faster (I had another error showing that turned out to be a red herring and fixed itself once I re-cloned/built everything). The problem seems to be a result of something connecting to the database right before it tries to recreate the DB. So if you previously ran it with It's worth mentioning that I only see this when postgres and xdist are used. |
@blueyed Looks like pytest-dev/pytest#6551 did the trick. After some minor tweaking of this branch, everything is passing, except for certain things, which looks like a change in terms of what's supported. Maybe those tox environments should be retired? |
2157f7d
to
5e53f0a
Compare
With
pytest==5.3.3
, fixtures now tear down when they should. This highlighted an issue in thedjango_db_blocker
fixture, in that it did not actually trigger the database to be set up. To fix this, thedjango_db_blocker
fixture was renamed to_django_db_blocker
, so that another fixture could be made to take the place ofdjango_db_blocker
by taking the name and requesting both the_django_db_blocker
anddjango_db_setup
fixtures to make sure the DB is set up when using the blocker.Additionally, some fixtures were relying on an older method of handling cleanup/finalization, and this was causing the blocker fix to have issues. Those fixtures have been updated to use the
yield
fixture strategy.