pytest 给用例打标签
阅读原文时间:2023年07月10日阅读:3

1.给用例添加自定义标签命令:@pytest.mark.tagname     #tagname是自定义的标签名

import pytest

class TestClass():
@pytest.mark.smoke
def test_one(self):
print("test_one方法执行")
assert 1==1
@pytest.mark.回归测试
def test_two(self):
print("test_two方法执行")
assert 'o' in 'love'

2.根据标签运行测试用例:pytest -m tagname

C:\Users\cale\checkapi\test_cc>pytest test_m1.py -m smoke -s
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test_cc
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items / 1 deselected / 1 selected

test_m1.py test_one方法执行
.

============================================================================================================= warnings summary =============================================================================================================
c:\users\ipharmacare\python37\lib\site-packages\_pytest\mark\structures.py:325
c:\users\ipharmacare\python37\lib\site-packages\_pytest\mark\structures.py:325: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
PytestUnknownMarkWarning,

c:\users\ipharmacare\python37\lib\site-packages\_pytest\mark\structures.py:325
c:\users\ipharmacare\python37\lib\site-packages\_pytest\mark\structures.py:325: PytestUnknownMarkWarning: Unknown pytest.mark.回归测试 - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
=============================================================================================== 1 passed, 1 deselected, 2 warnings in 0.22s ================================================================================================

3.因为自定义的标签没有注册,所以在运行时会出现警告的信息,pytest注册标签有两种方法

(1):注册pytest.ini文件(在当前目录创建pytest.ini文件)

[pytest]
markers=
smoke:冒烟测试 #冒号后面是标签的描述,可不填
two
回归测试 #如果标签是中文,编码需对应进行修改

再看下运行结果:警示信息没有了

**C:\Users\cale\checkapi\test_cc>pytest test_m1.py -s
========================================================================= test session starts =========================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test_cc, inifile: pytest.ini
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items

test_m1.py test_one方法执行
.test_two方法执行
.

========================================================================== 2 passed in 0.13s ==========================================================================**

(2):写钩子到conftest.py(在命令行当前目录下创建conftest.py)

def pytest_configure(config): config.addinivalue_line("markers",'smoke') config.addinivalue_line("markers", '回归测试')

运行结果如下

C:\Users\cale\checkapi\test_cc>pytest test_m1.py -s
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\cale\checkapi\test_cc
plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items

test_m1.py test_one方法执行
.test_two方法执行
.

============================================================================================================ 2 passed in 0.09s =============================================================================================================

4.在一个测试用例上打多个标签

import pytest

@pytest.mark.smoke
@pytest.mark.回归测试
def test_one(self):
print("test_one方法执行")
assert 1==1

5.给测试类打标签

import pytest

#方式一
@pytest.mark.smoke
class TestClass():
def test_one(self):
print("test_one方法执行")
assert 1==1
#方式二
class TestClass():
    pytestmark=[pytest.mark.smoke,pytest.mark.回归测试] #多个标签存在列表里面

def test_one(self): print("test_one方法执行") assert 1==1