1、Pytest 中标记用例
class Test01(): def test_szdcs(self): print("深圳多测师") def test_gzdcs(self): print("广州多测师") class Test02(): def test_shdcs(self): print("上海多测师") # 运行命令 pytest -k "not sz" -s # 结果如下 test_demo1.py 广州多测师. 上海多测师.
class Test01(): def test_szdcs(self): print("深圳多测师") def test_gzdcs(self): print("广州多测师") class Test02(): def test_shdcs(self): print("上海多测师") # 运行命令 pytest -k "g and z" -s # 结果如下 test_demo1.py 广州多测师.
class Test01(): def test_szdcs(self): print("深圳多测师") def test_gzdcs(self): print("广州多测师") class Test02(): def test_shdcs(self): print("上海多测师") # 运行命令 pytest -k "sh or sz" -s # 结果如下 test_demo1.py 深圳多测师. 上海多测师.
# 对方法加标签 import pytest class Test01(): def test_szdcs(self): print("深圳多测师") @pytest.mark.tag # 通过装饰器定义标签,自定义标签名称 tag def test_gzdcs(self): print("广州多测师") class Test02(): def test_shdcs(self): print("上海多测师") # 运行命令 pytest test_demo1.py -m tag -s # 结果如下 test_demo1.py 广州多测师.
# 对类加标签 import pytest class Test01(): def test_szdcs(self): print("深圳多测师") def test_gzdcs(self): print("广州多测师") @pytest.mark.tag class Test02(): def test_shdcs(self): print("上海多测师") # 运行命令 pytest test_demo1.py -m tag -s # 结果如下 test_demo1.py 上海多测师.
import pytest # 定义全局标签 mark = pytest.mark.tag class Test01(): def test_szdcs(self): print("深圳多测师") def test_gzdcs(self): print("广州多测师") class Test02(): def test_shdcs(self): print("上海多测师") # 运行命令 pytest -m tag -s # 结果如下 test_demo1.py 深圳多测师. 广州多测师. 上海多测师.
2、Pytest 中 skip 跳过用例
# 在方法上条件装饰器 skip,跳过单条用例 import pytest class Test01(): def test_szdcs(self): print("深圳多测师") @pytest.mark.skip(reason="跳过的用例") def test_gzdcs(self): print("广州多测师") class Test02(): def test_shdcs(self): print("上海多测师") # 运行命令 pytest -sv test_demo1.py # 结果如下 test_demo1.py::Test01::test_szdcs 深圳多测师 PASSED test_demo1.py::Test01::test_gzdcs SKIPPED test_demo1.py::Test02::test_shdcs 上海多测师 PASSED
# 在类上面添加装饰器 skip,跳过整个类中的所有用例 import pytest @pytest.mark.skip(reason="跳过的用例") class Test01(): def test_szdcs(self): print("深圳多测师") def test_gzdcs(self): print("广州多测师") class Test02(): def test_shdcs(self): print("上海多测师") # 运行命令 pytest -sv test_demo1.py # 结果如下 test_demo1.py::Test01::test_szdcs SKIPPED test_demo1.py::Test01::test_gzdcs SKIPPED test_demo1.py::Test02::test_shdcs 上海多测师 PASSED
3、Pytest 中 skipif 跳过用例
import pytest class Test01(): def test_szdcs(self): print("深圳多测师") @pytest.mark.skipif(2>1,reason="条件正确不执行") def test_gzdcs(self): print("广州多测师") class Test02(): def test_shdcs(self): print("上海多测师") # 运行命令 pytest -sv test_demo1.py # 结果如下 test_demo1.py::Test01::test_szdcs 深圳多测师 PASSED test_demo1.py::Test01::test_gzdcs SKIPPED test_demo1.py::Test02::test_shdcs 上海多测师 PASSED
原文:https://www.cnblogs.com/ZhengYing0813/p/13223455.html