"""Test exception.""" import json import pytest from tests.testutils import exception as exception_utils class TestException(Exception): """TestException.""" __test__ = False def __init__( self, arg0: object, arg1: object, kwarg0: object | None = None, kwarg1: object | None = None, ) -> None: """Init TestException.""" super(TestException, self).__init__( json.dumps( { "arg0": arg0, "arg1": arg1, "kwarg0": kwarg0, "kwarg1": kwarg1, } ) ) @pytest.mark.parametrize( ("exception_class", "exception_args", "exception_kwargs", "expected_message"), [ (Exception, ["some message"], {}, "some message"), ( TestException, ["abc", "def"], {"kwarg0": "jkl", "kwarg1": "pqr"}, '{"arg0": "abc", "arg1": "def", "kwarg0": "jkl", "kwarg1": "pqr"}', ), ], ) def test_raise_exception( exception_class: type[BaseException], exception_args: list[object], exception_kwargs: dict[str, object], expected_message: str, ) -> None: """Test raise_exception_if_necessary.""" with pytest.raises(exception_class) as excinfo: exception_utils.raise_exception( exception_class, *exception_args, **exception_kwargs ) assert expected_message == str(excinfo.value) def test_reraise_exception() -> None: """Test reraise_exception.""" with pytest.raises(Exception) as excinfo: exception_utils.reraise_exception(Exception("test exception")) assert "test exception" == str(excinfo.value)