Handle missing test_results due to pytest runner interrupt

If pytest execution is interrupted, the hook that exposes test_results
to the pytest session is never called so the results can't be
interpreted.

(cherry picked from commit 0a063f51d3)
This commit is contained in:
Tom Krizek 2023-03-22 15:52:05 +01:00
parent 6103ee988e
commit 445b30b883
No known key found for this signature in database
GPG key ID: 01623B9B652A20A7

View file

@ -316,10 +316,17 @@ if os.getenv("LEGACY_TEST_RUNNER", "0") == "0":
def get_test_result():
"""Aggregate test results from all individual tests from this module
into a single result: failed > skipped > passed."""
try:
all_test_results = request.session.test_results
except AttributeError:
# This may happen if pytest execution is interrupted and
# pytest_runtest_makereport() is never called.
logger.debug("can't obtain test results, test run was interrupted")
return "error"
test_results = {
node.nodeid: request.session.test_results[node.nodeid]
node.nodeid: all_test_results[node.nodeid]
for node in request.node.collect()
if node.nodeid in request.session.test_results
if node.nodeid in all_test_results
}
logger.debug(test_results)
assert len(test_results)