前言
在 pytest 中,有时候我们并不需要对所有的用例全部执行。pytest 提供了一种机制:有选择的挑选用例去执行,即标记测试函数。下面详细介绍几种方法给函数标记。
使用pytest.mark在函数上进行标记
标记格式
@表示这是一个装饰器,pytest.mark是 pytest 固定的写法,mark_name可以使用自定义标记和内置标记。如下:
常用内置标记
示例如下:
import pytest
def test_01(): print('hello')
@pytest.mark.skip()def test_add(): print('happy')
def test_02(): print('fun')
if __name__ == '__main__': pytest.main(['-s', '-v','test_02.py'])
复制代码
执行结果如下:
============================= test session starts =============================collecting ... collected 3 items
test_02.py::test_01 PASSED [ 33%]hello
test_02.py::test_add SKIPPED (unconditional skip) [ 66%]Skipped: unconditional skip
tes_t02.py::test_02 PASSED [100%]fun
======================== 2 passed, 1 skipped in 0.02s =========================
复制代码
自定义标记
注册标签名
通过 pytest.ini 配置文件注册标签,格式如下:
[pytest] # 固定的section名
markers= # 固定的option名称
标签名1: 标签名的说明内容。
标签名2
标签名N
复制代码
在测试用例/测试类中给用例打标记(只能使用已注册的标记名)
在测试用例的前面加上:@pytest.mark.已注册标签名。运行时,根据用例标签过滤(-m 标签名)。
示例如下:
import pytest
def test_01(): print('hello')
@pytest.mark.dodef test_add(): print('happy')
def test_02(): print('fun')
复制代码
再创建pytest.ini文件,不需要同目录,内容如下:
[pytest]markers = do: do undo: undo
复制代码
注:Windows 系统中pytest.ini文件不可添加注释,否则将会报错。
命令行执行,命令及输出如下:
pytest -s -v -m do test_02.py======================================================================== test session starts ========================================================================platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.execachedir: .pytest_cacherootdir: D:\pythonProject\my_selenium_project\testcases\pytest, configfile: pytest.inicollected 3 items / 2 deselected / 1 selected
tes_t02.py::test_add happyPASSED
================================================================== 1 passed, 2 deselected in 0.02s ==================================================================
复制代码
conftest.py 中定义钩子函数
示例如下:
def pytest_configure(config): marker_list = [ "smoke: marks test as smoke", "login", "order: 下单场景" ] for marker in marker_list: config.addinivalue_line("markers", marker)
复制代码
注:使用该方法需注意定义的格式,不能轻易修改函数名及入参。
使用示例:
import pytest # 标记测试函数@pytest.mark.smokedef test_01(): print("执行test_01") def test_02(): print("执行test_02") # 标记测试类@pytest.mark.orderclass TestOrder: def test_order(self): print("下单") def test_pay(self): print("支付") # 多个标签 @pytest.mark.smoke@pytest.mark.logindef test_login(): print("登录")
复制代码
给测试类打标签,还有另外一种方式,如下:
# 标记测试类(单个标签)class TestOrder: # 给类中的所有测试方法打上order标签 pytestmark = pytest.mark.order def test_order(self): print("下单") def test_pay(self): print("支付") # 标记测试类(多个标签)class TestOrder: # 给类中的所有测试方法打上order、smoke标签 pytestmark = [pytest.mark.order, pytest.mark.smoke] def test_order(self): print("下单") def test_pay(self): print("支付")
复制代码
执行的时候加上参数-m加标签名即可。
注:在测试模块中直接使用pytest.main()执行当前模块中的被打标签的用例是无效的,我们需要将执行代码分离出来,放在单独的执行模块里面,如放在 run.py 里,代码如下:
# run.py import pytest if __name__ == '__main__': pytest.main(["-s", "-m", "smoke or order"])
复制代码
总结
pytest 的标记功能让我们能够为测试用例添加元数据,使得测试用例能够更灵活地进行分类和选择性地运行。合理地使用标记,可以提高测试的组织性和可维护性,并且让测试执行更具效率。通过标记,你可以更好地管理和执行测试,提高代码质量和稳定性。希望本文能够帮到大家!
评论