From f9b6b9fa64ec00b2301a54bb8b7093b451c51632 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 14 Mar 2020 02:01:14 +0100 Subject: [PATCH] Work around unittest issue with pytest 5.4.{0,1} Ref: https://github.com/pytest-dev/pytest-django/issues/824 --- pytest_django/plugin.py | 20 ++++++++------------ tests/test_unittest.py | 24 ++++++++++++++++++++---- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 4deca87f3..f897cc5a3 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -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") diff --git a/tests/test_unittest.py b/tests/test_unittest.py index 985e868ed..997b86fd5 100644 --- a/tests/test_unittest.py +++ b/tests/test_unittest.py @@ -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 @@ -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: