From 61dfc24195863e883e2b5f28440ccf2f42b019a5 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Fri, 20 Dec 2024 18:32:11 +0100 Subject: [PATCH 1/2] Rework types of XUnit2 rerun and JUnit final results --- junitparser/junitparser.py | 23 ++++++++++++++--------- junitparser/xunit2.py | 16 +++++++++------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/junitparser/junitparser.py b/junitparser/junitparser.py index 6790feb..fee250c 100644 --- a/junitparser/junitparser.py +++ b/junitparser/junitparser.py @@ -241,7 +241,12 @@ def text(self, value: str): self._elem.text = value -class Skipped(Result): +class FinalResult(Result): + """Base class for final test result (in contrast to XUnit2 RerunResult).""" + + _tag = None + +class Skipped(FinalResult): """Test result when the case is skipped.""" _tag = "skipped" @@ -250,7 +255,7 @@ def __eq__(self, other): return super().__eq__(other) -class Failure(Result): +class Failure(FinalResult): """Test result when the case failed.""" _tag = "failure" @@ -259,7 +264,7 @@ def __eq__(self, other): return super().__eq__(other) -class Error(Result): +class Error(FinalResult): """Test result when the case has errors during execution.""" _tag = "error" @@ -268,7 +273,7 @@ def __eq__(self, other): return super().__eq__(other) -POSSIBLE_RESULTS = {Failure, Error, Skipped} +FINAL_RESULTS = {Failure, Error, Skipped} class System(Element): @@ -329,7 +334,7 @@ def __hash__(self): return super().__hash__() def __iter__(self): - all_types = set.union(POSSIBLE_RESULTS, {SystemOut}, {SystemErr}) + all_types = set.union(FINAL_RESULTS, {SystemOut}, {SystemErr}) for elem in self._elem.iter(): for entry_type in all_types: if elem.tag == entry_type._tag: @@ -353,11 +358,11 @@ def is_skipped(self): return False @property - def result(self): + def result(self) -> list[FinalResult]: """A list of :class:`Failure`, :class:`Skipped`, or :class:`Error` objects.""" results = [] for entry in self: - if isinstance(entry, tuple(POSSIBLE_RESULTS)): + if isinstance(entry, FinalResult): results.append(entry) return results @@ -366,13 +371,13 @@ def result(self): def result(self, value: Union[Result, List[Result]]): # First remove all existing results for entry in self.result: - if any(isinstance(entry, r) for r in POSSIBLE_RESULTS): + if isinstance(entry, FinalResult): self.remove(entry) if isinstance(value, Result): self.append(value) elif isinstance(value, list): for entry in value: - if any(isinstance(entry, r) for r in POSSIBLE_RESULTS): + if isinstance(entry, FinalResult): self.append(entry) @property diff --git a/junitparser/xunit2.py b/junitparser/xunit2.py index 014d0a2..81ec2e3 100644 --- a/junitparser/xunit2.py +++ b/junitparser/xunit2.py @@ -99,8 +99,10 @@ class StackTrace(junitparser.System): _tag = "stackTrace" -class RerunType(junitparser.Result): - _tag = "rerunType" +class RerunResult(junitparser.Result): + """Base class for intermediate / rerun test result (in contrast to JUnit FinalResult).""" + + _tag = None @property def stack_trace(self): @@ -157,19 +159,19 @@ def system_err(self, value: str): self.append(err) -class RerunFailure(RerunType): +class RerunFailure(RerunResult): _tag = "rerunFailure" -class RerunError(RerunType): +class RerunError(RerunResult): _tag = "rerunError" -class FlakyFailure(RerunType): +class FlakyFailure(RerunResult): _tag = "flakyFailure" -class FlakyError(RerunType): +class FlakyError(RerunResult): _tag = "flakyError" @@ -199,6 +201,6 @@ def flaky_errors(self): """""" return self._rerun_results(FlakyError) - def add_rerun_result(self, result: RerunType): + def add_rerun_result(self, result: RerunResult): """Append a rerun result to the testcase. A testcase can have multiple rerun results.""" self.append(result) From 13f1182369d432ab411a58a6910d56c83006dd5f Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Fri, 20 Dec 2024 18:57:18 +0100 Subject: [PATCH 2/2] Simplify type checking in TestCase.result, enforce input types --- junitparser/junitparser.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/junitparser/junitparser.py b/junitparser/junitparser.py index fee250c..6a40a6b 100644 --- a/junitparser/junitparser.py +++ b/junitparser/junitparser.py @@ -358,7 +358,7 @@ def is_skipped(self): return False @property - def result(self) -> list[FinalResult]: + def result(self) -> List[FinalResult]: """A list of :class:`Failure`, :class:`Skipped`, or :class:`Error` objects.""" results = [] for entry in self: @@ -368,17 +368,20 @@ def result(self) -> list[FinalResult]: return results @result.setter - def result(self, value: Union[Result, List[Result]]): + def result(self, value: Union[FinalResult, List[FinalResult]]): + # Check typing + if not (isinstance(value, FinalResult) or + isinstance(value, list) and all(isinstance(item, FinalResult) for item in value)): + raise ValueError("Value must be either FinalResult or list of FinalResult") + # First remove all existing results for entry in self.result: - if isinstance(entry, FinalResult): - self.remove(entry) - if isinstance(value, Result): + self.remove(entry) + if isinstance(value, FinalResult): self.append(value) - elif isinstance(value, list): + else: for entry in value: - if isinstance(entry, FinalResult): - self.append(entry) + self.append(entry) @property def system_out(self):