pytest-dev / 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.

Open
#1,118 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Platform/tests/graphql/base.py, where GraphqlBaseTestCase performs a ContentType database lookup during class definition, then inspect Platform/tests/graphql/mutation/test_announcement.py and the related base classes. Run that test through pytest and compare it with python manage.py test. Done means the test suite can be collected and run with pytest without the database-access error.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
backend, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.