在某些情況下,我們不想執(zhí)行測試,或者在特定時(shí)間內(nèi)測試案例不相關(guān)。在這種情況下,我們可以選擇xfail測試或跳過測試
xfailed測試將被執(zhí)行,但不會(huì)被視為部分失敗或通過的測試。如果該測試失敗,將不會(huì)顯示任何回溯。我們可以使用xfail測試
跳過測試意味著將不會(huì)執(zhí)行測試。我們可以使用跳過測試
使用以下代碼編輯test_addition.py
import pytest
def test_add_1():
assert 100+200 == 400,"failed"
def test_add_2():
assert 100+200 == 300,"failed"
def test_add_3():
assert 15+13 == 28,"failed"
def test_add_4():
assert 15+13 == 100,"failed"
def test_add_5():
assert 3+2 == 5,"failed"
def test_add_6():
assert 3+2 == 6,"failed"
這里
test_add_1和test_add_2被跳過,將不會(huì)執(zhí)行。
test_add_3和test_add_4失敗。這些測試將被執(zhí)行,并將成為xfailed(測試失敗)或xpassed(測試通過)測試的一部分。不會(huì)有任何失敗的回溯。
當(dāng)test_add_5通過時(shí),將執(zhí)行test_add_5和test_add_6,并且test_add_6將報(bào)告失敗并進(jìn)行追溯
通過py.test test_addition.py -v執(zhí)行測試并查看結(jié)果
test_addition.py::test_add_1 SKIPPED
test_addition.py::test_add_2 SKIPPED
test_addition.py::test_add_3 XPASS
test_addition.py::test_add_4 xfail
test_addition.py::test_add_5 PASSED
test_addition.py::test_add_6 FAILED
============================================== FAILURES ==============================================
_____________________________________________ test_add_6 _____________________________________________
def test_add_6():
> assert 3+2 == 6,"failed"
E AssertionError: failed
E assert (3 + 2) == 6
test_addition.py:24: AssertionError
================ 1 failed, 1 passed, 2 skipped, 1 xfailed, 1 xpassed in 0.07 seconds =================
我們可以創(chuàng)建XML格式的測試結(jié)果,并將其提供給Continuous Integration服務(wù)器進(jìn)行進(jìn)一步處理,等等。這可以通過
py.test test_sample1.py -v --junitxml =“ result.xml”
result.xml將記錄測試執(zhí)行結(jié)果。在下面找到一個(gè)示例result.xml
<testsuite errors="0" failures="1" name="pytest" skips="0" tests="2" time="0.046">
<testcase classname="test_sample1" file="test_sample1.py" line="3" name="test_file1_method1" time="0.001384973526">
<failure message="AssertionError:test failed because x=5 y=6 assert 5 ==6">
@pytest.mark.set1
def test_file1_method1():
x=5
y=6
assert x+1 == y,"test failed"
> assert x == y,"test failed because x=" + str(x) + " y=" + str(y)
E AssertionError: test failed because x=5 y=6
E assert 5 == 6
test_sample1.py:9: AssertionError
</failure>
</testcase>
<testcase classname="test_sample1" file="test_sample1.py" line="10" name="test_file1_method2" time="0.000830173492432" />
</testsuite>
從<testsuite errors =“ 0” failures =“ 1” name =“ pytest” skips =“ 0” tests =“ 2” time =“ 0.046”>中,我們可以看到總共兩個(gè)測試,其中一個(gè)失敗。在下面,您可以在<testcase>標(biāo)記下查看有關(guān)每個(gè)已執(zhí)行測試的詳細(xì)信息。