@pytest.mark.name_of_the_mark,注册标记使用方法

使用方法

@pytest.mark.name_of_the_mark,其中name_of_the_mark是自定义标记的名字,举个例子,下面定义了3个,对方法、函数都可以进行标记

# @File : test_001.py
import pytest

@pytest.mark.testa
class Testaddbook:
    @pytest.mark.addbook01
    def test_01(self):
        print('打印01')

    @pytest.mark.addbook02
    def test_02(self):
        print('打印02')

1、在Terminal终端输入命令:pytest -sq test_001.py    ,执行全部

 2、在Terminal终端输入命令:pytest -sq test_001.py -m=addbook02   ,执行标记名=addbook02的

3、 在Terminal终端输入命令:pytest -sq test_001.py::Testaddbook::test_01  ,用两个冒号分隔表示需要执行的类名或函数,按路径写,执行test_01

配置文件

pytest.ini文件需要注册自定义标记

[pytest]
markers =
    testa
    addbook01
    addbook02

如果不注册,就会出现警告:PytestUnknownMarkWarning: Unknown pytest.mark.addbook02 - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html

如果需要检查是否所有标记都已经注册了,那么可以增加--strict 来加强验证,这时任何未知标记都会报错,如修改pytest.int,少了一个addbook02

[pytest]
markers =
    testa
    addbook01

 1、在Terminal终端输入命令:pytest -sq test_001.py --strict -m=addbook02

原文地址:https://www.cnblogs.com/docstrange/p/14784664.html