pytest_httpx icon indicating copy to clipboard operation
pytest_httpx copied to clipboard

Add a way to reset HTTPXMock._requests

Open Normale opened this issue 3 years ago • 1 comments

I started using httpx_mock and tried to mock multiple requests to one resource. Then I expected httpx_mock.reset() to reset the state of HTTPXMock. However, it resets only callbacks, self._requests are still left unchanged.

Generally:

def test_something(httpx_mock: HTTPXMock):
    # custom setup that has to be here, but includes mocked requests
    httpx_mock.reset() 
    for i in range(5):
        httpx.request(...)
    assert len(httpx_mock.get_requests()) == 5 # fails, because of the mocked requests in "setup" part. There is no way to reset list of requests

Is there a reason why .reset() is not resetting requests list?

Normale avatar Aug 22 '22 11:08 Normale

Hi @Normale ,

I guess it wouldnt hurt to reset the requests list as well indeed. I'm trying to see if it would cause a regression for some but I don't think it would.

Feel free to come up with a PR for this, in the meantime you can save the length right after reset (or before), and assert the difference.

Colin-b avatar Aug 23 '22 21:08 Colin-b

Release 0.21.2 fixing this issue is now available on pypi.

Colin-b avatar Nov 03 '22 21:11 Colin-b