51

I'm writing some unit test code and I would like to mock requests module that is being used inside my function:

import requests

def get_employee(id):
    resp = requests.get(f'{__BASE_URL}/employee/{id}')
    if resp.status_code == 404:
        return None

    return resp.json()

I've tried to mock it using this code:

def test_get_employee(mocker):
    get_request_mock = mocker.patch.object(get_employee, "resp")
    print(get_request_mock)
    get_request_mock.status_code = 200
    get_request_mock.json.return_value = {'name': 'awesome-mock'}
    resp = get_employee('random-id')
    assert resp == {'name': 'awesome-mock'}

How can I mock requests using mocker? Is possible?

2 Answers2

51

You can use requests-mock (PyPI), there is a fixture for a pytest usage.

For your example:

from correct.package import __BASE_URL
from requests import HTTPError


def test_get_employee(requests_mock):
    test_id = 'random-id'
    requests_mock.get(f'{__BASE_URL}/employee/{test_id}', json= {'name': 'awesome-mock'})
    resp = get_employee('random-id')
    assert resp == {'name': 'awesome-mock'}

def test_absent_employee(requests_mock):
    test_id = 'does_not_exist'
    requests_mock.get(f'{__BASE_URL}/employee/{test_id}', status_code=404)
    with pytest.raises(HTTPError):
        resp = get_employee(test_id)
gimboland
  • 1,926
  • 2
  • 19
  • 28
ndclt
  • 2,590
  • 2
  • 12
  • 26
  • 2
    But how do I mock line `requests.get(f'{__BASE_URL}/employee/{id}')` from function `get_employee(id)`? –  Aug 26 '19 at 18:50
  • Is possible to return 404? –  Aug 26 '19 at 18:53
  • Yes. Just add a `status=404` as argument of the `get` function. – ndclt Aug 26 '19 at 18:54
  • What the hell is correct.package? Can't find it anywhere. – Zizzipupp Mar 05 '21 at 10:30
  • 2
    @Zizzipupp I think `from correct.package import __BASE_URL` is meant as your project's package where the base URL for the resource you're trying to mock is found. I assume it could be found somewhere in the configuration module. – Cuthbeorht Apr 29 '21 at 19:01
-9

This may help

from unittest import TestCase

import requests
import requests_mock


class TestHTTPRequest(TestCase):
    def test_context_manager(self):
        with requests_mock.Mocker() as mock_request:
            mock_request.get("http://123-fake-api.com", text="Hello!")
            response = requests.get("http://123-fake-api.com")

        assert response.text == "Hello!"
DimoMohit
  • 735
  • 4
  • 17