From 7310e756fd0ae29d624b0dd678b1a6a8bba78705 Mon Sep 17 00:00:00 2001 From: MaximIgitov Date: Sat, 31 Jan 2026 04:17:28 +0000 Subject: [PATCH] Clarify database access error message --- pytest_django/plugin.py | 7 ++++--- tests/test_database.py | 9 +++------ tests/test_fixtures.py | 4 ++-- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index fbf46771..9454995f 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -841,9 +841,10 @@ def _save_active_wrapper(self) -> None: def _blocking_wrapper(*args: Any, **kwargs: Any) -> NoReturn: # noqa: ARG002 __tracebackhide__ = True raise RuntimeError( - "Database access not allowed, " - 'use the "django_db" mark, or the ' - '"db" or "transactional_db" fixtures to enable it.' + "Database access not allowed. " + "Use the @pytest.mark.django_db mark or the db/transactional_db " + "fixtures to enable it. " + "See https://pytest-django.readthedocs.io/en/latest/database.html#database-access" ) def unblock(self) -> AbstractContextManager[None]: diff --git a/tests/test_database.py b/tests/test_database.py index cb5b54a0..e2f2ab0a 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -430,8 +430,7 @@ def test_db_access_3(self): "*test_db_access_2 FAILED*", "*test_db_access_3 FAILED*", "*ERROR at setup of TestCase_setupClass.test_db_access_1*", - '*RuntimeError: Database access not allowed, use the "django_db" mark, ' - 'or the "db" or "transactional_db" fixtures to enable it.', + '*RuntimeError: Database access not allowed. Use the @pytest.mark.django_db mark or the db/transactional_db fixtures to enable it. See https://pytest-django.readthedocs.io/en/latest/database.html#database-access' ] ) @@ -472,8 +471,7 @@ def test_db_access_in_conftest(self, django_pytester: DjangoPytester) -> None: result = django_pytester.runpytest_subprocess("-v") result.stderr.fnmatch_lines( [ - '*RuntimeError: Database access not allowed, use the "django_db" mark, ' - 'or the "db" or "transactional_db" fixtures to enable it.*' + '*RuntimeError: Database access not allowed. Use the @pytest.mark.django_db mark or the db/transactional_db fixtures to enable it. See https://pytest-django.readthedocs.io/en/latest/database.html#database-access*' ] ) @@ -488,7 +486,6 @@ def test_db_access_in_test_module(self, django_pytester: DjangoPytester) -> None result = django_pytester.runpytest_subprocess("-v") result.stdout.fnmatch_lines( [ - '*RuntimeError: Database access not allowed, use the "django_db" mark, ' - 'or the "db" or "transactional_db" fixtures to enable it.' + '*RuntimeError: Database access not allowed. Use the @pytest.mark.django_db mark or the db/transactional_db fixtures to enable it. See https://pytest-django.readthedocs.io/en/latest/database.html#database-access' ] ) diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index 16a548d4..221fba12 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -766,7 +766,7 @@ class Test_django_db_blocker: def test_block_manually(self, django_db_blocker: DjangoDbBlocker) -> None: try: django_db_blocker.block() - with pytest.raises(RuntimeError, match="^Database access not allowed,"): + with pytest.raises(RuntimeError, match="^Database access not allowed\. Use the @pytest\.mark\.django_db mark or the db/transactional_db fixtures to enable it\. See https://pytest-django\.readthedocs\.io/en/latest/database\.html#database-access"): Item.objects.exists() finally: django_db_blocker.restore() @@ -774,7 +774,7 @@ def test_block_manually(self, django_db_blocker: DjangoDbBlocker) -> None: @pytest.mark.django_db def test_block_with_block(self, django_db_blocker: DjangoDbBlocker) -> None: with django_db_blocker.block(): - with pytest.raises(RuntimeError, match="^Database access not allowed,"): + with pytest.raises(RuntimeError, match="^Database access not allowed\. Use the @pytest\.mark\.django_db mark or the db/transactional_db fixtures to enable it\. See https://pytest-django\.readthedocs\.io/en/latest/database\.html#database-access"): Item.objects.exists() def test_unblock_manually(self, django_db_blocker: DjangoDbBlocker) -> None: