File: test_requests_mock_disable_cache.py

package info (click to toggle)
python-requests-cache 1.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,024 kB
  • sloc: python: 7,029; makefile: 4
file content (26 lines) | stat: -rw-r--r-- 733 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""Example of using requests-cache with the requests-mock library"""

import pytest
import requests

import requests_cache


@pytest.fixture(scope='function')
def requests_cache_mock(requests_mock):
    with requests_cache.disabled():
        yield requests_mock


def test_requests_cache_mock(requests_cache_mock):
    """Within this test function, requests will be mocked and not cached"""
    url = 'https://example.com'
    requests_cache_mock.get(url, text='Mock response!')

    # Make sure the mocker is used
    response_1 = requests.get(url)
    assert response_1.text == 'Mock response!'

    # Make sure the cache is not used
    response_2 = requests.get(url)
    assert getattr(response_2, 'from_cache', False) is False