前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PyTest Xfail /跳过测试

PyTest Xfail /跳过测试

作者头像
用户7466307
发布2020-06-17 11:00:10
9300
发布2020-06-17 11:00:10
举报

在某些情况下,我们不想执行测试,或者在特定时间内测试案例不相关。在这种情况下,我们可以选择xfail测试或跳过测试

xfailed测试将被执行,但不会被视为部分失败或通过的测试。如果该测试失败,将不会显示任何回溯。我们可以使用xfail测试

代码语言:javascript
复制
@ pytest.mark.xfail。

跳过测试意味着将不会执行测试。我们可以使用跳过测试

代码语言:javascript
复制
@ pytest.mark.skip。

使用以下代码编辑test_addition.py

代码语言:javascript
复制
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执行测试并查看结果

代码语言:javascript
复制
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

我们可以创建XML格式的测试结果,并将其提供给Continuous Integration服务器进行进一步处理,等等。这可以通过

py.test test_sample1.py -v --junitxml =“ result.xml”

result.xml将记录测试执行结果。在下面找到一个示例result.xml

代码语言:javascript
复制
<?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>标记下查看有关每个已执行测试的详细信息。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-03-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软件测试test 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 结果XML
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档