Giter Club home page Giter Club logo

Comments (8)

leftvalue avatar leftvalue commented on September 7, 2024 1

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.

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

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.

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

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.

pabtecha avatar pabtecha commented on September 7, 2024

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.

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

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:

  1. a pytest marker to disable this check test function by test function
  2. a fixture disabling this check for the whole test file

from pytest_httpx.

leftvalue avatar leftvalue commented on September 7, 2024

2. fixture

I think both of it is quite great!

from pytest_httpx.

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

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.

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

Fixed in release 0.5.0

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.