使用自定义标记mark【pytest系列 6】

1、标记mark说明

  • pytest支持自定义标记,自定义标记可以把一个web项目划分成多个模块,然后指定模块名执行
  • 再比如:我们可以指定哪些用例是windows下执行的,哪些用例是mac下执行的,然后在代码运行的时候指定mark即可

2、代码演示

# test_01.py
import pytest


@pytest.mark.weibo
def test_weibo():
    print("===正在测试微博===")


@pytest.mark.toutiao
def test_toutiao():
    print("===正在测试头条===")


@pytest.mark.toutiao
def test_toutiao2():
    print("===再次测试头条===")


@pytest.mark.xinlang
def test_skip():
    print("===正在测试新浪===")


def test_no_mark():
    print("===没有打标记===")

3、指定模块运行

  • 执行标记为weibo的用例
    pytest -s -m xinlang test_01.py
    
  • 执行结果
    =========================================================================== test session starts ============================================================================
    platform darwin -- Python 3.9.2, pytest-7.1.1, pluggy-1.0.0
    rootdir: /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnMark, configfile: pytest.ini
    collected 5 items / 4 deselected / 1 selected                                                                                                                              
    
    test_01.py ===正在测试新浪===
    .
    
    ============================================================================= warnings summary =============================================================================
    test_01.py:25
      /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnMark/test_01.py:25: PytestUnknownMarkWarning: Unknown pytest.mark.xinlang - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
        @pytest.mark.xinlang
    
    -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
    ================================================================ 1 passed, 4 deselected, 1 warning in 0.01s ================================================================
    

避免结果中的warming

  • 创建pytest.ini文件,需要与运行的用例同一个目录下,或者根目录作用于全局
  • 加上自定义mark,如下即可
    [pytest]
    markers = 
    	weibo: this is weibo page
    	toutiao: this is toutiao page
    	xinlang: this is xinlang page
    

不想执行标记为xinlang的用例

pytest -s -m "not xinlang" test_01.py
=========================================================================== test session starts ============================================================================
platform darwin -- Python 3.9.2, pytest-7.1.1, pluggy-1.0.0
rootdir: /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnMark, configfile: pytest.ini
collected 5 items / 1 deselected / 4 selected                                                                                                                              

test_01.py ===正在测试微博===
.===正在测试头条===
.===再次测试头条===
.===没有打标记===
.

============================================================================= warnings summary =============================================================================
test_01.py:25
  /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnMark/test_01.py:25: PytestUnknownMarkWarning: Unknown pytest.mark.xinlang - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.xinlang

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================================================ 4 passed, 1 deselected, 1 warning in 0.01s ================================================================

执行多个自定义标记的用例

pytest -s -m "weibo or toutiao" test_01.py
=========================================================================== test session starts ============================================================================
platform darwin -- Python 3.9.2, pytest-7.1.1, pluggy-1.0.0
rootdir: /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnMark, configfile: pytest.ini
collected 5 items / 2 deselected / 3 selected                                                                                                                              

test_01.py ===正在测试微博===
.===正在测试头条===
.===再次测试头条===
.

============================================================================= warnings summary =============================================================================
test_01.py:25
  /Users/chenbinhao/Desktop/Sunnada-work/learnPytest/learnMark/test_01.py:25: PytestUnknownMarkWarning: Unknown pytest.mark.xinlang - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.xinlang

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================================================ 3 passed, 2 deselected, 1 warning in 0.01s ================================================================

参考文章:https://www.cnblogs.com/poloyy/p/12669068.html

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值