-
Notifications
You must be signed in to change notification settings - Fork 344
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
pytest 5.4 skips Django's teardown (causing e.g. "connection already closed" errors) #824
Comments
Can you confirm this happens also on pytest 5.4.1? If yes, then a minimal reproduction would be great. |
Yes, 5.4.1 too. Will try to create minimal case. |
Created minimal django project: https://github.com/Fak3/testproject-824-git |
Thanks for the reproduction! It looked to me like a problem in pytest's unittest support, so I looked at changes made to that in pytest in 5.4.x. Reverting pytest-dev/pytest#5996 "unittest: do not use TestCase.debug() with cc @blueyed @nicoddemus who worked on that PR and probably have a better idea than me. |
@Fak3 (It's the next round of the long-standing issue from 2016, and I still think it should have been reverted first at least instead (ref pytest-dev/pytest#6014), until it gets done properly, e.g. via something like blueyed/pytest#107)) pytest-django could intercept Django's |
Yes, that PR fixes this issue |
Is pytest-dev/pytest#6947 related? |
* Work around unittest issue with pytest 5.4.{0,1} Ref: #824 * tox/travis: pytest53
Yes, caused by it. Fixed in pytest-django 3.9.0 now. |
I think that the issue persist in pytest-django 3.9.0 #835 |
Same problem here! |
@PyB1l or @hugos94, any chance of testing this with pytest's latest |
For my tests i managed to isolate the issue in Django==3.0.2. For Django===3.0.5 and all the lastet version of pytest, pytest-django, i have no issues. @hugos94 could you check your versions and let us know? |
I am using the following versions:
Apparently the problem only happens after making requests in views of the application. In tests that i use graphql, the error doesn't occur. |
|
Hello, The problem seems to have reappeared:
Only the 2 tests below have an issue, which is weird considering the high number of tests still working fine:
|
I noticed this problem as well. Do you prefer to re-open this bug or create a new issue? FWIW, here is the list of my other dependencies.
My Django version is different from @nathanprat (3.0 vs 3.2) so it doesn't seem relevant. I see 3 common points with the above
UPDATE:after further investigation, I've created a minimal project with out issue, you can see the failure in the action executions. I've tracked down the problem to a custom fixture in our project. Doesn't sounds like a regression of this bug after all. |
@browniebroke if you would, could you explain why that particular fixture is causing the problem? Might help others with finding similar issues. |
I don't know why (yet) unfortunately, I've just managed to isolate this as the root cause, but I didn't persist further with the upgrade due to lack of time. In case it helps, this fixture is supposed to persist model data across test cases within a class, a bit like |
Thanks for the investigation. The bug was indeed caused by a class-level db fixture. I don't have time to investigate deeper right now, but the code is below if anyone is interested: @pytest.fixture(name='class_db', scope='class')
def fixture_class_db(request: 'Any',
django_db_setup: 'Any', # pylint: disable=unused-argument
django_db_blocker: 'Any') -> None:
"""
To avoid the error with 'fixture_db_setup(db)':
"ScopeMismatch: You tried to access the 'function' scoped fixture 'db'
with a 'module' scoped request object, involved factories"
We reimplement a 'db'-like fixture class-scoped.
"""
# Copy-pasted from pytest_django.fixtures
if "django_db_reset_sequences" in request.fixturenames:
request.getfixturevalue("django_db_reset_sequences")
if ("transactional_db" in request.fixturenames
or "live_server" in request.fixturenames):
request.getfixturevalue("transactional_db")
else:
_django_db_fixture_helper(
request,
django_db_blocker,
transactional=False) |
@browniebroke If you change your code from |
Thanks for the suggestion @bluetech. I tried your suggestion (I think) but no luck yet... Was it the solution you had in mind? |
Yep. Although it was actually also correct before, my eye just glanced over the (I understand this doesn't actually fix your problem. What I think you are trying to achieve -- safe sharing of DB data beyond a single test -- is currently a missing feature in pytest-django) |
Just for note. In some cases it happens when you override I had made this mistake. After inserting |
Just a quick update on my earlier issue is now resolved in pytest-django 4.5.0 and above 😄 Not sure what nor who solved it, but I just wanted to say thanks to the maintainer! |
as an additional note: for me it happened when calling |
* Work around unittest issue with pytest 5.4.{0,1} Ref: pytest-dev/pytest-django#824 * tox/travis: pytest53
pytest-django==3.5.1
django versions tested: 2.2.11 and 3.0.4
When one test fails, all tests which use database launched after this test will fail with traceback:
Downgrading to pytest==5.3.5 fixes the issue.
Not yet sure if this is pytest issue or pytest-django.
The text was updated successfully, but these errors were encountered: