萬能的標(biāo)簽
標(biāo)記和分類用例: @pytest.mark.level1
1)在用例上增加: @pytest.mark.runtest
2)在運(yùn)行時增加放椰,-m標(biāo)簽:pytest.main(['-v', '--html=./report/{}'.format(report_name), '--self-contained-html', '-m', 'runtest','--timeout=300','--reruns=4','--reruns-delay=1'])
標(biāo)記用例執(zhí)行順順序pytest.mark.run(order=1) (需安裝pytest-ordering)
標(biāo)記用例在指定條件下跳過或直接失敗 @pytest.mark.skipif()/xfail()
標(biāo)記使用指定fixture(測試準(zhǔn)備及清理方法) @pytest.mark.usefixtures()
詳見:http://www.reibang.com/p/1761c1c2b807
參數(shù)化 @pytest.mark.parametrize
#單個參數(shù)化
@pytest.mark.parametrize('x',[0,1])
def test_foo(x):
print("測試數(shù)據(jù)組合:x->%s"%(x))
'''
結(jié)果:
Test_demo.py .測試數(shù)據(jù)組合:x->0
.測試數(shù)據(jù)組合:x->1
'''
#同時聲明多個配對參數(shù)
@pytest.mark.parametrize('x,y',[(0,1),(1,2)])
def test_foo(x,y):
print("測試數(shù)據(jù)組合:x->%s,y->%s"%(x,y))
'''
結(jié)果:
Test_demo.py .測試數(shù)據(jù)組合:x->0,y->1
.測試數(shù)據(jù)組合:x->1,y->2
'''
#堆疊參數(shù)化裝飾器
@pytest.mark.parametrize('x',[0,1])
@pytest.mark.parametrize('y',[2,3])
def test_foo(x,y):
print("測試數(shù)據(jù)組合:x->%s,y->%s"%(x,y))
'''
結(jié)果:
Test_demo.py .測試數(shù)據(jù)組合:x->0,y->2
.測試數(shù)據(jù)組合:x->1,y->2
.測試數(shù)據(jù)組合:x->0,y->3
.測試數(shù)據(jù)組合:x->1,y->3
'''
標(biāo)記超時時間 @pytest.mark.timeout(60) (需安裝pytest-timeout)
1)在用例上增加: @pytest.mark.timeout(60)
2)在運(yùn)行時增加星澳,--timeout:pytest.main(['-v', '--html=./report/{}'.format(report_name), '--self-contained-html', '-m', 'runtest','--timeout=300','--reruns=4','--reruns-delay=1'])
標(biāo)記失敗重跑次數(shù)為5次哥纫,延遲1秒后重跑@pytest.mark.flaky(reruns=5, reruns_delay=1) (需安裝pytest-rerunfailures)
1)在執(zhí)行用例上增加: @pytest.mark.flaky(reruns=2, reruns_delay=1)
2)在運(yùn)行時增加二驰,--reruns大脉,--reruns-delay:pytest.main(['-v', '--html=./report/{}'.format(report_name), '--self-contained-html', '-m', 'runtest','--timeout=300','--reruns=4','--reruns-delay=1'])
3)用例上的設(shè)置會覆蓋運(yùn)行時的設(shè)置胡野。