Skip to main content

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:

 1 import time
 2 import pytest
 3 
 4 @pytest.mark.parametrize("sleep_time", [1, 10, 100])
 5 @pytest.mark.usefixtures("instant_sleep")
 6 def test_instant_sleep(sleep_time):
 7     start_time = time.time()
 8     start_monotonic = time.monotonic()
 9 
10     time.sleep(sleep_time)
11 
12     assert time.time() >= start_time + sleep_time
13     assert 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:

 1 import time
 2 
 3 def test_mock_time(mock_time):
 4     start_time = time.time()
 5     start_monotonic = time.monotonic()
 6 
 7     time.sleep(1)  # Actually sleeps for a second
 8 
 9     assert time.time() >= start_time + 1
10     assert time.monotonic() >= start_monotonic + 1
11 
12     mock_time.sleep.assert_called_once_with(1)
13     assert len(mock_time.time.mock_calls) == 2
14     assert 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.

 1 import time
 2 
 3 def test_mock_instant_sleep(mock_instant_sleep):
 4     start_time = time.time()
 5     start_monotonic = time.monotonic()
 6 
 7     time.sleep(86400)  # Doesn't sleep
 8 
 9     assert time.time() >= start_time + 86400
10     assert time.monotonic() >= start_monotonic + 86400
11 
12     mock_instant_sleep.sleep.assert_called_once_with(1)
13     assert len(mock_instant_sleep.time.mock_calls) == 2
14     assert 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

pytest_time-0.3.0.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

pytest_time-0.3.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

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

Hashes for pytest_time-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2a926367ca7f8783eb4bc93cf9df730b0b77e3adf3e5d9a5c33d0220e8800ce3
MD5 b6d2d699c683fb95d72607751811534f
BLAKE2b-256 20df6c088562b2bbca7fd7fbbe69f93a659fd3f101bac989847aa3039ccca9fe

See more details on using hashes here.

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

Hashes for pytest_time-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 53df2f35f5149efeabe424c9fbad5ff873d01eabdc0540fdc1851cd934812b78
MD5 0df28a40632e390599d8b48e9fad6b32
BLAKE2b-256 61d8a472cc5b0caea538527c019472a9d1c831766cd51786abe0185cc2b87754

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page