Skip to content

Commit

Permalink
Remove JunitCaseException exception object
Browse files Browse the repository at this point in the history
  • Loading branch information
eliorerz committed Aug 4, 2021
1 parent 14a64ba commit 18f9d1c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
7 changes: 4 additions & 3 deletions src/junit_report/_junit_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from . import _utils
from ._junit_decorator import JunitDecorator
from ._junit_test_suite import JunitTestSuite
from ._test_case_data import CaseFailure, JunitCaseException, TestCaseCategories, TestCaseData
from ._test_case_data import CaseFailure, TestCaseCategories, TestCaseData, is_case_exception_already_raised


class JunitTestCase(JunitDecorator):
Expand Down Expand Up @@ -45,10 +45,11 @@ def _add_failure(self, e: BaseException, message_prefix: str = ""):

def _on_exception(self, e: BaseException):
self._case_data.had_exception = True
if isinstance(e, JunitCaseException):
if is_case_exception_already_raised(e):
raise # already registered on son test case
setattr(e, "__is_junit_exception__", True)
self._add_failure(e)
raise JunitCaseException(exception=e)
raise e

def _on_wrapper_end(self) -> bool:
self._case_data.set_fin_time()
Expand Down
7 changes: 4 additions & 3 deletions src/junit_report/_junit_test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from junit_xml import TestCase, TestSuite, to_xml_report_string

from ._junit_decorator import JunitDecorator
from ._test_case_data import CaseFailure, JunitCaseException, TestCaseCategories, TestCaseData
from ._test_case_data import CaseFailure, TestCaseCategories, TestCaseData, is_case_exception_already_raised


class DuplicateSuiteError(KeyError):
Expand Down Expand Up @@ -196,8 +196,9 @@ def _collect_yield(self):
self._export(self.suite)

def _on_exception(self, e: BaseException):
if isinstance(e, JunitCaseException):
raise e.exception
if is_case_exception_already_raised(e):
raise e

self._handle_in_suite_exception(e)

def _handle_in_suite_exception(self, exception: BaseException):
Expand Down
19 changes: 7 additions & 12 deletions src/junit_report/_test_case_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from junit_xml import TestCase


JUNIT_EXCEPTION_ON_TAG = "__is_junit_exception__"


class TestCaseCategories(Enum):
FUNCTION = "function"
FIXTURE = "fixture"
Expand All @@ -17,18 +20,6 @@ class MainRunner(Enum):
NONE = "none"


class JunitCaseException(BaseException):
def __init__(self, exception: BaseException, *args: object) -> None:
super().__init__(*args)
self.exception: BaseException = exception

def __str__(self) -> str:
return self.exception.__str__()

def __repr__(self) -> str:
return self.exception.__repr__()


@dataclass
class CaseFailure:
message: str
Expand Down Expand Up @@ -73,3 +64,7 @@ def set_parametrize(self, params: List[Tuple[str, Any]]):
def set_parent(self, case_parent_name: str):
self._has_parent = True
self.case.classname = f"{self.case.classname}.{case_parent_name}"


def is_case_exception_already_raised(exception: BaseException) -> bool:
return hasattr(exception, JUNIT_EXCEPTION_ON_TAG)

0 comments on commit 18f9d1c

Please sign in to comment.