Skip to content

Commit f9b6b9f

Browse files
committed
Work around unittest issue with pytest 5.4.{0,1}
Ref: #824
1 parent 004ed4e commit f9b6b9f

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

pytest_django/plugin.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -507,20 +507,16 @@ def _django_setup_unittest(request, django_db_blocker):
507507
yield
508508
return
509509

510-
from _pytest.unittest import TestCaseFunction
510+
# Fix/patch pytest.
511+
# Before pytest 5.4: https://github.com/pytest-dev/pytest/issues/5991
512+
# After pytest 5.4: https://github.com/pytest-dev/pytest-django/issues/824
513+
from _pytest.monkeypatch import MonkeyPatch
511514

512-
if "debug" in TestCaseFunction.runtest.__code__.co_names:
513-
# Fix pytest (https://github.com/pytest-dev/pytest/issues/5991), only
514-
# if "self._testcase.debug()" is being used (forward compatible).
515-
from _pytest.monkeypatch import MonkeyPatch
515+
def non_debugging_runtest(self):
516+
self._testcase(result=self)
516517

517-
def non_debugging_runtest(self):
518-
self._testcase(result=self)
519-
520-
mp_debug = MonkeyPatch()
521-
mp_debug.setattr("_pytest.unittest.TestCaseFunction.runtest", non_debugging_runtest)
522-
else:
523-
mp_debug = None
518+
mp_debug = MonkeyPatch()
519+
mp_debug.setattr("_pytest.unittest.TestCaseFunction.runtest", non_debugging_runtest)
524520

525521
request.getfixturevalue("django_db_setup")
526522

tests/test_unittest.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ def tearDown(self):
5858

5959
def test_sole_test(django_testdir):
6060
"""
61-
Make sure the database are configured when only Django TestCase classes
61+
Make sure the database is configured when only Django TestCase classes
6262
are collected, without the django_db marker.
63-
"""
6463
64+
Also ensures that the DB is available after a failure (#824).
65+
"""
6566
django_testdir.create_test_module(
6667
"""
6768
import os
@@ -80,12 +81,27 @@ def test_foo(self):
8081
8182
# Make sure it is usable
8283
assert Item.objects.count() == 0
84+
85+
assert 0, "trigger_error"
86+
87+
class TestBar(TestCase):
88+
def test_bar(self):
89+
assert Item.objects.count() == 0
8390
"""
8491
)
8592

8693
result = django_testdir.runpytest_subprocess("-v")
87-
result.stdout.fnmatch_lines(["*TestFoo*test_foo PASSED*"])
88-
assert result.ret == 0
94+
result.stdout.fnmatch_lines(
95+
[
96+
"*::test_foo FAILED",
97+
"*::test_bar PASSED",
98+
'> assert 0, "trigger_error"',
99+
"E AssertionError: trigger_error",
100+
"E assert 0",
101+
"*= 1 failed, 1 passed in *",
102+
]
103+
)
104+
assert result.ret == 1
89105

90106

91107
class TestUnittestMethods:

0 commit comments

Comments
 (0)