在某些情况下,我们不想执行测试,或者在特定时间内测试案例不相关。在这种情况下,我们可以选择xfail测试或跳过测试
xfailed测试将被执行,但不会被视为部分失败或通过的测试。如果该测试失败,将不会显示任何回溯。我们可以使用xfail测试
@ pytest.mark.xfail。跳过测试意味着将不会执行测试。我们可以使用跳过测试
@ pytest.mark.skip。使用以下代码编辑test_addition.py
import pytest @pytest.mark.skip def test_add_1(): assert 100+200 == 400,"failed" @pytest.mark.skip def test_add_2(): assert 100+200 == 300,"failed" @pytest.mark.xfail def test_add_3(): assert 15+13 == 28,"failed" @pytest.mark.xfail 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被跳过,将不会执行。
test_add_3和test_add_4失败。这些测试将被执行,并将成为xfailed(测试失败)或xpassed(测试通过)测试的一部分。不会有任何失败的回溯。
当test_add_5通过时,将执行test_add_5和test_add_6,并且test_add_6将报告失败并进行追溯
通过py.test test_addition.py -v执行测试并查看结果
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 =================我们可以创建XML格式的测试结果,并将其提供给Continuous Integration服务器进行进一步处理,等等。这可以通过
py.test test_sample1.py -v --junitxml =“ result.xml”
result.xml将记录测试执行结果。在下面找到一个示例result.xml
<?xml version="1.0" encoding="UTF-8"?> <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”>中,我们可以看到总共两个测试,其中一个失败。在下面,您可以在<testcase>标记下查看有关每个已执行测试的详细信息。
▼
更多精彩推荐,请关注我们
▼
扫码关注更多精彩
你点的每个赞,我都认真当成了喜欢
