diff --git a/CHANGELOG.md b/CHANGELOG.md index dcbbf35..e4dfa72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index d119519..6b37a36 100644 --- a/README.md +++ b/README.md @@ -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): ... ``` @@ -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() diff --git a/pytest_httpx/_httpx_internals.py b/pytest_httpx/_httpx_internals.py index 66199ff..477d2bc 100644 --- a/pytest_httpx/_httpx_internals.py +++ b/pytest_httpx/_httpx_internals.py @@ -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 diff --git a/pytest_httpx/_httpx_mock.py b/pytest_httpx/_httpx_mock.py index 3379e2b..74e81cc 100644 --- a/pytest_httpx/_httpx_mock.py +++ b/pytest_httpx/_httpx_mock.py @@ -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}" @@ -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: diff --git a/tests/test_httpx_async.py b/tests/test_httpx_async.py index ee3cc61..b05576a 100644 --- a/tests/test_httpx_async.py +++ b/tests/test_httpx_async.py @@ -75,7 +75,7 @@ async def test_url_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for GET request on https://test_url2 amongst: -Match all requests on https://test_url""" +- Match all requests on https://test_url""" ) @@ -93,7 +93,7 @@ async def test_url_query_string_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for GET request on https://test_url?a=2&a=1 amongst: -Match all requests on https://test_url?a=1&a=2""" +- Match all requests on https://test_url?a=1&a=2""" ) @@ -122,7 +122,7 @@ async def test_method_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for POST request on https://test_url amongst: -Match GET requests""" +- Match GET requests""" ) @@ -1129,7 +1129,7 @@ async def test_multi_value_headers_not_matching_single_value_issued( assert ( str(exception_info.value) == """No response can be found for GET request on https://test_url with {'my-custom-header': 'value1, value2'} headers amongst: -Match all requests with {'my-custom-header': 'value1'} headers""" +- Match all requests with {'my-custom-header': 'value1'} headers""" ) @@ -1154,7 +1154,7 @@ async def test_multi_value_headers_not_matching_multi_value_issued( assert ( str(exception_info.value) == """No response can be found for GET request on https://test_url with {'my-custom-header': 'value1, value3'} headers amongst: -Match all requests with {'my-custom-header': 'value1, value2'} headers""" +- Match all requests with {'my-custom-header': 'value1, value2'} headers""" ) @@ -1173,7 +1173,7 @@ async def test_headers_matching_respect_case(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == f"""No response can be found for GET request on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}'}} headers amongst: -Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}'}} headers""" +- Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}'}} headers""" ) @@ -1196,7 +1196,7 @@ async def test_headers_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == f"""No response can be found for GET request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers amongst: -Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2', 'Host2': 'test_url'}} headers""" +- Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2', 'Host2': 'test_url'}} headers""" ) @@ -1218,7 +1218,7 @@ async def test_url_not_matching_upper_case_headers_matching( assert ( str(exception_info.value) == """No response can be found for GET request on https://test_url with {'MyHeader': 'Something'} headers amongst: -Match GET requests on https://test_url?q=b with {'MyHeader': 'Something'} headers""" +- Match GET requests on https://test_url?q=b with {'MyHeader': 'Something'} headers""" ) @@ -1253,7 +1253,7 @@ async def test_proxy_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for GET request on http://test_url with http://my_test_proxy/ proxy URL amongst: -Match all requests with http://my_test_proxy proxy URL""" +- Match all requests with http://my_test_proxy proxy URL""" ) @@ -1270,7 +1270,7 @@ async def test_proxy_not_existing(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for GET request on http://test_url with no proxy URL amongst: -Match all requests with http://my_test_proxy proxy URL""" +- Match all requests with http://my_test_proxy proxy URL""" ) @@ -1351,7 +1351,7 @@ async def test_content_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for POST request on https://test_url with b'This is the body2' body amongst: -Match all requests with b'This is the body' body""" +- Match all requests with b'This is the body' body""" ) @@ -1386,7 +1386,7 @@ async def test_json_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for POST request on https://test_url with b'{"c": 3, "b": 2, "a": 1}' body amongst: -Match all requests with {'a': 1, 'b': 2} json body""" +- Match all requests with {'a': 1, 'b': 2} json body""" ) @@ -1406,7 +1406,7 @@ async def test_headers_and_json_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for POST request on https://test_url with {} headers and b'{"c": 3, "b": 2, "a": 1}' body amongst: -Match all requests with {'foo': 'bar'} headers and {'a': 1, 'b': 2} json body""" +- Match all requests with {'foo': 'bar'} headers and {'a': 1, 'b': 2} json body""" ) @@ -1423,7 +1423,7 @@ async def test_match_json_invalid_json(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for POST request on https://test_url with b'foobar' body amongst: -Match all requests with {'a': 1, 'b': 2} json body""" +- Match all requests with {'a': 1, 'b': 2} json body""" ) @@ -1458,7 +1458,7 @@ async def test_headers_not_matching_and_content_matching(httpx_mock: HTTPXMock) assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" +- Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" ) @@ -1481,7 +1481,7 @@ async def test_headers_matching_and_content_not_matching(httpx_mock: HTTPXMock) assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" +- Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" ) @@ -1504,7 +1504,7 @@ async def test_headers_and_content_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" +- Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" ) @@ -1543,7 +1543,7 @@ async def test_headers_not_matching_and_url_and_content_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" +- Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" ) @@ -1569,7 +1569,7 @@ async def test_url_and_headers_not_matching_and_content_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" +- Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" ) @@ -1595,7 +1595,7 @@ async def test_url_and_headers_matching_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" +- Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" ) @@ -1621,7 +1621,7 @@ async def test_headers_matching_and_url_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" +- Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" ) @@ -1647,7 +1647,7 @@ async def test_url_matching_and_headers_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" +- Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" ) @@ -1671,7 +1671,7 @@ async def test_url_and_headers_and_content_not_matching(httpx_mock: HTTPXMock) - assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" +- Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" ) @@ -1714,7 +1714,7 @@ async def test_headers_not_matching_and_method_and_url_and_content_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" +- Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" ) @@ -1741,7 +1741,7 @@ async def test_url_and_headers_not_matching_and_method_and_content_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" +- Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" ) @@ -1768,7 +1768,7 @@ async def test_method_and_url_and_headers_matching_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" +- Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" ) @@ -1795,7 +1795,7 @@ async def test_method_and_headers_matching_and_url_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" +- Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" ) @@ -1822,7 +1822,7 @@ async def test_method_and_url_matching_and_headers_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" +- Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" ) @@ -1849,7 +1849,7 @@ async def test_method_matching_and_url_and_headers_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" +- Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" ) @@ -1876,7 +1876,7 @@ async def test_method_and_url_and_headers_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match PUT requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" +- Match PUT requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" ) diff --git a/tests/test_httpx_sync.py b/tests/test_httpx_sync.py index a4c6047..f37bdb8 100644 --- a/tests/test_httpx_sync.py +++ b/tests/test_httpx_sync.py @@ -67,7 +67,7 @@ def test_url_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for GET request on https://test_url2 amongst: -Match all requests on https://test_url""" +- Match all requests on https://test_url""" ) @@ -84,7 +84,7 @@ def test_url_query_string_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for GET request on https://test_url?a=2&a=1 amongst: -Match all requests on https://test_url?a=1&a=2""" +- Match all requests on https://test_url?a=1&a=2""" ) @@ -111,7 +111,7 @@ def test_method_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for POST request on https://test_url amongst: -Match GET requests""" +- Match GET requests""" ) @@ -157,7 +157,7 @@ def test_url_not_matching_upper_case_headers_matching(httpx_mock: HTTPXMock) -> assert ( str(exception_info.value) == """No response can be found for GET request on https://test_url with {'MyHeader': 'Something'} headers amongst: -Match GET requests on https://test_url?q=b with {'MyHeader': 'Something'} headers""" +- Match GET requests on https://test_url?q=b with {'MyHeader': 'Something'} headers""" ) @@ -902,7 +902,7 @@ def test_multi_value_headers_not_matching_single_value_issued( assert ( str(exception_info.value) == """No response can be found for GET request on https://test_url with {'my-custom-header': 'value1, value2'} headers amongst: -Match all requests with {'my-custom-header': 'value1'} headers""" +- Match all requests with {'my-custom-header': 'value1'} headers""" ) @@ -926,7 +926,7 @@ def test_multi_value_headers_not_matching_multi_value_issued( assert ( str(exception_info.value) == """No response can be found for GET request on https://test_url with {'my-custom-header': 'value1, value3'} headers amongst: -Match all requests with {'my-custom-header': 'value1, value2'} headers""" +- Match all requests with {'my-custom-header': 'value1, value2'} headers""" ) @@ -944,7 +944,7 @@ def test_headers_matching_respect_case(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == f"""No response can be found for GET request on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}'}} headers amongst: -Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}'}} headers""" +- Match all requests with {{'user-agent': 'python-httpx/{httpx.__version__}'}} headers""" ) @@ -966,7 +966,7 @@ def test_headers_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == f"""No response can be found for GET request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers amongst: -Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2', 'Host2': 'test_url'}} headers""" +- Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2', 'Host2': 'test_url'}} headers""" ) @@ -998,7 +998,7 @@ def test_proxy_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for GET request on http://test_url with http://my_test_proxy/ proxy URL amongst: -Match all requests with http://my_test_proxy proxy URL""" +- Match all requests with http://my_test_proxy proxy URL""" ) @@ -1014,7 +1014,7 @@ def test_proxy_not_existing(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for GET request on http://test_url with no proxy URL amongst: -Match all requests with http://my_test_proxy proxy URL""" +- Match all requests with http://my_test_proxy proxy URL""" ) @@ -1086,7 +1086,7 @@ def test_content_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for POST request on https://test_url with b'This is the body2' body amongst: -Match all requests with b'This is the body' body""" +- Match all requests with b'This is the body' body""" ) @@ -1128,7 +1128,7 @@ def test_json_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for POST request on https://test_url with b'{"c": 3, "b": 2, "a": 1}' body amongst: -Match all requests with {'a': 1, 'b': 2} json body""" +- Match all requests with {'a': 1, 'b': 2} json body""" ) @@ -1147,7 +1147,7 @@ def test_headers_and_json_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for POST request on https://test_url with {} headers and b'{"c": 3, "b": 2, "a": 1}' body amongst: -Match all requests with {'foo': 'bar'} headers and {'a': 1, 'b': 2} json body""" +- Match all requests with {'foo': 'bar'} headers and {'a': 1, 'b': 2} json body""" ) @@ -1163,7 +1163,7 @@ def test_match_json_invalid_json(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == """No response can be found for POST request on https://test_url with b'foobar' body amongst: -Match all requests with {'a': 1, 'b': 2} json body""" +- Match all requests with {'a': 1, 'b': 2} json body""" ) @@ -1196,7 +1196,7 @@ def test_headers_not_matching_and_content_matching(httpx_mock: HTTPXMock) -> Non assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" +- Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" ) @@ -1218,7 +1218,7 @@ def test_headers_matching_and_content_not_matching(httpx_mock: HTTPXMock) -> Non assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" +- Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" ) @@ -1240,7 +1240,7 @@ def test_headers_and_content_not_matching(httpx_mock: HTTPXMock) -> None: assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" +- Match all requests with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" ) @@ -1277,7 +1277,7 @@ def test_headers_not_matching_and_url_and_content_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" +- Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" ) @@ -1302,7 +1302,7 @@ def test_url_and_headers_not_matching_and_content_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" +- Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" ) @@ -1327,7 +1327,7 @@ def test_url_and_headers_matching_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" +- Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" ) @@ -1352,7 +1352,7 @@ def test_headers_matching_and_url_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" +- Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" ) @@ -1377,7 +1377,7 @@ def test_url_matching_and_headers_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" +- Match all requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" ) @@ -1400,7 +1400,7 @@ def test_url_and_headers_and_content_not_matching(httpx_mock: HTTPXMock) -> None assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" +- Match all requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" ) @@ -1439,7 +1439,7 @@ def test_headers_not_matching_and_method_and_url_and_content_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" +- Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" ) @@ -1465,7 +1465,7 @@ def test_url_and_headers_not_matching_and_method_and_content_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" +- Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body' body""" ) @@ -1491,7 +1491,7 @@ def test_method_and_url_and_headers_matching_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" +- Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" ) @@ -1517,7 +1517,7 @@ def test_method_and_headers_matching_and_url_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" +- Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url'}} headers and b'This is the body2' body""" ) @@ -1543,7 +1543,7 @@ def test_method_and_url_matching_and_headers_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" +- Match POST requests on https://test_url with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" ) @@ -1569,7 +1569,7 @@ def test_method_matching_and_url_and_headers_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" +- Match POST requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" ) @@ -1595,7 +1595,7 @@ def test_method_and_url_and_headers_and_content_not_matching( assert ( str(exception_info.value) == f"""No response can be found for POST request on https://test_url with {{'Host': 'test_url', 'User-Agent': 'python-httpx/{httpx.__version__}'}} headers and b'This is the body' body amongst: -Match PUT requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" +- Match PUT requests on https://test_url2 with {{'User-Agent': 'python-httpx/{httpx.__version__}', 'Host': 'test_url2'}} headers and b'This is the body2' body""" ) diff --git a/tests/test_plugin.py b/tests/test_plugin.py index a3149a0..d0b06e9 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -36,8 +36,11 @@ def test_httpx_mock_unused_response(httpx_mock): result.stdout.fnmatch_lines( [ "*AssertionError: The following responses are mocked but not requested:", - "*Match all requests", - ] + "* - Match all requests", + "* ", + "* 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", + ], + consecutive=True, ) @@ -78,8 +81,11 @@ def unused(*args, **kwargs): result.stdout.fnmatch_lines( [ "*AssertionError: The following responses are mocked but not requested:", - "*Match all requests", - ] + "* - Match all requests", + "* ", + "* 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", + ], + consecutive=True, ) @@ -127,8 +133,11 @@ def test_httpx_mock_unexpected_request(httpx_mock): result.stdout.fnmatch_lines( [ "*AssertionError: The following requests were not expected:", - "*[]", - ] + "* - GET request on https://foo.tld", + "* ", + "* 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", + ], + consecutive=True, )