pytest标记测试用例为预期失败(@pytest.mark.xfail)

  我们平时在写测试用例的过程中,有时会遇到“已知由于某些原因,某些场景是有问题的,或者是功能暂时没有实现”这种情况,那么测试用例执行的时候我们就知道这个测试用例会失败,也就是预期失败,这个时候我们就可以使用@pytest.mark.xfail装饰器来标记测试用例为预期失败函数。

@pytest.mark.xfail使用

标记测试函数为失败函数

 方法:
     xfail(condition=None, reason=None, raises=None, run=True, strict=False)

 常用参数:
     condition:预期失败的条件,必传参数
     reason:失败的原因,必传参数

 使用方法:
     @pytest.mark.xfail(condition, reason="xx")

举例:

# file_name: test_xfail.py


import pytest


class Test_C:

    def test_a(self):
        print('\n------------------> test_a has ran')
        assert 1

    @pytest.mark.xfail(condition=2 > 1, reason='标记为预期失败')
    def test_b(self):
        print('------------------> test_b has ran')
        assert 0


if __name__ == '__main__':
    pytest.main(['-s', 'test_xfail.py'])

运行结果:

 x表示被@pytest.mark.xfail标记失败了。

posted @ 2021-02-22 18:00  lwjnicole  阅读(309)  评论(0编辑  收藏  举报