Skip to main content

Requests-Asserts

CircleCI Coverage Status PyPI version PyPI - License PyPI - Python Version PyPI - Status

The decorator and context manager to mock and verify HTTP requests made by requests library for unittest.

How to install

pip install requests-asserts

How to use

Make a list of RequestMock elements that contain all information about the expected request and response. Use RequestMock.assert_requests(request_mocks) with the list as a decorator or context manager.

Example

import requests
from unittests import TestCase 

def get_likes_on_post(username, password, post_id):
    access_token = requests.post(
        'http://my.site/login',
        json={'username': username, 'password': password}
    ).json()['access_token']

    likes = requests.get(
        f'http://my.site/posts/{post_id}',
        headers={
            'Accept': 'application/json', 
            'Authorization': f'Bearer {access_token}'
        }
    ).json()['likes']

    return likes

class TestGetLikesOnPost(TestCase):
    @RequestMock.assert_requests([
        RequestMock(
            request_url='http://my.site/login',
            request_json={'username': 'the name', 'password': 'the password'},
            request_method=RequestMock.Method.POST,
            response_json={"access_token": 'the-token'}
        ),
        RequestMock(
            request_url='http://my.site/posts/3',
            request_headers_contains={'Authorization': 'Bearer the-token'},
            response_json={'name': 'The cool story', 'likes': 42}
        )
    ])
    def test_get_likes_on_post(self):
        self.assertEqual(42, get_likes_on_post('the name', 'the password', 3))

Release files for requests-asserts 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for requests-asserts 0.1.3
File Size Uploaded
requests-asserts-0.1.3.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for requests-asserts 0.1.3
File Interpreter ABI Platform
requests_asserts-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 9.4 kB

Release files / requests-asserts-0.1.3.tar.gz

Download URL requests-asserts-0.1.3.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9905605c6c4f23ae36a8882836506bb7fb55c2c43414a7524b68e5a24b9e0841
BLAKE2b-256 checksum
How to use checksums
118a3485cd37e9218d477e505b4b8da8463249b7fa310b206606670f900af632
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.7 CPython/3.6.14 Linux/4.15.0-1106-aws

Release files / requests_asserts-0.1.3-py3-none-any.whl

Download URL requests_asserts-0.1.3-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
24db52fc09f4185e3a95535c6e24f682f071cb253b034534ab3f757506c751d2
BLAKE2b-256 checksum
How to use checksums
20d5c418381bb4ec5fc7745e1ceab9130cc893917e0b553c83f4ac67752d95a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.7 CPython/3.6.14 Linux/4.15.0-1106-aws

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page