tests: Don't fail on teardown too if test failed.

When a end-to-end test failed which would've marked an error message as
expected later in the test, seeing the teardown message about an unexpected
error being logged is really confusing.
This commit is contained in:
Florian Bruhin 2016-02-04 06:43:14 +01:00
parent 43b6f18864
commit 774bcbf6b3
3 changed files with 36 additions and 6 deletions

View File

@ -435,3 +435,14 @@ def pytest_configure(config):
def pytest_unconfigure(config):
if config.xvfb_display is not None:
config.xvfb_display.stop()
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
"""Make test information available in fixtures.
See http://pytest.org/latest/example/simple.html#making-test-result-information-available-in-fixtures
"""
outcome = yield
rep = outcome.get_result()
setattr(item, "rep_" + rep.when, rep)

View File

@ -259,11 +259,21 @@ class QuteProc(testprocess.Process):
if skip_texts:
pytest.skip(', '.join(skip_texts))
def after_test(self):
def after_test(self, did_fail):
"""Handle unexpected/skip logging and clean up after each test.
Args:
did_fail: Set if the main test failed already, then logged errors
are ignored.
"""
__tracebackhide__ = True
bad_msgs = [msg for msg in self._data
if self._is_error_logline(msg) and not msg.expected]
if did_fail:
super().after_test()
return
try:
if bad_msgs:
text = 'Logged unexpected errors:\n\n' + '\n'.join(
@ -399,7 +409,7 @@ def quteproc(quteproc_process, httpbin, request):
request.node._quteproc_log = quteproc_process.captured_log
quteproc_process.before_test()
yield quteproc_process
quteproc_process.after_test()
quteproc_process.after_test(did_fail=request.node.rep_call.failed)
@pytest.yield_fixture
@ -410,4 +420,4 @@ def quteproc_new(qapp, httpbin, request):
request.node._quteproc_log = proc.captured_log
# Not calling before_test here as that would start the process
yield proc
proc.after_test()
proc.after_test(did_fail=request.node.rep_call.failed)

View File

@ -40,7 +40,16 @@ def test_quteproc_error_message(qtbot, quteproc, cmd):
# Usually we wouldn't call this from inside a test, but here we force the
# error to occur during the test rather than at teardown time.
with pytest.raises(pytest.fail.Exception):
quteproc.after_test()
quteproc.after_test(did_fail=False)
def test_quteproc_error_message_did_fail(qtbot, quteproc):
"""Make sure the test does not fail on teardown if the main test failed."""
with qtbot.waitSignal(quteproc.got_error):
quteproc.send_cmd(':message-error test')
# Usually we wouldn't call this from inside a test, but here we force the
# error to occur during the test rather than at teardown time.
quteproc.after_test(did_fail=True)
def test_quteproc_skip_via_js(qtbot, quteproc):
@ -50,7 +59,7 @@ def test_quteproc_skip_via_js(qtbot, quteproc):
# Usually we wouldn't call this from inside a test, but here we force
# the error to occur during the test rather than at teardown time.
quteproc.after_test()
quteproc.after_test(did_fail=False)
assert str(excinfo.value) == 'test'
@ -74,7 +83,7 @@ def test_quteprocess_quitting(qtbot, quteproc_process):
with qtbot.waitSignal(quteproc_process.proc.finished, timeout=15000):
quteproc_process.send_cmd(':quit')
with pytest.raises(testprocess.ProcessExited):
quteproc_process.after_test()
quteproc_process.after_test(did_fail=False)
@pytest.mark.parametrize('data, attrs', [