我有一些 py.test 测试有多个依赖和参数化的装置,我想测量每个装置所花费的时间。但是,在带有--durations它的日志中只显示setup了实际测试的时间,但没有给我详细说明每个固定装置花费的时间。
以下是如何执行此操作的具体示例:
import logging
import time
import pytest
logger = logging.getLogger(__name__)
@pytest.hookimpl(hookwrapper=True)
def pytest_fixture_setup(fixturedef, request):
start = time.time()
yield
end = time.time()
logger.info(
'pytest_fixture_setup'
f', request={request}'
f', time={end - start}'
)
Run Code Online (Sandbox Code Playgroud)
输出类似于:
2018-10-29 20:43:18,783 - INFO pytest_fixture_setup, request=<SubRequest 'some_data_source' for <Function 'test_ruleset_customer_to_campaign'>>, time=3.4723987579345703
Run Code Online (Sandbox Code Playgroud)
魔术是hookwrapper:
pytest 插件可以实现钩子包装器,它包装其他钩子实现的执行。钩子包装器是一个生成器函数,它只产生一次。当 pytest 调用钩子时,它首先执行钩子包装器并传递与常规钩子相同的参数。
我遇到的一个相当重要的问题是conftest.py必须在项目的根文件夹中才能找到pytest_fixture_setup钩子。
| 归档时间: |
|
| 查看次数: |
1505 次 |
| 最近记录: |