Skip to content

Commit

Permalink
Work around unittest issue with pytest 5.4.{0,1}
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Mar 15, 2020
1 parent 004ed4e commit f9b6b9f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
20 changes: 8 additions & 12 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,20 +507,16 @@ def _django_setup_unittest(request, django_db_blocker):
yield
return

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

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

def non_debugging_runtest(self):
self._testcase(result=self)

mp_debug = MonkeyPatch()
mp_debug.setattr("_pytest.unittest.TestCaseFunction.runtest", non_debugging_runtest)
else:
mp_debug = None
mp_debug = MonkeyPatch()
mp_debug.setattr("_pytest.unittest.TestCaseFunction.runtest", non_debugging_runtest)

request.getfixturevalue("django_db_setup")

Expand Down
24 changes: 20 additions & 4 deletions tests/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ def tearDown(self):

def test_sole_test(django_testdir):
"""
Make sure the database are configured when only Django TestCase classes
Make sure the database is configured when only Django TestCase classes
are collected, without the django_db marker.
"""
Also ensures that the DB is available after a failure (#824).
"""
django_testdir.create_test_module(
"""
import os
Expand All @@ -80,12 +81,27 @@ def test_foo(self):
# Make sure it is usable
assert Item.objects.count() == 0
assert 0, "trigger_error"
class TestBar(TestCase):
def test_bar(self):
assert Item.objects.count() == 0
"""
)

result = django_testdir.runpytest_subprocess("-v")
result.stdout.fnmatch_lines(["*TestFoo*test_foo PASSED*"])
assert result.ret == 0
result.stdout.fnmatch_lines(
[
"*::test_foo FAILED",
"*::test_bar PASSED",
'> assert 0, "trigger_error"',
"E AssertionError: trigger_error",
"E assert 0",
"*= 1 failed, 1 passed in *",
]
)
assert result.ret == 1


class TestUnittestMethods:
Expand Down

0 comments on commit f9b6b9f

Please sign in to comment.