Comments (8)
Hello @sidharthv96 ,
Can you tell me if I understand your request properly:
You have 80 different HTTP calls performed at least once.
When running your test suite, you want those calls to go through only the first time.
You want every subsequent call to return the exact same response as the first call.
If you want this behavior in production as well you can implement caching to avoid actually performing the subsequent HTTP calls.
Let me know if I understand it properly and we'll see what we can do then.
from pytest_httpx.
Yes, that's what I was trying to achieve.
For production usecase, I'm planning to implement encode/httpx#1693
from pytest_httpx.
If you want to have this kind of behavior in production as well, then you don't need to change anything, cause the caching will itself prevent subsequent calls. If you want your test cases to actually perform the HTTP call, there is already a feature to let it go through
from pytest_httpx.
@Colin-b My requirement is to get a copy of the response into a file (When run for the first time) that I can commit into my repo so it runs fine even in a CI.
We can ignore the production use case for now as the cache hit ratio in prod will be very low as the endpoints will be called with different values.
So I want the calls to go trough only if there is not a matching file present. That result should be then saved into a file, which will be returned on subsequent calls.
from pytest_httpx.
You want to save the content of the first calls so that you can use this content in the test suite later on? Even for the first calls?
from pytest_httpx.
Yes, just like pytest-snapshot saves the output to verify in later tests, but in this case, the input (response from API) will be saved the first time and then reused later.
from pytest_httpx.
pytest-httpx is only aiming at creating test cases. You actually have 2 specifics needs:
- Store data received from your HTTP queries
- Send those data back in test cases
Step 1 is, imo not the responsability of this package.
However step 2 is already handled by this package, once you have your data (in memory, or stored somewhere else) then you can send them back already.
Let say you only differentiate per URL and store in your files the following content+format:
line 1: full URL
line 2: full body
You can subscribe those data to be sent as in the following:
def mock_data_from_file(httpx_mock, file_path: str):
url = None # TODO read line 1
data = None # TODO read line 2
httpx_mock.add_response(url=url, data=data)
And call this function for each one of your files.
Because people might want to match on different indicators (part of the URL, part of the headers, only send a sub-section of the initial body, ...) it would not make sense to add this feature in the package imo.
As for step 1 (saving data), I guess you can subscribe a custom HTTPX handler containing a counter per URL, performing call if the counter is 0 or None and otherwise not doing anything (or returning previously stored value, as you want).
from pytest_httpx.
Hello, I assume my answer provided you enough insight :)
If not feel free to re-open.
from pytest_httpx.
Related Issues (20)
- 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
- How to mock and test request with content passed as byte iterator
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.