logfail: Continue running test and fail afterwards.
This commit is contained in:
parent
09c265ddb0
commit
66ed4e9c4e
@ -40,6 +40,7 @@ class LogFailHandler(logging.Handler):
|
||||
|
||||
def __init__(self, level=logging.NOTSET, min_level=logging.WARNING):
|
||||
self._min_level = min_level
|
||||
self.failed = False
|
||||
super().__init__(level)
|
||||
|
||||
def emit(self, record):
|
||||
@ -62,9 +63,7 @@ class LogFailHandler(logging.Handler):
|
||||
return
|
||||
if record.levelno < self._min_level:
|
||||
return
|
||||
pytest.fail("Got logging message on logger {} with level {}: "
|
||||
"{}!".format(record.name, record.levelname,
|
||||
record.getMessage()))
|
||||
self.failed = True
|
||||
|
||||
|
||||
@pytest.yield_fixture(scope='session', autouse=True)
|
||||
@ -74,6 +73,8 @@ def fail_on_logging():
|
||||
yield
|
||||
logging.getLogger().removeHandler(handler)
|
||||
handler.close()
|
||||
if handler.failed:
|
||||
pytest.fail("Got unexpected logging message!", pytrace=False)
|
||||
|
||||
|
||||
@pytest.yield_fixture(autouse=True)
|
||||
|
@ -127,6 +127,48 @@ def test_log_expected_wrong_logger(log_testdir):
|
||||
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
|
||||
def skipping_fixture():
|
||||
pytest.skip("Skipping to test caplog workaround.")
|
||||
|
Loading…
Reference in New Issue
Block a user