diff --git a/python/publish/unittestresults.py b/python/publish/unittestresults.py index 14eb1ee7..127ea623 100644 --- a/python/publish/unittestresults.py +++ b/python/publish/unittestresults.py @@ -78,7 +78,7 @@ def with_commit(self, commit: str) -> 'ParsedUnitTestResultsWithCommit': class ParsedUnitTestResultsWithCommit(ParsedUnitTestResults): commit: str - def with_stats(self, + def with_cases(self, cases_skipped: int, cases_failures: int, cases_errors: int, @@ -113,6 +113,22 @@ def with_stats(self, tests_errors=tests_errors ) + def without_cases(self): + # when there are no case information, we use the + # testsuite information for case and test level + return self.with_cases( + # test states and counts from cases + cases_skipped=self.suite_skipped, + cases_failures=self.suite_failures, + cases_errors=self.suite_errors, + cases_time=self.suite_time, + case_results=UnitTestCaseResults(), + + tests=self.suite_tests, + tests_skipped=self.suite_skipped, + tests_failures=self.suite_failures, + tests_errors=self.suite_errors, + ) @dataclass(frozen=True) class UnitTestResults(ParsedUnitTestResultsWithCommit): @@ -268,6 +284,10 @@ def get_test_results(parsed_results: ParsedUnitTestResultsWithCommit, :return: unit test result statistics """ cases = parsed_results.cases + + if len(cases) == 0: + return parsed_results.without_cases() + cases_skipped = [case for case in cases if case.result in ['skipped', 'disabled']] cases_failures = [case for case in cases if case.result == 'failure'] cases_errors = [case for case in cases if case.result == 'error'] @@ -288,7 +308,7 @@ def get_test_results(parsed_results: ParsedUnitTestResultsWithCommit, tests_failures = len([test for test, state in test_results.items() if state == 'failure']) tests_errors = len([test for test, state in test_results.items() if state == 'error']) - return parsed_results.with_stats( + return parsed_results.with_cases( # test states and counts from cases cases_skipped=len(cases_skipped), cases_failures=len(cases_failures), diff --git a/python/test/files/no-cases-but-tests.xml b/python/test/files/no-cases-but-tests.xml new file mode 100644 index 00000000..a1f85fbb --- /dev/null +++ b/python/test/files/no-cases-but-tests.xml @@ -0,0 +1 @@ + diff --git a/python/test/test_publish.py b/python/test/test_publish.py index dc8fe64d..d835daa0 100644 --- a/python/test/test_publish.py +++ b/python/test/test_publish.py @@ -1868,6 +1868,16 @@ def test_file_without_cases(self): f'\n' f'Results for commit a commit.\n')) + def test_file_without_cases_but_with_tests(self): + parsed = parse_junit_xml_files(['files/no-cases-but-tests.xml']).with_commit('a commit sha') + results = get_test_results(parsed, False) + stats = get_stats(results) + md = get_long_summary_md(stats) + self.assertEqual(md, (f'1 files  1 suites   0s {duration_label_md}\n' + f'6 {all_tests_label_md} 3 {passed_tests_label_md} 2 {skipped_tests_label_md} 1 {failed_tests_label_md}\n' + f'\n' + f'Results for commit a commit.\n')) + def test_non_parsable_file(self): parsed = parse_junit_xml_files(['files/empty.xml']).with_commit('a commit sha') results = get_test_results(parsed, False)