Skip to content

Commit a5bc562

Browse files
committed
Output at the end
1 parent 017693a commit a5bc562

File tree

3 files changed

+71
-23
lines changed

3 files changed

+71
-23
lines changed

pytest_django/fixtures.py

+21-20
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

@@ -101,8 +102,7 @@ def django_db_setup(
101102
**setup_databases_args
102103
)
103104

104-
if get_django_version() < (1, 11):
105-
run_check(request)
105+
run_checks(request)
106106

107107
def teardown_database():
108108
with django_db_blocker.unblock():
@@ -115,30 +115,31 @@ def teardown_database():
115115
request.addfinalizer(teardown_database)
116116

117117

118-
def run_check(request):
118+
def run_checks(request):
119119
from django.core.management import call_command
120120
from django.core.management.base import SystemCheckError
121121

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

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

143144

144145
@contextmanager

pytest_django/plugin.py

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from .fixtures import django_username_field # noqa
3131
from .fixtures import live_server # noqa
3232
from .fixtures import rf # noqa
33+
from .fixtures import run_checks # noqa
3334
from .fixtures import settings # noqa
3435
from .fixtures import transactional_db # noqa
3536
from .pytest_compat import getfixturevalue
@@ -319,6 +320,13 @@ def pytest_runtest_setup(item):
319320
_disable_class_methods(cls)
320321

321322

323+
def pytest_terminal_summary(terminalreporter, exitstatus):
324+
check_exc = getattr(run_checks, 'exc', None)
325+
if check_exc:
326+
terminalreporter.write('\n')
327+
terminalreporter.write(str(check_exc))
328+
329+
322330
@pytest.fixture(autouse=True, scope='session')
323331
def django_test_environment(request):
324332
"""

tests/test_fixtures.py

+42-3
Original file line numberDiff line numberDiff line change
@@ -220,24 +220,63 @@ def test_set_non_existent(settings):
220220
])
221221

222222

223+
@pytest.mark.django_project(extra_settings="""
224+
INSTALLED_APPS = ['tpkg.app']
225+
""")
223226
class TestChecks:
224227

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

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

242281

243282
class TestLiveServer:

0 commit comments

Comments
 (0)