From 3ce681d167f30f63316f5423c391ade941138d10 Mon Sep 17 00:00:00 2001 From: Frank Niessink Date: Wed, 15 Jan 2025 23:19:52 +0100 Subject: [PATCH] When measuring test cases, warn the user if none of the configured sources contains test cases. (#10662) Fixes #10615. --- .../src/metric_collectors/test_cases.py | 4 ++++ .../metric_collectors/test_test_cases.py | 7 +++++++ .../source_collectors/junit/test_tests.py | 19 +++++++++++++++++++ docs/src/changelog.md | 1 + 4 files changed, 31 insertions(+) diff --git a/components/collector/src/metric_collectors/test_cases.py b/components/collector/src/metric_collectors/test_cases.py index 810bd7cbe0..8e4c32e8b9 100644 --- a/components/collector/src/metric_collectors/test_cases.py +++ b/components/collector/src/metric_collectors/test_cases.py @@ -79,6 +79,10 @@ async def collect(self) -> MetricMeasurement | None: return None test_cases = self.test_cases(measurement.sources) test_case_keys = set(test_cases.keys()) + if not test_case_keys: + for source in measurement.sources: + source.parse_error = "No test case keys found in this source" + return measurement # Derive the test result of the test cases from the test results of the tests for entity in self.test_report_entities(measurement.sources): for test_case_key in self.referenced_test_cases(entity) & test_case_keys: diff --git a/components/collector/tests/metric_collectors/test_test_cases.py b/components/collector/tests/metric_collectors/test_test_cases.py index c4e749d39f..c4ff838168 100644 --- a/components/collector/tests/metric_collectors/test_test_cases.py +++ b/components/collector/tests/metric_collectors/test_test_cases.py @@ -108,6 +108,13 @@ async def test_missing_sources(self): """Test missing sources.""" self.assertIsNone(await self.collect({})) + async def test_no_test_cases(self): + """Test missing test cases.""" + self.response.text = AsyncMock(return_value=self.JUNIT_XML) + junit = {"type": "junit", "parameters": {"url": self.test_report_url}} + measurement = await self.collect({"junit": junit}) + self.assertEqual("No test case keys found in this source", measurement.sources[0].parse_error) + async def test_no_test_report(self): """Test missing test report.""" measurement = await self.collect({"jira": {"type": "jira", "parameters": {"url": self.jira_url, "jql": "jql"}}}) diff --git a/components/collector/tests/source_collectors/junit/test_tests.py b/components/collector/tests/source_collectors/junit/test_tests.py index 74e1eb7f71..d63d36a684 100644 --- a/components/collector/tests/source_collectors/junit/test_tests.py +++ b/components/collector/tests/source_collectors/junit/test_tests.py @@ -63,3 +63,22 @@ async def test_repeated_test_case_names(self): """ response = await self.collect(get_request_text=junit_xml_repeated_test_case_name) self.assert_measurement(response, value="4", total="4") + + async def test_one_top_level_nested_test_suite(self): + """Test that a JUnit XML file with one top level nested test suite works.""" + xml = """ + + + + + + """ + response = await self.collect(get_request_text=xml) + self.assert_measurement( + response, + value="1", + total="1", + entities=[ + {"key": "cn:tc1", "old_key": "tc1", "name": "tc1", "class_name": "cn", "test_result": "errored"}, + ], + ) diff --git a/docs/src/changelog.md b/docs/src/changelog.md index b107fcabb9..f4e193d367 100644 --- a/docs/src/changelog.md +++ b/docs/src/changelog.md @@ -18,6 +18,7 @@ If your currently installed *Quality-time* version is not the latest version, pl - Don't throw an exception when a Trivy JSON file contains vulnerabilities without fixed version information. Fixes [#10606](https://github.com/ICTU/quality-time/issues/10606). - When measuring the number of job runs within a time period with Jenkins as source, don't throw an exception when a Jenkins pipeline build has no result yet. Fixes [#10610](https://github.com/ICTU/quality-time/issues/10610). +- When measuring test cases, warn the user if none of the configured sources contains test cases. Fixes [#10615](https://github.com/ICTU/quality-time/issues/10615). - Correctly count test suites in JUnit XML files with one root testsuite element. Fixes [#10616](https://github.com/ICTU/quality-time/issues/10616). ### Added