diff --git a/tmt/steps/execute/__init__.py b/tmt/steps/execute/__init__.py index f1da4e7d52..63d91bad74 100644 --- a/tmt/steps/execute/__init__.py +++ b/tmt/steps/execute/__init__.py @@ -1169,3 +1169,29 @@ def results(self) -> list["tmt.result.Result"]: https://tmt.readthedocs.io/en/latest/spec/plans.html#execute """ return self._results + + def results_for_tests(self, tests: list['tmt.base.Test']) \ + -> list[tuple[Optional[Result], Optional['tmt.base.Test']]]: + """ + Collect results and corresponding tests. + + :returns: a list of result and test pairs. + * if there is not test found for the result, e.g. when + results were loaded from storage but tests were not, + ``None`` represents the missing test: ``(result, None)``. + * if there is no result for a test, e.g. when the test was + not executed, ``None`` represents the missing result: + ``(None, test)``. + """ + + known_serial_numbers = {test.serial_number: test for test in tests} + referenced_serial_numbers = {result.serial_number for result in self._results} + + return [ + (result, known_serial_numbers.get(result.serial_number)) + for result in self._results + ] + [ + (None, test) + for test in tests + if test.serial_number not in referenced_serial_numbers + ]