Skip to content

Commit 3bd72f0

Browse files
committed
Output at the end
1 parent 1f75bf7 commit 3bd72f0

File tree

3 files changed

+71
-23
lines changed

3 files changed

+71
-23
lines changed

pytest_django/fixtures.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import sys
77
from contextlib import contextmanager
8+
from io import BytesIO
89

910
import pytest
1011

@@ -99,8 +100,7 @@ def django_db_setup(
99100
**setup_databases_args
100101
)
101102

102-
if get_django_version() < (1, 11):
103-
run_check(request)
103+
run_checks(request)
104104

105105
def teardown_database():
106106
with django_db_blocker.unblock():
@@ -113,30 +113,31 @@ def teardown_database():
113113
request.addfinalizer(teardown_database)
114114

115115

116-
def run_check(request):
116+
def run_checks(request):
117117
from django.core.management import call_command
118118
from django.core.management.base import SystemCheckError
119119

120120
# Only run once per process
121-
if getattr(run_check, 'did_fail', False):
121+
if getattr(run_checks, 'ran', False):
122122
return
123+
run_checks.ran = True
123124

124-
with disable_input_capture(request):
125-
try:
126-
call_command('check')
127-
except SystemCheckError as ex:
128-
run_check.did_fail = True
129-
130-
if hasattr(request.config, 'slaveinput'):
131-
# Kill the xdist test process horribly
132-
# N.B. 'shouldstop' maybe be obeyed properly in later as hinted at in
133-
# https://github.com/pytest-dev/pytest-xdist/commit/e8fa73719662d1be5074a0750329fe0c35583484
134-
print(ex.args[0])
135-
sys.exit(1)
136-
else:
137-
request.session.exitstatus = 1
138-
request.session.shouldstop = True
139-
raise
125+
out = BytesIO()
126+
try:
127+
call_command('check', out=out, err=out)
128+
except SystemCheckError as ex:
129+
run_checks.exc = ex
130+
131+
if hasattr(request.config, 'slaveinput'):
132+
# Kill the xdist test process horribly
133+
# N.B. 'shouldstop' may be obeyed properly in the future as hinted at in
134+
# https://github.com/pytest-dev/pytest-xdist/commit/e8fa73719662d1be5074a0750329fe0c35583484
135+
print(ex.args[0])
136+
sys.exit(1)
137+
else:
138+
# Ensure we get the EXIT_TESTSFAILED exit code
139+
request.session.testsfailed += 1
140+
request.session.shouldstop = True
140141

141142

142143
@contextmanager

pytest_django/plugin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from .fixtures import django_username_field # noqa
3232
from .fixtures import live_server # noqa
3333
from .fixtures import rf # noqa
34+
from .fixtures import run_checks # noqa
3435
from .fixtures import settings # noqa
3536
from .fixtures import transactional_db # noqa
3637
from .pytest_compat import getfixturevalue
@@ -329,6 +330,13 @@ def pytest_runtest_setup(item):
329330
_disable_class_methods(cls)
330331

331332

333+
def pytest_terminal_summary(terminalreporter, exitstatus):
334+
check_exc = getattr(run_checks, 'exc', None)
335+
if check_exc:
336+
terminalreporter.write('\n')
337+
terminalreporter.write(str(check_exc))
338+
339+
332340
@pytest.fixture(autouse=True, scope='session')
333341
def django_test_environment(request):
334342
"""

tests/test_fixtures.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,24 +222,63 @@ def test_set_non_existent(settings):
222222
])
223223

224224

225+
@pytest.mark.django_project(extra_settings="""
226+
INSTALLED_APPS = ['tpkg.app']
227+
""")
225228
class TestChecks:
226229

227230
def test_checks_are_run(self, django_testdir):
228231
django_testdir.create_app_file("""
229232
from django.core.checks import Error, register
230233
231-
@register
234+
@register()
235+
def succeed(app_configs, **kwargs):
236+
succeed.did_run = True
237+
return []
238+
""", '__init__.py')
239+
django_testdir.makepyfile("""
240+
from tpkg.app import succeed
241+
242+
def test_simple(db):
243+
assert getattr(succeed, 'did_run', None) is True
244+
""")
245+
246+
result = django_testdir.runpytest_subprocess('-s')
247+
assert result.ret == 0
248+
249+
def test_failing_checks_fail_tests(self, django_testdir):
250+
django_testdir.create_app_file("""
251+
from django.core.checks import Error, register
252+
253+
@register()
232254
def fail(app_configs, **kwargs):
233255
return [Error('My failure message', id='test.001')]
234256
""", '__init__.py')
235257
django_testdir.makepyfile("""
236-
def test_simple():
258+
def test_simple(db):
237259
assert True
238260
""")
239261

240262
result = django_testdir.runpytest_subprocess('-s')
241-
result.stderr.fnmatch_lines(['*My failure message*'])
242263
assert result.ret != 0
264+
result.stdout.fnmatch_lines(['*My failure message*'])
265+
266+
def test_failing_checks_fail_tests_on_xdist(self, django_testdir):
267+
django_testdir.create_app_file("""
268+
from django.core.checks import Error, register
269+
270+
@register()
271+
def fail(app_configs, **kwargs):
272+
return [Error('My failure message', id='test.001')]
273+
""", '__init__.py')
274+
django_testdir.makepyfile("""
275+
def test_simple(db):
276+
assert True
277+
""")
278+
279+
result = django_testdir.runpytest_subprocess('-s', '-n2')
280+
assert result.ret != 0
281+
result.stdout.fnmatch_lines(['*My failure message*'])
243282

244283

245284
class TestLiveServer:

0 commit comments

Comments
 (0)