[PYTHON] Mock HTTP requests via the requests module in Responses

Reponses is a library that mock the requests module that performs HTTP communication. Made by Dropbox that seems to be reliable. The license is APL 2.0.

class APITestCase(TestCase):
    @responses.activate
    def test_api(self):
        import api

        # setup
        responses.add(
            responses.GET,
            'https://example.com/api/v3/users',
            status=200,
            body="[{'user': {'id': 1, 'username': 'test'}}]",
            content_type="application/json",
        )

        # test
        api.get_users()
        assert responses.calls[0].request.method == 'GET'
        assert responses.calls[0].request.url == 'https://example.com/api/v3/users'

There is also httpretty because it is similar, but Responses was more suitable for me. I will try it for a while.

Recommended Posts

Mock HTTP requests via the requests module in Responses
Put the module in the Pepper app
Master the weakref module in Python
Understand the arguments required for the HTTP request method of the Requests module
Create a record with attachments in KINTONE using the Python requests module
Install the Python module in any directory
Try adding an external module to pepper. For the time being, in requests.
I want to see a list of WebDAV files in the Requests module
Launch an HTTP server in the current directory
How to run the Ansible module added in Ansible Tower
[python] Get the list of classes defined in the module
Load the module with the same name in another location
Write data to KINTONE using the Python requests module
I want to write in Python! (3) Utilize the mock
[Note] I can't call the installed module in jupyter
Get the URL of the HTTP redirect destination in Python