'Django_db mark for django parametrized tests
I have been learning django for the past few weeks and I tried using the parametrizing fixtures and test functions and from what I understood I can simply run multiple tests at once. With the parametrized test I am trying to test functions, which are found in all models. I read the documentation, but sadly, as soon as I tried it I got the following error message Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.. I did read about the error and possible fixes and what I found was to create an autouse fixture and put it in conftest.py:
import pytest
@pytest.fixture(autouse=True)
def enable_db_access_for_all_tests(db):
pass
Sadly, this change made 0 difference and I received the same exact error after running the test. I did also try to use the django_db mark to grant the test access to the database, but that also did not seem to work.
Solution 1:[1]
It took me a while to realize it, but the above WAS "working". If you look closely at the error, it changed. Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it. is still there, incorrectly, but also, for me anyway, it was running migrations, which I didn't want it to do, and it was crashing on some old data migration. Adding --nomigrations to the command resolved the issue for me.
Solution 2:[2]
Create a decorator @pytest.mark.django_db above the function that is concerned or you can also use @pytest.mark.django_db(transaction=True) , this helps pytest to tell django that the concerned function will require database access
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Scott Stafford |
| Solution 2 | optimus |
