diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b4e08d..e74c739 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - URL with more than one value for the same parameter were not matched properly (matching was performed on the first value). +- `httpx_mock.add_exception` is now properly documented (accepts `BaseException` instead of `Exception`). ## [0.35.0] - 2024-11-28 ### Changed diff --git a/README.md b/README.md index 6cd30ce..6d97041 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Build status Coverage Code style: black -Number of tests +Number of tests Number of downloads

diff --git a/pytest_httpx/_httpx_mock.py b/pytest_httpx/_httpx_mock.py index f51d15b..6667f21 100644 --- a/pytest_httpx/_httpx_mock.py +++ b/pytest_httpx/_httpx_mock.py @@ -120,7 +120,7 @@ def add_callback( """ self._callbacks.append((_RequestMatcher(self._options, **matchers), callback)) - def add_exception(self, exception: Exception, **matchers: Any) -> None: + def add_exception(self, exception: BaseException, **matchers: Any) -> None: """ Raise an exception if a request match. diff --git a/tests/test_httpx_async.py b/tests/test_httpx_async.py index ac4b3fb..3f1ab89 100644 --- a/tests/test_httpx_async.py +++ b/tests/test_httpx_async.py @@ -1052,15 +1052,30 @@ async def test_request_exception_raising(httpx_mock: HTTPXMock) -> None: @pytest.mark.asyncio -async def test_non_request_exception_raising(httpx_mock: HTTPXMock) -> None: - httpx_mock.add_exception( - httpx.HTTPError("Unable to read within 5.0"), url="https://test_url" - ) +@pytest.mark.parametrize( + ("exception_type", "message"), + [ + # httpx exception without request context + pytest.param( + httpx.HTTPError, "Unable to read within 5.0", id="non_request_exception" + ), + # BaseException derived exception + pytest.param( + asyncio.CancelledError, + "Request was cancelled", + id="cancelled_exception", + ), + ], +) +async def test_non_request_exception_raising( + httpx_mock: HTTPXMock, exception_type: type, message: str +) -> None: + httpx_mock.add_exception(exception_type(message), url="https://test_url") async with httpx.AsyncClient() as client: - with pytest.raises(httpx.HTTPError) as exception_info: + with pytest.raises(exception_type) as exception_info: await client.get("https://test_url") - assert str(exception_info.value) == "Unable to read within 5.0" + assert str(exception_info.value) == message @pytest.mark.asyncio