Skip to content

Commit c97a9a5

Browse files
committed
Output at the end
1 parent 1771cb1 commit c97a9a5

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
@@ -29,6 +29,7 @@
2929
from .fixtures import django_username_field # noqa
3030
from .fixtures import live_server # noqa
3131
from .fixtures import rf # noqa
32+
from .fixtures import run_checks # noqa
3233
from .fixtures import settings # noqa
3334
from .fixtures import transactional_db # noqa
3435
from .pytest_compat import getfixturevalue
@@ -318,6 +319,13 @@ def pytest_runtest_setup(item):
318319
_disable_class_methods(cls)
319320

320321

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

tests/test_fixtures.py

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

160160

161+
@pytest.mark.django_project(extra_settings="""
162+
INSTALLED_APPS = ['tpkg.app']
163+
""")
161164
class TestChecks:
162165

163166
def test_checks_are_run(self, django_testdir):
164167
django_testdir.create_app_file("""
165168
from django.core.checks import Error, register
166169
167-
@register
170+
@register()
171+
def succeed(app_configs, **kwargs):
172+
succeed.did_run = True
173+
return []
174+
""", '__init__.py')
175+
django_testdir.makepyfile("""
176+
from tpkg.app import succeed
177+
178+
def test_simple(db):
179+
assert getattr(succeed, 'did_run', None) is True
180+
""")
181+
182+
result = django_testdir.runpytest_subprocess('-s')
183+
assert result.ret == 0
184+
185+
def test_failing_checks_fail_tests(self, django_testdir):
186+
django_testdir.create_app_file("""
187+
from django.core.checks import Error, register
188+
189+
@register()
168190
def fail(app_configs, **kwargs):
169191
return [Error('My failure message', id='test.001')]
170192
""", '__init__.py')
171193
django_testdir.makepyfile("""
172-
def test_simple():
194+
def test_simple(db):
173195
assert True
174196
""")
175197

176198
result = django_testdir.runpytest_subprocess('-s')
177-
result.stderr.fnmatch_lines(['*My failure message*'])
178199
assert result.ret != 0
200+
result.stdout.fnmatch_lines(['*My failure message*'])
201+
202+
def test_failing_checks_fail_tests_on_xdist(self, django_testdir):
203+
django_testdir.create_app_file("""
204+
from django.core.checks import Error, register
205+
206+
@register()
207+
def fail(app_configs, **kwargs):
208+
return [Error('My failure message', id='test.001')]
209+
""", '__init__.py')
210+
django_testdir.makepyfile("""
211+
def test_simple(db):
212+
assert True
213+
""")
214+
215+
result = django_testdir.runpytest_subprocess('-s', '-n2')
216+
assert result.ret != 0
217+
result.stdout.fnmatch_lines(['*My failure message*'])
179218

180219

181220
class TestLiveServer:

0 commit comments

Comments
 (0)