Skip to content

DB blocker: use RuntimeError instead of pytest.fail #781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ def _save_active_wrapper(self):
def _blocking_wrapper(*args, **kwargs):
__tracebackhide__ = True
__tracebackhide__ # Silence pyflakes
pytest.fail(
raise RuntimeError(
"Database access not allowed, "
'use the "django_db" mark, or the '
'"db" or "transactional_db" fixtures to enable it.'
Expand Down
14 changes: 7 additions & 7 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ def db_supports_reset_sequences():


def test_noaccess():
with pytest.raises(pytest.fail.Exception):
with pytest.raises(RuntimeError):
Item.objects.create(name="spam")
with pytest.raises(pytest.fail.Exception):
with pytest.raises(RuntimeError):
Item.objects.count()


@pytest.fixture
def noaccess():
with pytest.raises(pytest.fail.Exception):
with pytest.raises(RuntimeError):
Item.objects.create(name="spam")
with pytest.raises(pytest.fail.Exception):
with pytest.raises(RuntimeError):
Item.objects.count()


Expand Down Expand Up @@ -254,7 +254,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*",
'*Failed: Database access not allowed, use the "django_db" mark, '
'*RuntimeError: Database access not allowed, use the "django_db" mark, '
'or the "db" or "transactional_db" fixtures to enable it.',
]
)
Expand All @@ -274,7 +274,7 @@ def test_db_access_in_conftest(self, django_testdir):
result = django_testdir.runpytest_subprocess("-v")
result.stderr.fnmatch_lines(
[
'*Failed: Database access not allowed, use the "django_db" mark, '
'*RuntimeError: Database access not allowed, use the "django_db" mark, '
'or the "db" or "transactional_db" fixtures to enable it.*'
]
)
Expand All @@ -290,7 +290,7 @@ def test_db_access_in_test_module(self, django_testdir):
result = django_testdir.runpytest_subprocess("-v")
result.stdout.fnmatch_lines(
[
'*Failed: Database access not allowed, use the "django_db" mark, '
'*RuntimeError: Database access not allowed, use the "django_db" mark, '
'or the "db" or "transactional_db" fixtures to enable it.'
]
)
29 changes: 29 additions & 0 deletions tests/test_db_access_in_repr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@


def test_db_access_with_repr_in_report(django_testdir):
django_testdir.create_test_module(
"""
import pytest

from .app.models import Item

def test_via_db_blocker(django_db_setup, django_db_blocker):
with django_db_blocker.unblock():
Item.objects.get(name='This one is not there')

def test_via_db_fixture(db):
Item.objects.get(name='This one is not there')
"""
)

result = django_testdir.runpytest_subprocess("--tb=auto")
result.stdout.fnmatch_lines([
"tpkg/test_the_test.py FF",
"E *DoesNotExist: Item matching query does not exist.",
"tpkg/test_the_test.py:8: ",
'self = *RuntimeError*Database access not allowed*',
"E *DoesNotExist: Item matching query does not exist.",
"* 2 failed in *",
])
assert "INTERNALERROR" not in str(result.stdout) + str(result.stderr)
assert result.ret == 1
2 changes: 1 addition & 1 deletion tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def test_database_name():


def test_database_noaccess():
with pytest.raises(pytest.fail.Exception):
with pytest.raises(RuntimeError):
Item.objects.count()


Expand Down
4 changes: 2 additions & 2 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,15 +607,15 @@ class Test_django_db_blocker:
def test_block_manually(self, django_db_blocker):
try:
django_db_blocker.block()
with pytest.raises(pytest.fail.Exception):
with pytest.raises(RuntimeError):
Item.objects.exists()
finally:
django_db_blocker.restore()

@pytest.mark.django_db
def test_block_with_block(self, django_db_blocker):
with django_db_blocker.block():
with pytest.raises(pytest.fail.Exception):
with pytest.raises(RuntimeError):
Item.objects.exists()

def test_unblock_manually(self, django_db_blocker):
Expand Down