Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance assertion failure messages #158

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Assertion failure message in case of unmatched responses is now linking documentation on how to deactivate the check.
- Assertion failure message in case of unmatched requests is now linking documentation on how to deactivate the check.

### Fixed
- Assertion failure message in case of unmatched requests at teardown is now describing requests in a more user-friendly way.
- Assertion failure message in case of unmatched requests at teardown is now prefixing requests with `- ` to highlight the fact that this is a list, preventing misapprehension in case only one element exists.
- Assertion failure message in case of unmatched responses at teardown is now prefixing responses with `- ` to highlight the fact that this is a list, preventing misapprehension in case only one element exists.
- TimeoutException message issued in case of unmatched request is now prefixing available responses with `- ` to highlight the fact that this is a list, preventing misapprehension in case only one element exists.

## [0.31.2] - 2024-09-23
### Fixed
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ Refer to [available options](#available-options) for an exhaustive list of optio
```python
import pytest

@pytest.mark.httpx_mock(assert_all_responses_were_requested=True)
@pytest.mark.httpx_mock(assert_all_responses_were_requested=False)
def test_something(httpx_mock):
...
```
Expand Down Expand Up @@ -676,7 +676,7 @@ This option can be useful if you add responses using shared fixtures.
```python
import pytest

@pytest.mark.httpx_mock(assert_all_responses_were_requested=True)
@pytest.mark.httpx_mock(assert_all_responses_were_requested=False)
def test_fewer_requests_than_expected(httpx_mock):
# Even if this response never received a corresponding request, the test will not fail at teardown
httpx_mock.add_response()
Expand Down
7 changes: 1 addition & 6 deletions pytest_httpx/_httpx_internals.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import base64
from typing import (
Union,
Dict,
Tuple,
Optional,
)
from typing import Union, Optional
from collections.abc import Sequence, Iterable, AsyncIterator, Iterator

import httpcore
Expand Down
28 changes: 20 additions & 8 deletions pytest_httpx/_httpx_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def _explain_that_no_response_was_found(

message = f"No response can be found for {RequestDescription(real_transport, request, matchers)}"

matchers_description = "\n".join([str(matcher) for matcher in matchers])
matchers_description = "\n".join([f"- {matcher}" for matcher in matchers])
if matchers_description:
message += f" amongst:\n{matchers_description}"

Expand Down Expand Up @@ -290,17 +290,29 @@ def _assert_options(self, options: HTTPXMockOptions) -> None:
matcher for matcher, _ in self._callbacks if not matcher.nb_calls
]
matchers_description = "\n".join(
[str(matcher) for matcher in callbacks_not_executed]
[f"- {matcher}" for matcher in callbacks_not_executed]
)

assert (
not callbacks_not_executed
), f"The following responses are mocked but not requested:\n{matchers_description}"
assert not callbacks_not_executed, (
"The following responses are mocked but not requested:\n"
f"{matchers_description}\n"
"\n"
"If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-register-more-responses-than-what-will-be-requested"
)

if options.assert_all_requests_were_expected:
assert (
not self._requests_not_matched
), f"The following requests were not expected:\n{self._requests_not_matched}"
requests_description = "\n".join(
[
f"- {request.method} request on {request.url}"
for request in self._requests_not_matched
]
)
assert not self._requests_not_matched, (
f"The following requests were not expected:\n"
f"{requests_description}\n"
"\n"
"If this is on purpose, refer to https://github.com/Colin-b/pytest_httpx/blob/master/README.md#allow-to-not-register-responses-for-every-request"
)


def _unread(response: httpx.Response) -> httpx.Response:
Expand Down
Loading