Pytest框架之 - xfail标记用例为预期失败

  • 作者:lwj
  • 分类:Pytest
  • 发表日期:2020-11-01 20:09:19
  • 阅读(411)
  • 评论(0)

pytes.xfail()

在测试用例"中"调用pytes.xfail()方法,可以选择传入reason参数表示原因, 如下代码在测试用例中标记用例为xfail

#!/usr/bin/python3
# coding=utf-8
# Author: 文
import pytest
class Test_Two():
    def test_01(self):
        print("===========> test_01 start")
        pytest.xfail(reason='该功能尚未完善')
        print("===========> test_01")

    def test_02(self):
        print("===========> test_02")

if __name__ == "__main__":
    pytest.main(["-rs", "test_three.py::Test_Two"])

pytest.xfail()方法之前的代码运行之后的不运行; 运行结果中有一条用例被标记为xfailed, 标记为xfail的用例显示为x, 如下:

F:\Python3.6.5\python.exe E:/xxx/test_three.py
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: E:\xxx, configfile: pytest.ini
plugins: html-2.1.1, metadata-1.10.0
collected 2 items
test_three.py ===========> test_01 start
x===========> test_02 start
===========> test_02
.
======================== 1 passed, 1 xfailed in 1.09s =========================
Process finished with exit code 0

@pytest.mark.xfail

xfai还有一种使用方法就是@pytest.mark.xfail标签, 他的含义:期望测试用例是失败的, 但是不会影响测试用例的的执行; 如果测试用例执行失败的则结果是xfailed(不会额外显示出错误信息), 如果用例执行成功则结果是xpassed

condition:如果满足条件则标记用例执行失败,  默认为True
reason:用例标记为预期失败的原因,  默认为None
raises:指定个异常类或者异常类元组表明期望用例抛出这些异常;若用例失败不是因为这些异常那用例会执行失败标记为FAILED
run:是否执行, 若为True则执行,若为False则用例不执行直接标记为XFAIL,默认为True
strict:为False用例执行成功为xpassed执行失败则为xfailed;为True用例执行成功标记为failed执行失败则为xfailed,  默认None

参数condition

#!/usr/bin/python3
# coding=utf-8
# Author: 文

import pytest
class Test_Two():
    # 参数condition为True则pytest.mark.xfail不执行
    @pytest.mark.xfail(condition=1<2, reason="pytest.mark.xfail不执行")
    def test_01(self):
        print("===========> test_01")

    # 参数condition为False则pytest.mark.xfail执行
    @pytest.mark.xfail(condition=1>2, reason="pytest.mark.xfail执行")
    def test_02(self):
        print("===========> test_02")

if __name__ == "__main__":
    pytest.main(["-rs", "test_three.py::Test_Two"])

执行结果如下:一个为passed一个为xpassed 

F:\Python3.6.5\python.exe E:/xxx/test_three.py
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: E:\xxx, configfile: pytest.ini
plugins: html-2.1.1, metadata-1.10.0
collected 2 items
test_three.py X.                                                         [100%]
======================== 1 passed, 1 xpassed in 0.38s =========================
Process finished with exit code 0

参数run

#!/usr/bin/python3
# coding=utf-8
# Author: 文

import pytest
class Test_Two():
    @pytest.mark.xfail(run=True)      # 参数run为True不执行
    def test_01(self):
        print("===========> test_01")
        assert 1==1

    @pytest.mark.xfail(run=False)      # 参数run为False不执行
    def test_02(self):
        print("===========> test_02")
        assert 1==1

if __name__ == "__main__":
    pytest.main(["-rs", "test_three.py::Test_Two"])

执行结果如下:一个为xfailed一个为xpassed 

F:\Python3.6.5\python.exe E:/xxx/test_three.py
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: E:\xxx, configfile: pytest.ini
plugins: html-2.1.1, metadata-1.10.0
collected 2 items
test_three.py Xx                                                         [100%]
======================== 1 xfailed, 1 xpassed in 0.07s ========================
Process finished with exit code 0

参数strict和reason

import pytest
class Test_Two():

    @pytest.mark.xfail(strict=False, reason="test_01 strict=False用例执行成功结果为xpassed")
    def test_01(self):
        print("===========> test_01")
        assert 1==1

    @pytest.mark.xfail(strict=False, reason="test_02 strict=False用例执行失败结果为xfailed")
    def test_02(self):
        print("===========> test_02")
        assert 1 == 0

    @pytest.mark.xfail(strict=True, reason="test_03 strict=True用例执行成功结果为failed")
    def test_03(self):
        print("===========> test_03")
        assert 1==1

    @pytest.mark.xfail(strict=True, reason="test_04 strict=True用例执行失败结果为xfailed")
    def test_04(self):
        print("===========> test_04")
        assert 1 == 0

if __name__ == "__main__":
    pytest.main(["-rs", "test_three.py::Test_Two"])

执行结果如下:一个为failed 两个为xfailed 一个为xpassed

F:\Python3.6.5\python.exe E:/xxx/test_three.py
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: E:\xxx, configfile: pytest.ini
plugins: html-2.1.1, metadata-1.10.0
collected 4 items
test_three.py XxFx                                                       [100%]
================================== FAILURES ===================================
______________________________ Test_Two.test_03 _______________________________
[XPASS(strict)] test_03 strict=True用例执行成功结果为failed
---------------------------- Captured stdout call -----------------------------
===========> test_03
=================== 1 failed, 2 xfailed, 1 xpassed in 0.05s ===================
Process finished with exit code 0

 ~ 不积跬步无以至千里

觉得不错,支持一下!

提交评论

您尚未登录,登录之后方可评论~ 登录 or 注册

评论列表

暂无评论
返回顶部

建议反馈

1. 可在博文底部留言评论

2. 发邮件到i_suichuan@163.com