I am learning PyTest in django and I encountered problem that I cannot solve.
I am getting this error:
____________________________________________________________________________________________________________ ERROR collecting prices_tool/tests/test_views.py ____________________________________________________________________________________________________________
prices_tool/tests/test_views.py:9: in <module>
from prices_tool.views import results
prices_tool/views.py:14: in <module>
from prices_tool.forms import SearchForm, FreeSearchForm
prices_tool/forms.py:7: in <module>
class FreeSearchForm(forms.Form):
prices_tool/forms.py:11: in FreeSearchForm
for make in makes:
../prices_tool-env/lib/python3.8/site-packages/django/db/models/query.py:287: in __iter__
self._fetch_all()
../prices_tool-env/lib/python3.8/site-packages/django/db/models/query.py:1308: in _fetch_all
self._result_cache = list(self._iterable_class(self))
../prices_tool-env/lib/python3.8/site-packages/django/db/models/query.py:111: in __iter__
for row in compiler.results_iter(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size):
../prices_tool-env/lib/python3.8/site-packages/django/db/models/sql/compiler.py:1108: in results_iter
results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
../prices_tool-env/lib/python3.8/site-packages/django/db/models/sql/compiler.py:1154: in execute_sql
cursor = self.connection.cursor()
../prices_tool-env/lib/python3.8/site-packages/django/utils/asyncio.py:26: in inner
return func(*args, **kwargs)
../prices_tool-env/lib/python3.8/site-packages/django/db/backends/base/base.py:259: in cursor
return self._cursor()
../prices_tool-env/lib/python3.8/site-packages/django/db/backends/base/base.py:235: in _cursor
self.ensure_connection()
E RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.
========================================================================================================================== short test summary info ===========================================================================================================================
ERROR prices_tool/tests/test_views.py - RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================================================== 1 error in 0.47s ==============================================================================================================================
It seems that I cannot import any method from my views.py. Of course I tried adding @pytest.mark.django_db
it did not work. I googled around and found some posts here and I added in my conftest.py
this:
@pytest.fixture(autouse=True)
def enable_db_access_for_all_tests(db):
pass
And when that did not work I even tried with this:
@pytest.fixture(scope='session')
def django_db_setup():
settings.DATABASES['default'] = {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'path/to/dbfile.sqlite3',
}
But none of this fix or changed error message. How can I fix this?
Thanks and Cheers!
I have found solution to my problem. I am not sure if it is a smart thing to do, but now it's obvious solution.
Just move import of problematic view BELOW @pytest.mark.django_db
.
For example:
@pytest.mark.django_db
class TestViews:
def test_view(self, client):
path = reverse('results')
request = RequestFactory().get(path)
request.user = mixer.blend(User)
context = {
'info': info
}
from prices_tool.views import results
response = results(request, context)
assert response.status_code == 200
In this way you will avoid errors that are occurring because some view is importing db stuff.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.