Skip to content

Commit

Permalink
Support testsuites without testcases (#233)
Browse files Browse the repository at this point in the history
* Use numbers from suites when there are no cases
* Renaming with_stats to with_cases, adding without_cases
  • Loading branch information
EnricoMi committed Mar 7, 2022
1 parent d512bc6 commit f57c7ad
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
24 changes: 22 additions & 2 deletions python/publish/unittestresults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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']
Expand All @@ -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),
Expand Down
1 change: 1 addition & 0 deletions python/test/files/no-cases-but-tests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite errors="0" failures="1" hostname="test" name="pytest" skipped="2" tests="6" time="0.010" timestamp="2020-09-01T07:11:23.815873"></testsuite></testsuites>
10 changes: 10 additions & 0 deletions python/test/test_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f57c7ad

Please sign in to comment.