diff --git a/.travis.yml b/.travis.yml index 777e2b7cc..20fcba839 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,8 +37,9 @@ jobs: - python: 3.6 env: TOXENV=py36-djmaster-sqlite-coverage + # Explicitly test (older) pytest 5.3. - python: 3.5 - env: TOXENV=py35-dj110-postgres-coverage + env: TOXENV=py35-dj110-postgres-pytest53-coverage services: - postgresql diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 8b6e4ce79..12de3b6da 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 1f637e374..1f6dcd55c 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: diff --git a/tox.ini b/tox.ini index 84bbb9859..f1ddf2beb 100644 --- a/tox.ini +++ b/tox.ini @@ -29,6 +29,7 @@ deps = pytest41: pytest>=4.1,<4.2 pytest41: attrs==17.4.0 + pytest53: pytest>=5.3,<5.4 xdist: pytest-xdist>=1.15 setenv =