Comments (8)
Thx, sure it can be done. May I know what is the use case for mocking more responses than what your application actually sent ? It will help to document the use cases for this feature.
Thx
think about that:
in function a, I send a request named A,
in function b, I send a request named B,
in function c, I send a request named C
then it's an interesting example
function d call func a & b
function e call func b & c
function f call func a & c
you can think about I wanna share all request mock in a big register,so that all function can share the mock, but any function have no chance to send all mocked requests inner the mock request register at all.
from pytest_httpx.
Thx, sure it can be done. May I know what is the use case for mocking more responses than what your application actually sent ? It will help to document the use cases for this feature.
Thx
from pytest_httpx.
Hi @leftvalue, I can put this feature quite fast but I would really like to document the use case before doing so. Can you tell me more?
from pytest_httpx.
Hi @Colin-b let me jump in and provide an example where I find this useful. It is mostly to reduce duplicated code during testing.
Let's say I have a method:
import httpx
def foobar():
with httpx.Client() as client:
response1 = client.get("http://test_url")
if response1.json()['condition']:
raise SomeException
response2 = client.get("http://test_url2")
return response2.json()
right now to test this you would do something like:
def test_foobar_exception(http_mock: HttpMock):
http_mock.add_response(method='GET', url='http://test_url', json={'condition': True})
with pytest.raises(SomeException):
foobar()
def test_foobar(http_mock: HttpMock):
http_mock.add_response(method='GET', url='http://test_url', json={'condition': False})
http_mock.add_response(method='GET', url='http://test_url2', json={'foo': 'bar'})
assert foobar() == {'foo': 'bar'}
It would be nice to be able to mock all the possible responses and then let the developer check if it was called or not. That way you could reuse some code and do something like:
@contextmanager
def mock_requests(http_mock: HttpMock, condition: bool = False):
http_mock.add_response(method='GET', url='http://test_url', json={'condition': condition})
http_mock.add_response(method='GET', url='http://test_url2', json={'foo': 'bar'})
yield
def test_foobar_exception(http_mock: HttpMock):
http_mock.add_response(method='GET', url='http://test_url', json={'condition': True})
with pytest.raises(SomeException):
with mock_requests(http_mock, condition=True):
foobar()
def test_foobar(http_mock: HttpMock):
with mock_requests(http_mock, condition=False):
assert foobar() == {'foo': 'bar'}
It is specially useful when the responses are big/ there are several endpoints.
I hope it helps to illustrate some cases when someone might want not check that all requests were called.
from pytest_httpx.
I assume test_foobar_exception
was actually
def test_foobar_exception(http_mock: HttpMock):
with pytest.raises(SomeException):
with mock_requests(http_mock, condition=True):
foobar()
in your example.
I will add the option then (but you can come up with a PR if you want as I will be quite busy for a few days and I will not have the time to do it right away, even if I will see what I can do today)
What's the preferred option here @pabtecha @leftvalue:
- a pytest marker to disable this check test function by test function
- a fixture disabling this check for the whole test file
from pytest_httpx.
2. fixture
I think both of it is quite great!
from pytest_httpx.
Hello,
Apologies for the delay, I am still under quite a bit of load but I managed some time to implement the fixture (as a start).
Next release will contains the assert_all_responses_were_requested
fixture:
import pytest
@pytest.fixture
def assert_all_responses_were_requested() -> bool:
return False
from pytest_httpx.
Fixed in release 0.5.0
from pytest_httpx.
Related Issues (20)
- pyright with strict typeCheckingMode reports error: Type of "add_response" is partially unknown because of **matchers HOT 1
- Matching the `Authorization` header correctly while another parameter doesn't match causes a very confusing error message HOT 2
- Do you plan on putting back support for Python 3.8? HOT 5
- Allow to record and store requests+responses HOT 1
- Allow to add responses based on a file content
- How can I test if the request is being proxied? HOT 4
- httpx does not read the response if an event_hook raises an exception HOT 2
- How can I send multiple responses matching the same request? HOT 3
- Custom Async Client's Transport is not used HOT 7
- Ability to fail a test case if unexpected requests are received and code is swallowing exceptions HOT 4
- support for python 3.8 HOT 2
- Switch from setup.py to a build library HOT 1
- pytest_httpx breaks httpx timeout logic HOT 2
- Support for pytest 8.x.x HOT 2
- `HTTPXMock` context manager HOT 3
- Introduce decorators to setup httpx_mock
- support query param matcher
- Allow to match on different kind of timeouts
- Can one add mocked responses *after* creating the HTTPX client? HOT 2
- The following responses are mocked but not requested HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytest_httpx.