logfail: Continue running test and fail afterwards.

This commit is contained in:
Florian Bruhin 2015-09-09 07:43:56 +02:00
parent 09c265ddb0
commit 66ed4e9c4e
2 changed files with 46 additions and 3 deletions

View File

@ -40,6 +40,7 @@ class LogFailHandler(logging.Handler):
def __init__(self, level=logging.NOTSET, min_level=logging.WARNING): def __init__(self, level=logging.NOTSET, min_level=logging.WARNING):
self._min_level = min_level self._min_level = min_level
self.failed = False
super().__init__(level) super().__init__(level)
def emit(self, record): def emit(self, record):
@ -62,9 +63,7 @@ class LogFailHandler(logging.Handler):
return return
if record.levelno < self._min_level: if record.levelno < self._min_level:
return return
pytest.fail("Got logging message on logger {} with level {}: " self.failed = True
"{}!".format(record.name, record.levelname,
record.getMessage()))
@pytest.yield_fixture(scope='session', autouse=True) @pytest.yield_fixture(scope='session', autouse=True)
@ -74,6 +73,8 @@ def fail_on_logging():
yield yield
logging.getLogger().removeHandler(handler) logging.getLogger().removeHandler(handler)
handler.close() handler.close()
if handler.failed:
pytest.fail("Got unexpected logging message!", pytrace=False)
@pytest.yield_fixture(autouse=True) @pytest.yield_fixture(autouse=True)

View File

@ -127,6 +127,48 @@ def test_log_expected_wrong_logger(log_testdir):
res.stdout.fnmatch_lines(['*1 error*']) res.stdout.fnmatch_lines(['*1 error*'])
def test_output_passing(log_testdir):
log_testdir.makepyfile("""
import logging
def test_foo():
logging.error('log msg 1')
logging.error('log msg 2')
""")
res = log_testdir.runpytest('-p capturelog')
res.stdout.fnmatch_lines([
'*= ERRORS =*',
'*_ ERROR at teardown of test_foo _*',
'Got unexpected logging message!',
'*1 error*',
])
def test_output_failing(log_testdir):
log_testdir.makepyfile("""
import logging
def test_foo():
logging.error('log msg 1')
logging.error('log msg 2')
raise Exception
""")
res = log_testdir.runpytest('-p capturelog')
res.stdout.fnmatch_lines([
'*= ERRORS =*',
'*_ ERROR at teardown of test_foo _*',
'Got unexpected logging message!',
'*= FAILURES =*',
'*- Captured log -*',
'*ERROR log msg 1',
'*ERROR log msg 2',
'*1 error*',
])
@pytest.fixture @pytest.fixture
def skipping_fixture(): def skipping_fixture():
pytest.skip("Skipping to test caplog workaround.") pytest.skip("Skipping to test caplog workaround.")