pytest-dev/pytest-django

Switching from Django UnitTest to pytest-django results in : Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.

Some7hing0riginal opened this issue · 1 comments

I'm trying to keep my tests the same but wrap them in pytest which would let me use some nice to haves provided by pytest. When I run via pytest, I get the following

Platform/tests/graphql/mutation/test_announcement.py:11: in <module>
    from Platform.tests.graphql.mutation.base import MutationBaseTestCase
Platform/tests/graphql/mutation/base.py:1: in <module>
    from ..base import GraphqlBaseTestCase
Platform/tests/graphql/base.py:17: in <module>
    class GraphqlBaseTestCase(GraphqlMixin, PlatformBaseTestCase):
Platform/tests/graphql/base.py:18: in GraphqlBaseTestCase
    project_content_type = ContentType.objects.get_for_model(ProxyProject)
venv/lib/python3.9/site-packages/django/contrib/contenttypes/models.py:51: in get_for_model
    ct = self.get(app_label=opts.app_label, model=opts.model_name)
venv/lib/python3.9/site-packages/django/db/models/manager.py:85: in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
venv/lib/python3.9/site-packages/cacheops/query.py:351: in get
    return qs._no_monkey.get(qs, *args, **kwargs)
venv/lib/python3.9/site-packages/django/db/models/query.py:492: in get
    num = len(clone)
venv/lib/python3.9/site-packages/django/db/models/query.py:302: in __len__
    self._fetch_all()
venv/lib/python3.9/site-packages/cacheops/query.py:271: in _fetch_all
    return self._no_monkey._fetch_all(self)
venv/lib/python3.9/site-packages/django/db/models/query.py:1507: in _fetch_all
    self._result_cache = list(self._iterable_class(self))
venv/lib/python3.9/site-packages/django/db/models/query.py:57: in __iter__
    results = compiler.execute_sql(
venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py:1359: in execute_sql
    cursor = self.connection.cursor()
venv/lib/python3.9/site-packages/django/utils/asyncio.py:26: in inner
    return func(*args, **kwargs)
venv/lib/python3.9/site-packages/django/db/backends/base/base.py:284: in cursor
    return self._cursor()
venv/lib/python3.9/site-packages/django/db/backends/base/base.py:260: 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.

This happens on pytest version 8.11 with pytest-django version 4.8.0

Additionally, I attempted to mark every test class involved (even each parent class for the test shown in the stack trace) like so

@pytest.mark.django_db
class GraphqlBaseTestCase(GraphqlMixin, PlatformBaseTestCase):

Running via python manage.py test works fine.

Any idea how I can get this working?