【pytest-14】mark使用自定义标记
前言
pytest可以支持自定义标记,自定义标记可以吧一个web项目划分多个模块,然后指定模块名称就行,一个大项目自动化用例时,可以划分为多个模块,也可以使用标记功能,表名哪些是模块1用例,哪些是模块2的,运行代码的时候指定mark名称运行
mark标记
以下用例,标记test_send_http()为webtest
#encoding:utf-8 #@Time:2020/12/9 11:27 #@Author:sunny import pytest @pytest.mark.vx def test_vx(): print("测试微信") @pytest.mark.douyin def test_douyin(): print("测试抖音") @pytest.mark.toutiao def test_toutiao(): print("测试头条") def test_noMark(): print("没有标记的测试") if __name__ == '__main__': pytest.main(["-s","test_mark.py","-m=douyin"])
只运行用douyin标记的测试,运行的时候,加个-m 参数,指定参数值douyin
pytest -v -m douyin
运行结果:

如果不想执行标记douyin的用例,那就用“not douyin”
pytest -v -m “not douyin”
运行结果:

根据节点ID选择测试
可以提供一个或多个节点ID作为位置参数,以仅选择指定的测试。这使得根据模块,类,方法或函数名称选择测试变得容易:
pytest -v test_server.py::TestClass::test_method
示例:
import pytest @pytest.mark.vx def test_vx(): print("测试微信") @pytest.mark.douyin def test_douyin(): print("测试抖音") @pytest.mark.toutiao def test_toutiao(): print("测试头条") def test_noMark(): print("没有标记的测试") class TestClass: def test_method(self): pass if __name__ == '__main__': pytest.main(["-v","test_mark.py::TestClass::test_method"])
运行结果:

当然也能选择运行整个class
$ pytest -v test_server.py::TestClass
也能选择多个节点运行,多个节点中间空格隔开
$ pytest -v test_server.py::TestClass test_server.py::test_send_http


善于跌倒仍喜爱奔跑~

浙公网安备 33010602011771号