Skip to content

Commit b3ba275

Browse files
committed
DB blocker: use RuntimeError instead of pytest.fail
pytest fails to handle `pytest.fail.Exception` when raised in `repr`. This changes pytest-django to raise `RuntimeError` instead. Ref: pytest-dev/pytest#6020 Fixes pytest-dev#713
1 parent 9dcc8cf commit b3ba275

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-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_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)