Questions tagged [requests-mock]

The requests-mock python library at its core is simply a transport adapter that can be preloaded with responses that are returned if certain URIs are requested. This is particularly useful in unit tests where you want to return known responses from HTTP requests without making actual calls.

https://requests-mock.readthedocs.io/

28 questions
15
votes
1 answer

requests_mock NoMockAddress Exception

I'm currently trying to do unit tests using the requests_mock library in order to patch requests. I've written the following code: def test_general_search(requests_mock): params = {'query': 'testsetset', 'exact': 0, 'max_pages': '1', 'category':…
Lior Dahan
  • 682
  • 2
  • 7
  • 19
10
votes
1 answer

Why is the requests-mock decorator pattern throwing a "fixture 'm' not found" error with pytest?

I'm making an HTTP GET request using the requests library. For example (truncated): requests.get("http://123-fake-api.com") I've written a test following the requests-mock decorator pattern. import requests import…
Ryan Payne
  • 5,249
  • 4
  • 28
  • 69
5
votes
1 answer

`requests_mock` applies to all requests even if they are not set and throws NoMockAddress exception

I found that requests_mock used as a fixture with pytest applies to all requests even if they are not set. I'm not sure if it's a requests_mock/pytest bug or I'm missing something. Eventually, I don't need to mock 'api-b' call but I can't find out…
5
votes
2 answers

requests-mock: how can I match POSTed payload in a mocked endpoint

What I've Done I've written an authentication class for obtaining an application's bearer token from Twitter using the application's API Key and its API key secret as demonstrated in the Twitter developer docs. I've mocked the appropriate endpoint…
mfonism
  • 535
  • 6
  • 15
3
votes
1 answer

How does mock testing REST APIs test the API when the actual API is not called?

I am learning to use mock tests for my FastAPI endpoints. And I am puzzled by this very basic question: how does mock test actually test an API response if the actual HTTP call is not made? I understand that by mimicking the expected response, we…
sjd
  • 1,329
  • 4
  • 28
  • 48
3
votes
1 answer

Python Mock_requests: Can I use wildcards in the url parameter of the Mocker? How to achieve pattern matching of urls when used with pytest?

I have written a test that tries to mock an exception as a side effect of a requests get operation. I am using the requests_mock library as shown in the code listing below: def test_owm_client_raises_error_for_timeout_error(owm_proxy: OWMProxy) ->…
anon_dcs3spp
  • 2,342
  • 2
  • 28
  • 62
3
votes
0 answers

prevent requests-mock from lower casing my post data

When using requests-mock, my querystring data is lowercased: request.post('http://foo.bar', params={'Foo': 'Bar'}) def foo_bar(self, request, context): import pdb; pdb.set_trace() The actual value of request.qs will be {'foo': ['bar']}. How do…
dan-klasson
  • 13,734
  • 14
  • 63
  • 101
2
votes
2 answers

Check query string on the request using requests_mock

How to check if a request mocked by requests_mock added some query parameters to a URL? I have a function func thats do a HTTP POST on the url with some query string on the URL and I want to check if was called with this query string. This is my…
Rodrigo
  • 135
  • 4
  • 45
  • 107
2
votes
1 answer

verify auth in python mock_requests lib

I'm creating a unit test making sure my http client is passing auth token correctly. I'm using requests_mock lib with requests_mock.mock() as m: m.get( "https://my-endpoint", text="{}", ) history = m.request_history[0] assert…
Alex M981
  • 2,264
  • 14
  • 24
2
votes
0 answers

Why is coverage reporting invalid information on some machines when using requests_mock?

The following code path: def _get_token(url, data): print('will show covered') response = requests.post(url, data=data) print('will not show covered but will still print') access_token = response.json().get('access_token') return…
enderland
  • 13,825
  • 17
  • 98
  • 152
1
vote
0 answers

requests_mock multiple responses for single url

I am trying to use the requests_mock as part of my unit test to test API Calls which run in a while loop so in order to end the while loop or to meet the condition of the while loop I need to send different response to my API. My URL remains the…
1
vote
0 answers

SSL Certificate chain mocking

I would like to test couple of methods that involves Python requests library. Cases I'd like to cover are: server unreachable server reachable, but targeted service not available call failed call succeeded Now the tricky part is, I need to cover…
nikoladsp
  • 115
  • 2
  • 8
1
vote
1 answer

Using a wildcard in the middle of a URI endpoint for requests_mock JSON responses

I have some code that I would like to test, it is a fairly vanilla GET request wrapper, but the implementation of it requests data from the API multiple times with different IDs. Adding mock JSON responses for the tests is problematic as there are…
Phil Gunning
  • 212
  • 1
  • 8
1
vote
1 answer

how to test multiple json responses with requests-mock

I'm creating some unit tests using the pytest module and requests-mock for mocking the Response object for requests. I have the following pytest fixture @pytest.fixture(scope="function") def mock_response(requests_mock): test_url =…
Deena
  • 343
  • 1
  • 11
1
vote
3 answers

How to mock multiple urls in request mock

I have a method which is calling two different end points and validating there response. def foo_bar: status_1 = requests.post( "http://myapi/test/status1", {}, headers=headers) status_2 = requests.post( …
Sai prateek
  • 11,842
  • 9
  • 51
  • 66
1
2