news 2026/4/15 6:23:33

pytest文档 - pytest-runtime-yoyo 对用例运行时长断言

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
pytest文档 - pytest-runtime-yoyo 对用例运行时长断言

说明

pytest 执行用例的时候,我们希望对用例的运行时间断言,当用例执行时长大于预期标记此用例失败。
@pytest.mark.runtime(1)运行时长单位是秒
此插件已打包上传到pypi https://pypi.org/project/pytest-runtime-yoyo/1.0.0/

基本示例 test_demo.py

  1. import pytestimport timedef test_a1():

  2. time.sleep(2)@pytest.mark.runtime(1)def test_a2():

  3. time.sleep(2)

运行结果

  1. ======================== short test summary info =====================

  2. FAILED test_demo.py::test_a2

  3. ======================== 1 failed, 1 passed in 4.18s ===============

版本变更记录

v1.0.0 发布时间: 2023/6/12

实现功能

  • 1.用例中使用@pytest.mark.runtime(1)标记用例执行时间

  • 2.pytest.ini 新增runtime全局参数

  • 3.命令行中新增--runtime全局参数

Installation / 安装

最近环境体验

  • Python 3.8+ 版本

  • Pytest 7.2.0+ 新版

pip 安装插件

pip install pytest-runtime-yoyo

Usage / 标记用例运行时长

基于函数的用例中使用@pytest.mark.runtime(1)标记用例执行时间

  1. import pytestimport time# 作者-上海悠悠 微信/QQ交流:283340479# blog地址 https://www.cnblogs.com/yoyoketang/def test_a1():

  2. time.sleep(2)@pytest.mark.runtime(1)def test_a2():

  3. time.sleep(2)

基于测试类的用例, 在测试类上标记runtime,对测试类下的每个用例都会生效

  1. import pytestimport time@pytest.mark.runtime(3)class TestRun:

  2. def test_a3(self):

  3. time.sleep(2) def test_a4(self):

  4. time.sleep(1)

标记模块下全部用例

对整个测试模块下的用例全部标记 runtime

  1. import pytestimport time# 作者-上海悠悠 微信/QQ交流:283340479# blog地址 https://www.cnblogs.com/yoyoketang/pytestmark = pytest.mark.runtime(3)def test_a5():

  2. time.sleep(1)def test_a6():

  3. time.sleep(2)class TestRun:

  4. def test_a7(self):

  5. time.sleep(2) def test_a8(self):

  6. time.sleep(4)

执行结果

  1. collected 4 items

  2. test_x2.py ...F [100%]

  3. ================ FAILURES ===================================

  4. _____________________ TestRun.test_a8 __________________________

  5. ================= short test summary info =====================

  6. FAILED test_x2.py::TestRun::test_a8

  7. ================= 1 failed, 3 passed in 9.15s =======

如果测试模块,测试类和测试用例上都有runtime 标记

  1. import pytestimport time# 作者-上海悠悠 微信/QQ交流:283340479# blog地址 https://www.cnblogs.com/yoyoketang/pytestmark = pytest.mark.runtime(3)def test_a5():

  2. time.sleep(1)def test_a6():

  3. time.sleep(2)@pytest.mark.runtime(1)class TestRun:

  4. def test_a7(self):

  5. time.sleep(2) @pytest.mark.runtime(5)

  6. def test_a8(self):

  7. time.sleep(4)

那么运行的优先级是: 测试用例 runtime > 测试类 runtime > 测试模块 runtime

全局用例配置

对全部用例设置 runtime 标记,可以在pytest.ini中设置全局配置

  1. [pytest]

  2. runtime = 3

也可以在执行 pytest 命令的时候带上命令行参数--runtime

pytest --runtime=3

优先级是: 命令行参数 > pytest.ini 配置

全局配置只针对测试模块,测试类,测试用例没标记 runtime 的用例生效。

如果测试模块,测试类,测试用例有标记 runtime,那么优先级是大于全局配置的。

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/12 10:28:10

基于灵易Smart-ROM智能降阶软件的弯管流体速度场预测

在复杂流体系统的设计中,工程师们常常面临这样一个难题:虽然传统的CFD数值仿真能提供较高的物理精度,但在进行多工况或参数化分析时,每次调整入口边界条件都需要重新运行完整的Fluent仿真。这个过程往往耗时数十分钟甚至数小时&am…

作者头像 李华
网站建设 2026/4/4 0:12:36

亲测好用8个一键生成论文工具,本科生轻松搞定毕业论文!

亲测好用8个一键生成论文工具,本科生轻松搞定毕业论文! AI 工具如何让论文写作变得轻松 在当前的学术环境中,越来越多的本科生开始借助 AI 工具来提升论文写作效率。尤其是随着 AIGC(人工智能生成内容)技术的不断发展&…

作者头像 李华