参考来源:https://www.jianshu.com/p/7df6d781f100
pytest插件汇总:http://plugincompat.herokuapp.com/
以下是常用插件介绍
1、用例依赖:pytest-dependency
官方示例:https://pytest-dependency.readthedocs.io/en/latest/usage.html#basic-usage
import pytest #在类下面实现依赖关系,第一种方式 class TestClass(object): @pytest.mark.dependency() # @pytest.mark.xfail(reason="deliberate fail") def test_a(self): assert True @pytest.mark.dependency() def test_b(self): pass @pytest.mark.dependency(depends=["TestClass::test_a"]) def test_c(self): pass @pytest.mark.dependency(depends=["TestClass::test_b"]) def test_d(self): pass @pytest.mark.dependency(depends=["TestClass::test_b", "TestClass::test_c"]) def test_e(self): pass #在类下面实现依赖关系,第二种方式 class TestClassNamed(object): @pytest.mark.dependency(name="a") #@pytest.mark.xfail(reason="deliberate fail") def test_a(self): assert False @pytest.mark.dependency(name="b", depends=["a"]) def test_b(self): pass @pytest.mark.dependency(name="c") def test_c(self): pass @pytest.mark.dependency(name="d", depends=["c"]) def test_d(self): pass @pytest.mark.dependency(name="e", depends=["b", "c"]) def test_e(self): pass
#不在类下面,第一种 import pytest @pytest.mark.dependency() @pytest.mark.xfail(reason="deliberate fail") def test_a(): assert False @pytest.mark.dependency() def test_b(): pass @pytest.mark.dependency(depends=["test_a"]) def test_c(): pass @pytest.mark.dependency(depends=["test_b"]) def test_d(): pass @pytest.mark.dependency(depends=["test_b", "test_c"]) def test_e(): pass #不在类下面,第二种 import pytest @pytest.mark.dependency(name="a") @pytest.mark.xfail(reason="deliberate fail") def test_a(): assert False @pytest.mark.dependency(name="b") def test_b(): pass @pytest.mark.dependency(name="c", depends=["a"]) def test_c(): pass @pytest.mark.dependency(name="d", depends=["b"]) def test_d(): pass @pytest.mark.dependency(name="e", depends=["b", "c"]) def test_e(): pass
2、多重校验:pytest-assume
def test_a(self): pytest.assume(3 == 3) pytest.assume(5 == 3) pytest.assume(4 == 5)
即使第二个断言失败了,第三个断言还是会继续执行
3、执行顺序:pytest-ordering(还可以对类进行标记,尽量但不要混着用)
#先执行test_b再执行test_a @pytest.mark.run(order=2) def test_a(self): pass @pytest.mark.run(order=1) def test_b(self): pass
4、失败重跑:pytest-rerunfailures(还可以对类进行标记,尽量但不要混着用)
#reruns是重跑次数,reruns_delay是间隔时间 @pytest.mark.flaky(reruns=6, reruns_delay=2) def test_a(self): pass
5、重复执行:pytest-repeat
<1>对整个文件的测试用例重复执行:pytest -v test_case.py --count=5
<2>直接标记某个用例执行多少次(还可以对类进行标记,尽量不要混着用):@pytest.mark.repeat(2)
实际发现重复执行的用例(断言是True),作为其他用例的依赖时,会导致其他用例跳过
用例在统计的时候会把重复执行的次数计算进去
6、标记
参考来源有更多详细的用法:https://www.cnblogs.com/lixy-88428977/p/9697881.html
示例是对单个用例的标记,还可以对类进行标记
''' 命令行执行:pytest -v -m "ios" ,则只会运行标记为ios的用例 命令行执行:pytest -v -m "not ios" ,则会运行没有标记ios的用例 ''' @pytest.mark.ios def test_c(self): pass @pytest.mark.android def test_d(self): pass