-
Notifications
You must be signed in to change notification settings - Fork 343
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
Database transactions with asyncio #580
Comments
Did you ever found a way around this? It seems like adding the asyncio library is affecting non-async tests as well. |
Possibly related: https://code.djangoproject.com/ticket/32409 Does this still happen with asgiref>=3.3.0? |
Yes, I'm seeing this with |
There's a corresponding issue in pytest-asyncio that's been lying around for some time. I'm unsure if I can close it, so this is why I'm reaching out. My current understanding is that pytest-django is a "pytest wrapper" around Django's unittest test cases. Is this correct? However, pytest-asyncio does not work together with unittest as of v0.18.3 (see the README, and pytest-dev/pytest-asyncio#77). Is there anything that needs to be done on the pytest-asyncio side to solve this? |
I think I encountered this issue today. Marking the tests with |
@Jenselme It works for me as well. The problem is that marking it with |
Indeed, I noticed that also. I might be reverting to using the sync HTTP client and wrap async operations in I had a look at I’d like for a cleaner way to do this in pytest, but until then, making all test sync seem like a good alternative. |
Looks like a problem is because django async orm don't support atomic blocks. Pytest-django tests isolation logic bases on wrapping each test (and test fixtures) in @transaction.atomic. But async client opens new event loop inside, with self management logic. |
Using regular test case does not work as expected, as the database is not being cleaned properly: pytest-dev/pytest-django#580
Wrapping your async test in Here's a note I have on this issue:
Some other things I have seen made me convinced that
^ if you see stuff like this in your logs this is also very likely to be this same issue. |
The newly released Channels 2.0 Django package "relies on the
asyncio
library and native Python async support". They recommend to usepy.test
tests with theasyncio
plugin.Now, you can use the
db
anddjango_db
fixtures withasyncio
(when you use Channels'database_sync_to_async
wrapper):However, transactions don't seem to work. As a result all changes to the test database will be kept.
"Note that you can’t mix this with
unittest.TestCase
subclasses" (whichpytest-django
does internally – but I don't know if this is of importance concerning this issue, because anotherasyncio
issue seems to be related).Is there a way to use transactions in an async setting with the current packages? Or are there plans to support transactions in async tests in the near future? Or should this be done in another package?
The text was updated successfully, but these errors were encountered: