未提供项目描述
项目描述
pytest-time插件扩展了pytest来控制时间——内置的Python模块,而不是宇宙中的概念。
固定装置
Pytest-time提供了一些固定装置供您在项目中使用,具体取决于您的特定需求。
瞬时睡眠
instant_sleep固定装置是最基本的包装,旨在在任何范围内使用。它通过monkeypatches内置的time模块以使时间一致,但在运行time.sleep时实际上并不睡眠。这包括修改time.time()、time.monotonic()以及它们的纳秒对应物,以包含在睡眠后预期的额外延迟。
以下是一个基本使用instant_sleep的示例
import time
import pytest
@pytest.mark.parametrize("sleep_time", [1, 10, 100])
@pytest.mark.usefixtures("instant_sleep")
def test_instant_sleep(sleep_time):
start_time = time.time()
start_monotonic = time.monotonic()
time.sleep(sleep_time)
assert time.time() >= start_time + sleep_time
assert time.monotonic() >= start_monotonic + sleep_time
此代码在使用固定装置前后几乎具有相同的行为。为了演示,让我们启用固定装置并计时此文件...
$ 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
并禁用
$ 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
睡眠对于实际应用来说几乎是一瞬间的事情。然而,time模块仍然表现得好像已经过去了相应的时间。
记录时间调用
Pytest-time还提供了mock_time,这是一个包装了多个time函数的Mock对象的.fixture,但仍运行真实的调用。这在你需要确保某些调用发生等情况时非常有用。该.fixture将为测试提供用于检查的Mock对象。
import time
def test_mock_time(mock_time):
start_time = time.time()
start_monotonic = time.monotonic()
time.sleep(1) # Actually sleeps for a second
assert time.time() >= start_time + 1
assert time.monotonic() >= start_monotonic + 1
mock_time.sleep.assert_called_once_with(1)
assert len(mock_time.time.mock_calls) == 2
assert len(mock_time.monotonic.mock_calls) == 2
模拟Powernap
上面的两个 fixture 在mock_instant_sleep中为你组合在一起。这个.fixture替换了和instant_sleep fixture中一样的相关time函数,但同时也提供了这些函数的mock包装,允许记录时间。
import time
def test_mock_instant_sleep(mock_instant_sleep):
start_time = time.time()
start_monotonic = time.monotonic()
time.sleep(86400) # Doesn't sleep
assert time.time() >= start_time + 86400
assert time.monotonic() >= start_monotonic + 86400
mock_instant_sleep.sleep.assert_called_once_with(86400)
assert len(mock_instant_sleep.time.mock_calls) == 2
assert len(mock_instant_sleep.monotonic.mock_calls) == 2
项目详情
下载文件
下载适合您平台文件的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分发
pytest_time-0.3.2.tar.gz (77.1 kB 查看哈希值)
构建分发
pytest_time-0.3.2-py3-none-any.whl (9.3 kB 查看哈希值)
关闭
pytest_time-0.3.2.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 71baf1a0e516c0aedcc870512592d3a19393a0e4834062a583103085320e0880 |
|
MD5 | 4fa3406a9ea195e1f7a47ab6d451f4e0 |
|
BLAKE2b-256 | b05b6abba0b15f134bd9e7c769dc938ac829b88007fdf9c7fa8cd21be0e8d012 |
关闭
pytest_time-0.3.2-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | fee4c01400bb8ffc76e1b5c654d20db7a662d78921f1587e309964286fdf5cf7 |
|
MD5 | 64b71470a3cacb60920677d617237d7c |
|
BLAKE2b-256 | e120168a6a1ca715e6243dfaa39a5bc01a92c44a6a6300d1d4d28714b6c8fecd |