I keep getting this error RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it. while running my tests using this command: py.test -n auto --disable-socket -W error::RuntimeWarning --cov=src --cov-report=html tests/
Based on your recommendation, my conftest.py has the following content:
import pytest
from core.models import GenericSettings
from tests.core.test_models import GenericSettingsFactory
@pytest.fixture
def generic_settings() -> GenericSettings:
# return the GenericSettingsFactory (factoryboy)
return GenericSettingsFactory()
@pytest.fixture(autouse=True)
def enable_db_access_for_all_tests(db):
pass
...
src/ass/tasks/com.py:12: in <module>
from com.tasks import com_send_email_task
src/com/tasks/__init__.py:2: in <module>
from .sending import * # noqa
src/com/tasks/sending.py:23: in <module>
generic_settings = GenericSettings.load()
src/admin/models.py:433: in load
obj, _ = cls.objects.get_or_create(id=1)
virtualenv/lib/python3.9/site-packages/django/db/models/manager.py:85: in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
virtualenv/lib/python3.9/site-packages/django/db/models/query.py:581: in get_or_create
return self.get(**kwargs), False
virtualenv/lib/python3.9/site-packages/django/db/models/query.py:431: in get
num = len(clone)
virtualenv/lib/python3.9/site-packages/django/db/models/query.py:262: in __len__
self._fetch_all()
virtualenv/lib/python3.9/site-packages/django/db/models/query.py:1324: in _fetch_all
self._result_cache = list(self._iterable_class(self))
virtualenv/lib/python3.9/site-packages/django/db/models/query.py:51: in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
virtualenv/lib/python3.9/site-packages/django/db/models/sql/compiler.py:1173: in execute_sql
cursor = self.connection.cursor()
virtualenv/lib/python3.9/site-packages/django/utils/asyncio.py:33: in inner
return func(*args, **kwargs)
virtualenv/lib/python3.9/site-packages/django/db/backends/base/base.py:259: in cursor
return self._cursor()
virtualenv/lib/python3.9/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.
The model GenericSettings which was highlighted by the traceback is a singleton with the following content:
I keep getting this error
RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.
while running my tests using this command:py.test -n auto --disable-socket -W error::RuntimeWarning --cov=src --cov-report=html tests/
Based on your recommendation, my
conftest.py
has the following content:pytest.ini
has the following as content:Part of the traceback is:
The model
GenericSettings
which was highlighted by the traceback is asingleton
with the following content:How can I get rid of these errors?
Preliminaries: