网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

学习pytest -m标记,轻松实现精准测试,提升代码质量!

GG网络技术分享 2025-11-10 22:56 2


根据上文的内容,

基本用法

  • 运行全部标记为 need_test 的用例: bash pytest -m need_test
  • 运行全部标记为 slow 的用例: bash pytest -m slow

许多标记组合用

  • 一边运行标记为 need_testslow 的用例: bash pytest -m "need_test and slow"
  • 一边运行标记为 need_testslow 的用例: bash pytest -m "need_test or slow"

标记选择器

  • 排除标记为 need_test 的用例: bash pytest -m "not need_test"
  • 用括号进行麻烦的标记选择: bash pytest -m "need_test and "

全局标记与局部标记

  • pytest.ini 配置文件中设置全局标记: ini markers = need_test: test need to run slow: test is slow
  • 在测试文件中设置局部标记: python import pytest

    @pytest.mark.needtest def testlogin: assert login

    @pytest.mark.slow def testbuygoods: assert buy_goods

    @pytest.mark.dependency @pytest.mark.slow def testaddcart: assert add_cart

  • 在命令行中运行带有特定全局标记的用例: bash pytest -m need_test

局部标记示例

  • conftest.py 文件中定义局部标记: python import pytest

    def pytestcollectionmodifyitems: for item in items: if 'addcart' in item.nodeid: item.addmarker

  • 运行带有局部标记 slow 的用例: bash pytest -m "slow"

用例,搞优良测试效率和质量。

标签:

提交需求或反馈

Demand feedback