Colin-b/pytest_httpx

Automatic snapshot of Requests

Closed this issue · 8 comments

Hi, I have a use-case where I'm querying over 80 different endpoints.
Currently the outputs are verified using pytest-snapshot.
But the tests won't run fast as the HTTP calls have to be made.

What I'm trying to achieve is a system where if I add a new module and run the test, it's HTTP calls will be run once and written to a file. For future runs, that response will be provided by the mock, so we can skip the HTTP calls.

Is such an approach feasible with enough modifications in pytest_httpx, or are there any architectural limitations which would stop me from adding such a feature?

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.

Yes, that's what I was trying to achieve.

image

For production usecase, I'm planning to implement encode/httpx#1693

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

@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.

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?

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.

pytest-httpx is only aiming at creating test cases. You actually have 2 specifics needs:

  1. Store data received from your HTTP queries
  2. 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).

Hello, I assume my answer provided you enough insight :)
If not feel free to re-open.