万能的标签
标记和分类用例: @pytest.mark.level1
1)在用例上增加: @pytest.mark.runtest
2)在运行时增加,-m标签:pytest.main(['-v', '--html=./report/{}'.format(report_name), '--self-contained-html', '-m', 'runtest','--timeout=300','--reruns=4','--reruns-delay=1'])
标记用例执行顺顺序pytest.mark.run(order=1) (需安装pytest-ordering)
标记用例在指定条件下跳过或直接失败 @pytest.mark.skipif()/xfail()
标记使用指定fixture(测试准备及清理方法) @pytest.mark.usefixtures()
详见:https://www.jianshu.com/p/1761c1c2b807
参数化 @pytest.mark.parametrize
#单个参数化
@pytest.mark.parametrize('x',[0,1])
def test_foo(x):
print("测试数据组合:x->%s"%(x))
'''
结果:
Test_demo.py .测试数据组合:x->0
.测试数据组合:x->1
'''
#同时声明多个配对参数
@pytest.mark.parametrize('x,y',[(0,1),(1,2)])
def test_foo(x,y):
print("测试数据组合:x->%s,y->%s"%(x,y))
'''
结果:
Test_demo.py .测试数据组合:x->0,y->1
.测试数据组合:x->1,y->2
'''
#堆叠参数化装饰器
@pytest.mark.parametrize('x',[0,1])
@pytest.mark.parametrize('y',[2,3])
def test_foo(x,y):
print("测试数据组合:x->%s,y->%s"%(x,y))
'''
结果:
Test_demo.py .测试数据组合:x->0,y->2
.测试数据组合:x->1,y->2
.测试数据组合:x->0,y->3
.测试数据组合:x->1,y->3
'''
标记超时时间 @pytest.mark.timeout(60) (需安装pytest-timeout)
1)在用例上增加: @pytest.mark.timeout(60)
2)在运行时增加,--timeout:pytest.main(['-v', '--html=./report/{}'.format(report_name), '--self-contained-html', '-m', 'runtest','--timeout=300','--reruns=4','--reruns-delay=1'])
标记失败重跑次数为5次,延迟1秒后重跑@pytest.mark.flaky(reruns=5, reruns_delay=1) (需安装pytest-rerunfailures)
1)在执行用例上增加: @pytest.mark.flaky(reruns=2, reruns_delay=1)
2)在运行时增加,--reruns,--reruns-delay:pytest.main(['-v', '--html=./report/{}'.format(report_name), '--self-contained-html', '-m', 'runtest','--timeout=300','--reruns=4','--reruns-delay=1'])
3)用例上的设置会覆盖运行时的设置。