Skip to content

Commit f444b6a

Browse files
authored
DB blocker: use RuntimeError instead of pytest.fail (#781)
pytest fails to handle `pytest.fail.Exception` when raised in `repr`, resulting in confusing "INTERNALERROR" messages, hiding the real failure (since it happens during reporting). This changes pytest-django to raise `RuntimeError` instead. While this will be fixed via pytest-dev/pytest#6047 likely, it might be good to have this anyway (also for older pytest versions). Fixes #713 Fixes #341
1 parent 9dcc8cf commit f444b6a

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

pytest_django/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ def _save_active_wrapper(self):
777777
def _blocking_wrapper(*args, **kwargs):
778778
__tracebackhide__ = True
779779
__tracebackhide__ # Silence pyflakes
780-
pytest.fail(
780+
raise RuntimeError(
781781
"Database access not allowed, "
782782
'use the "django_db" mark, or the '
783783
'"db" or "transactional_db" fixtures to enable it.'

tests/test_database.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ def db_supports_reset_sequences():
1616

1717

1818
def test_noaccess():
19-
with pytest.raises(pytest.fail.Exception):
19+
with pytest.raises(RuntimeError):
2020
Item.objects.create(name="spam")
21-
with pytest.raises(pytest.fail.Exception):
21+
with pytest.raises(RuntimeError):
2222
Item.objects.count()
2323

2424

2525
@pytest.fixture
2626
def noaccess():
27-
with pytest.raises(pytest.fail.Exception):
27+
with pytest.raises(RuntimeError):
2828
Item.objects.create(name="spam")
29-
with pytest.raises(pytest.fail.Exception):
29+
with pytest.raises(RuntimeError):
3030
Item.objects.count()
3131

3232

@@ -254,7 +254,7 @@ def test_db_access_3(self):
254254
"*test_db_access_2 FAILED*",
255255
"*test_db_access_3 FAILED*",
256256
"*ERROR at setup of TestCase_setupClass.test_db_access_1*",
257-
'*Failed: Database access not allowed, use the "django_db" mark, '
257+
'*RuntimeError: Database access not allowed, use the "django_db" mark, '
258258
'or the "db" or "transactional_db" fixtures to enable it.',
259259
]
260260
)
@@ -274,7 +274,7 @@ def test_db_access_in_conftest(self, django_testdir):
274274
result = django_testdir.runpytest_subprocess("-v")
275275
result.stderr.fnmatch_lines(
276276
[
277-
'*Failed: Database access not allowed, use the "django_db" mark, '
277+
'*RuntimeError: Database access not allowed, use the "django_db" mark, '
278278
'or the "db" or "transactional_db" fixtures to enable it.*'
279279
]
280280
)
@@ -290,7 +290,7 @@ def test_db_access_in_test_module(self, django_testdir):
290290
result = django_testdir.runpytest_subprocess("-v")
291291
result.stdout.fnmatch_lines(
292292
[
293-
'*Failed: Database access not allowed, use the "django_db" mark, '
293+
'*RuntimeError: Database access not allowed, use the "django_db" mark, '
294294
'or the "db" or "transactional_db" fixtures to enable it.'
295295
]
296296
)

tests/test_db_access_in_repr.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
3+
def test_db_access_with_repr_in_report(django_testdir):
4+
django_testdir.create_test_module(
5+
"""
6+
import pytest
7+
8+
from .app.models import Item
9+
10+
def test_via_db_blocker(django_db_setup, django_db_blocker):
11+
with django_db_blocker.unblock():
12+
Item.objects.get(name='This one is not there')
13+
14+
def test_via_db_fixture(db):
15+
Item.objects.get(name='This one is not there')
16+
"""
17+
)
18+
19+
result = django_testdir.runpytest_subprocess("--tb=auto")
20+
result.stdout.fnmatch_lines([
21+
"tpkg/test_the_test.py FF",
22+
"E *DoesNotExist: Item matching query does not exist.",
23+
"tpkg/test_the_test.py:8: ",
24+
'self = *RuntimeError*Database access not allowed*',
25+
"E *DoesNotExist: Item matching query does not exist.",
26+
"* 2 failed in *",
27+
])
28+
assert "INTERNALERROR" not in str(result.stdout) + str(result.stderr)
29+
assert result.ret == 1

tests/test_environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def test_database_name():
219219

220220

221221
def test_database_noaccess():
222-
with pytest.raises(pytest.fail.Exception):
222+
with pytest.raises(RuntimeError):
223223
Item.objects.count()
224224

225225

tests/test_fixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,15 +607,15 @@ class Test_django_db_blocker:
607607
def test_block_manually(self, django_db_blocker):
608608
try:
609609
django_db_blocker.block()
610-
with pytest.raises(pytest.fail.Exception):
610+
with pytest.raises(RuntimeError):
611611
Item.objects.exists()
612612
finally:
613613
django_db_blocker.restore()
614614

615615
@pytest.mark.django_db
616616
def test_block_with_block(self, django_db_blocker):
617617
with django_db_blocker.block():
618-
with pytest.raises(pytest.fail.Exception):
618+
with pytest.raises(RuntimeError):
619619
Item.objects.exists()
620620

621621
def test_unblock_manually(self, django_db_blocker):

0 commit comments

Comments
 (0)