Skip to content

Commit

Permalink
Output at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz committed May 16, 2017
1 parent 017693a commit a5bc562
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 23 deletions.
41 changes: 21 additions & 20 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import sys
from contextlib import contextmanager
from io import BytesIO

import pytest

Expand Down Expand Up @@ -101,8 +102,7 @@ def django_db_setup(
**setup_databases_args
)

if get_django_version() < (1, 11):
run_check(request)
run_checks(request)

def teardown_database():
with django_db_blocker.unblock():
Expand All @@ -115,30 +115,31 @@ def teardown_database():
request.addfinalizer(teardown_database)


def run_check(request):
def run_checks(request):
from django.core.management import call_command
from django.core.management.base import SystemCheckError

# Only run once per process
if getattr(run_check, 'did_fail', False):
if getattr(run_checks, 'ran', False):
return
run_checks.ran = True

with disable_input_capture(request):
try:
call_command('check')
except SystemCheckError as ex:
run_check.did_fail = True

if hasattr(request.config, 'slaveinput'):
# Kill the xdist test process horribly
# N.B. 'shouldstop' maybe be obeyed properly in later as hinted at in
# https://github.com/pytest-dev/pytest-xdist/commit/e8fa73719662d1be5074a0750329fe0c35583484
print(ex.args[0])
sys.exit(1)
else:
request.session.exitstatus = 1
request.session.shouldstop = True
raise
out = BytesIO()
try:
call_command('check', out=out, err=out)
except SystemCheckError as ex:
run_checks.exc = ex

if hasattr(request.config, 'slaveinput'):
# Kill the xdist test process horribly
# N.B. 'shouldstop' may be obeyed properly in the future as hinted at in
# https://github.com/pytest-dev/pytest-xdist/commit/e8fa73719662d1be5074a0750329fe0c35583484
print(ex.args[0])
sys.exit(1)
else:
# Ensure we get the EXIT_TESTSFAILED exit code
request.session.testsfailed += 1
request.session.shouldstop = True


@contextmanager
Expand Down
8 changes: 8 additions & 0 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .fixtures import django_username_field # noqa
from .fixtures import live_server # noqa
from .fixtures import rf # noqa
from .fixtures import run_checks # noqa
from .fixtures import settings # noqa
from .fixtures import transactional_db # noqa
from .pytest_compat import getfixturevalue
Expand Down Expand Up @@ -319,6 +320,13 @@ def pytest_runtest_setup(item):
_disable_class_methods(cls)


def pytest_terminal_summary(terminalreporter, exitstatus):
check_exc = getattr(run_checks, 'exc', None)
if check_exc:
terminalreporter.write('\n')
terminalreporter.write(str(check_exc))


@pytest.fixture(autouse=True, scope='session')
def django_test_environment(request):
"""
Expand Down
45 changes: 42 additions & 3 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,24 +220,63 @@ def test_set_non_existent(settings):
])


@pytest.mark.django_project(extra_settings="""
INSTALLED_APPS = ['tpkg.app']
""")
class TestChecks:

def test_checks_are_run(self, django_testdir):
django_testdir.create_app_file("""
from django.core.checks import Error, register
@register
@register()
def succeed(app_configs, **kwargs):
succeed.did_run = True
return []
""", '__init__.py')
django_testdir.makepyfile("""
from tpkg.app import succeed
def test_simple(db):
assert getattr(succeed, 'did_run', None) is True
""")

result = django_testdir.runpytest_subprocess('-s')
assert result.ret == 0

def test_failing_checks_fail_tests(self, django_testdir):
django_testdir.create_app_file("""
from django.core.checks import Error, register
@register()
def fail(app_configs, **kwargs):
return [Error('My failure message', id='test.001')]
""", '__init__.py')
django_testdir.makepyfile("""
def test_simple():
def test_simple(db):
assert True
""")

result = django_testdir.runpytest_subprocess('-s')
result.stderr.fnmatch_lines(['*My failure message*'])
assert result.ret != 0
result.stdout.fnmatch_lines(['*My failure message*'])

def test_failing_checks_fail_tests_on_xdist(self, django_testdir):
django_testdir.create_app_file("""
from django.core.checks import Error, register
@register()
def fail(app_configs, **kwargs):
return [Error('My failure message', id='test.001')]
""", '__init__.py')
django_testdir.makepyfile("""
def test_simple(db):
assert True
""")

result = django_testdir.runpytest_subprocess('-s', '-n2')
assert result.ret != 0
result.stdout.fnmatch_lines(['*My failure message*'])


class TestLiveServer:
Expand Down

0 comments on commit a5bc562

Please sign in to comment.