Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around unittest issue with pytest 5.4.{0,1} #825

Merged
merged 2 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
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
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down