Giter Club home page Giter Club logo

Comments (8)

Colin-b avatar Colin-b commented on September 7, 2024

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.

sidharthv96 avatar sidharthv96 commented on September 7, 2024

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

image

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

from pytest_httpx.

Colin-b avatar Colin-b commented on September 7, 2024

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.

sidharthv96 avatar sidharthv96 commented on September 7, 2024

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

Colin-b avatar Colin-b commented on September 7, 2024

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.

sidharthv96 avatar sidharthv96 commented on September 7, 2024

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.

Colin-b avatar Colin-b commented on September 7, 2024

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

from pytest_httpx.

Colin-b avatar Colin-b commented on September 7, 2024

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

from pytest_httpx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.