pytest + yaml 框架 -37.mark 标记对用例运行时长断言
前言
pytest 执行用例的时候,我们希望对用例的运行时间断言,当用例执行时长大于预期标记此用例失败。
@pytest.mark.runtime(1)
运行时长单位是秒
此插件已打包上传到pypi https://pypi.org/project/pytest-runtime-yoyo/1.0.0/
(备注:从v1.2.4 以后新版本不再公开,新功能内部 VIP 学员可以使用,公开版本仅解决bug, 不提供新功能了。)
环境准备
pip install pytest-yaml-yoyo
此功能在v1.3.1 版本上实现
代码中实现
基本示例 test_demo.py
import pytest
import time
def test_a1():
time.sleep(2)
@pytest.mark.runtime(1)
def test_a2():
time.sleep(2)
运行结果
======================== short test summary info =====================
FAILED test_demo.py::test_a2
======================== 1 failed, 1 passed in 4.18s ===============
在 yaml 用例中实现
在yaml 中添加 runtime
config:
name: yy
test_a1:
name: a11
mark: runtime(1)
sleep: 2
print: "xx111"
test_a2:
name: a22
sleep: 2
print: "xx22"
也可以在config 中添加runtime 对整个yaml 中的用例标记
config:
name: yy
mark: runtime(1)
test_a1:
name: a11
sleep: 2
print: "xx111"
test_a2:
name: a22
sleep: 2
print: "xx22"
如果config 中和用例中都有runtime ,那么用例的runtime优先级>config 中的runtime
全局用例配置
对全部用例设置 runtime 标记,可以在 pytest.ini
中设置全局配置
[pytest]
runtime = 3
也可以在执行 pytest 命令的时候带上命令行参数--runtime
pytest --runtime=3
优先级是: 命令行参数 > pytest.ini 配置
全局配置只针对yaml 中config,测试用例没标记 runtime 的用例生效。
如果yaml 中config,测试用例有标记 runtime,那么优先级是大于全局配置的。
网易云完整视频课程https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338
报名咨询wx:283340479 (已报名的同学学习过程中有问题,都可以协助解决)