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
1
vote
1 answer

mock response.elapsed in requests_mock

I'm using requests_mock in my unit tests and would like to mock response.elapsed attribute but have't found proper way to do it. Just found a workaround with adding sleep to text callback. with requests_mock.mock() as m: def…
Alex M981
  • 2,264
  • 14
  • 24
1
vote
1 answer

How to fake my response in pytest requests mock

My function that I am trying to test is returning list of strings: def listForumsIds: response = requests.get(url) forums= response.json().get('forums') forumsIds= [forum['documentId'] for forum in forums] # return like: ['id1',…
stefani
  • 19
  • 1
  • 2
1
vote
2 answers

How to check if the expected data is sent by a post or put request?

I am using requests-mock and I am trying to figure out how to assert that the put request has been called correctly: def funtion_to_be_tested(): requests.put( headers={'X-API-Key': 'api_key'}, url=url, params={'param1':…
Michael_Scharf
  • 33,154
  • 22
  • 74
  • 95
1
vote
1 answer

Capture URL parameter with requests-mock

I'm using requests-mock to mock an external service with a dynamic response. The service's URL is something like http://test/containers/test/1234, where 1234 is the object id I want to dynamically generate. I've tried the regular expression matcher…
tutuca
  • 3,444
  • 6
  • 32
  • 54
0
votes
0 answers

No mock address: GET

I'm trying to create a mock test, but I'm getting the error: No mock address: GET In my view the test is correct, can someone help me find the problem? Test code: def test_get_entity(self, requests_mock: MockerCore): contents = [ …
Leticia Fatima
  • 512
  • 2
  • 4
  • 19
0
votes
1 answer

How to check GET-query-parameters with requests-mock?

I have a function which makes a GET-request with a dict of params. In my unit-tests I want to make sure that the parameters are set correctly. However when I try to use requests-mock to mock the request, I can only check for the URL without the…
Philip Koch
  • 197
  • 1
  • 11
0
votes
0 answers

requests_mock - overlapping URLs

I'm trying to set-up a fixture that will mock some responses for https://github.com/influxdata/influxdb-python and then I want to use the same fixture in some tests to add additional matchers. If the API had suited what I was attempting to do, I…
Filip Allberg
  • 3,941
  • 3
  • 20
  • 37
0
votes
1 answer

Why isn't my environment variable being cleared in FastApi test?

I'm relatively new to FastApi and unit testing in Python. I am testing an api that has a conditional check on whether an environment variable FOO is present. I am trying to clear that environment variable so that when the api gets invoked by the…
Vinyl Warmth
  • 2,226
  • 3
  • 25
  • 50
0
votes
2 answers

Mocked URLs not being forward to handler when using requests_mock in another fixture

I am creating my own fixture to simulate a service endpoint needed in my unit tests. In order to intercept the HTTP requests, I use requests_mock as follows: @pytest.fixture def sparql_endpoint(requests_mock): yield lambda uri, initial_data:…
user2650994
  • 136
  • 5
0
votes
2 answers

Using request_mock to dynamically set response based on request

I am trying to mock a simple POST request that creates a resource from the request body, and returns the resource that was created. For simplicity, let's assume the created resource is exactly as passed in, but given an ID when created. Here is my…
pwerth
  • 210
  • 1
  • 6
  • 14
0
votes
1 answer

How to use request-mock to test custom Authentication Handlers with Requests?

I know I can easily use requests-mock to get calls/custom sessions calls quickly. Now I have a custom authenticator: class SimpsonsAuth(requests.auth.AuthBase): def __call__(self, r): # Implement my authentication …
0
votes
1 answer

provide large responses to python requests_mock

I am using python requests to to access some apis, recently I learned requests_mock for mocking http responses for testing. The responses from api that I am using are quire large adapter.register_uri('GET', 'http://api.gateway/payment/, text='VERY…
Emmanuel Mtali
  • 4,383
  • 3
  • 27
  • 53
0
votes
2 answers

Mocked request doing actual requests call, not mock

I am mocking a get request in my unittest code using requests-mock, but when I run the code during testing, it still tries to hit the actual URL instead of returning the mocked data. This is my code: try: response = requests.get(api_url,…
1
2