No project description provided
Project description
The pytest-time plugin extends pytest to control time — the built-in Python module, not the concept within the universe.
Fixtures
Pytest-time offers several fixtures for use in your projects, depending on your particular needs.
Instant Sleep
The instant_sleep fixture is the most basic wrapper and is designed to be used at any scope. It monkeypatches the built-in time module to be chronologically consistent while not actually sleeping when running time.sleep. This includes modifying the behaviour of time.time(), time.monotonic() and their nanosecond counterparts to include the additional delay expected after sleeping.
A basic use of instant_sleep is shown below:
1import time
2import pytest
34
@pytest.mark.parametrize("sleep_time", [1, 10, 100])
5@pytest.mark.usefixtures("instant_sleep")
6def test_instant_sleep(sleep_time):
7start_time = time.time()
8start_monotonic = time.monotonic()
910
time.sleep(sleep_time)
1112
assert time.time() >= start_time + sleep_time
13assert time.monotonic() >= start_monotonic + sleep_time
This code will behave almost identically with and without the instant_sleep fixture in use. To demonstrate, let’s time this file with the fixture enabled…
$ time pytest test_instant_sleep.py
=========== test session starts ===========
platform linux -- Python 3.11.4, pytest-7.3.1, pluggy-1.0.0
rootdir: /home/lengau/Projects/pytest-time
configfile: pyproject.toml
plugins: check-2.1.5, mock-3.10.0, hypothesis-6.78.2, time-0.2.1.dev3+ga0d3b98.d20230624, cov-4.1.0
collected 3 items
test_instant_sleep.py ... [100%]
=========== 3 passed in 0.01s ===========
real 0m0.276s
user 0m0.240s
sys 0m0.025s
and disabled:
$ time pytest test_instant_sleep_no_fixture.py
=========== test session starts ===========
platform linux -- Python 3.11.4, pytest-7.3.1, pluggy-1.0.0
rootdir: /home/lengau/Projects/pytest-time
configfile: pyproject.toml
plugins: check-2.1.5, mock-3.10.0, hypothesis-6.78.2, time-0.2.1.dev3+ga0d3b98.d20230624, cov-4.1.0
collected 3 items
test_instant_sleep_no_fixture.py ... [100%]
=========== 3 passed in 111.01s (0:01:51) ===========
real 1m51.354s
user 0m0.250s
sys 0m0.020s
The sleep is, for practical purposes, essentially instant. And yet, the time module still acts as though the appropriate time has passed.
Recording Time Calls
Pytest-time also provides mock_time, a fixture that wraps several time functions in Mock objects but still runs the real calls. This is useful if you need to ensure that certain calls occurred, etc. The fixture will provide Mock objects for inspection in tests:
1import time
23
def test_mock_time(mock_time):
4start_time = time.time()
5start_monotonic = time.monotonic()
67
time.sleep(1) # Actually sleeps for a second
89
assert time.time() >= start_time + 1
10assert time.monotonic() >= start_monotonic + 1
1112
mock_time.sleep.assert_called_once_with(1)
13assert len(mock_time.time.mock_calls) == 2
14assert len(mock_time.monotonic.mock_calls) == 2
Mocking a Powernap
The two above are combined for you in the mock_instant_sleep fixture. This fixture replaces the relevant time functions as in the `instant_sleep fixture, but also provides mock wrappers around those functions, allowing for recording time.
1import time
23
def test_mock_instant_sleep(mock_instant_sleep):
4start_time = time.time()
5start_monotonic = time.monotonic()
67
time.sleep(86400) # Doesn't sleep
89
assert time.time() >= start_time + 86400
10assert time.monotonic() >= start_monotonic + 86400
1112
mock_instant_sleep.sleep.assert_called_once_with(1)
13assert len(mock_instant_sleep.time.mock_calls) == 2
14assert len(mock_instant_sleep.monotonic.mock_calls) == 2
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest_time-0.3.0.tar.gz
.
File metadata
- Download URL: pytest_time-0.3.0.tar.gz
- Upload date:
- Size: 21.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a926367ca7f8783eb4bc93cf9df730b0b77e3adf3e5d9a5c33d0220e8800ce3 |
|
MD5 | b6d2d699c683fb95d72607751811534f |
|
BLAKE2b-256 | 20df6c088562b2bbca7fd7fbbe69f93a659fd3f101bac989847aa3039ccca9fe |
File details
Details for the file pytest_time-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: pytest_time-0.3.0-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53df2f35f5149efeabe424c9fbad5ff873d01eabdc0540fdc1851cd934812b78 |
|
MD5 | 0df28a40632e390599d8b48e9fad6b32 |
|
BLAKE2b-256 | 61d8a472cc5b0caea538527c019472a9d1c831766cd51786abe0185cc2b87754 |