zoukankan      html  css  js  c++  java
  • pytest扫盲11--xfail参数详解

    xfail 函数文档如下:

        def xfail(self,condition=None, reason=None, raises=None, run=True, strict=False):
            """mark the the test function as an expected failure if eval(self,condition)
            has a True value.  # 将eval(self,condition)为真的测试函数标记为失败。
    
            Optionally specify a reason for better reporting and run=False if
            you don't even want to execute the test function.  # 不想执行某个测试功能,可以指定 run=False,可以自定义错误原因 reason=‘’
    
            See http://doc.pytest.org/en/latest/skipping.html  # 官方文档说明
            """

    xfail 作用:

      1)xfail意味着你期望测试由于某种原因失败。常见的示例是对尚未实现的功能或尚未修复的错误进行测试。

      2)当预期失败时(标记为pytest.mark.xfail),但实际测试通过,标记为xpass用 x 标记代替 F,但无错误信息输出,将在测试摘要中报告

      3)使用 pytest.xfail(reason) 从用例中强制标记测试。

      4)通过 -r 显示 xfail、skip 详细信息   pytest -r

         5)忽略xfail:

    pytest --runxfail

    1)直接使用 @pytest.mark.xfail

    # File  : test_demo_10_xfail.py
    # IDE   : PyCharm
    
    import pytest
    
    def division(a, b):
        return int(a / b)
    
    @pytest.mark.xfail
    @pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), pytest.param(1, 0, 0, marks=pytest.mark.xfail), (6, 8, 0)], ids=['整除', '被除数为0', '除数为0', '非整除'])
    def test_1(a, b, c):
        res = division(a, b)
        assert res == c

    执行(使用xfail标记的用例仍然会执行):

    • xfailed  为失败的用例,小写 x
    • xpassed  为通过的用例,大写 X
    E:personalpython38python.exe E:/personal/GitWorkSpace/pytest_basic/main.py
    test_demo_10_xfail.py::test_1[整除]
    test_demo_10_xfail.py::test_1[被除数为0]
    test_demo_10_xfail.py::test_1[除数为0]
    test_demo_10_xfail.py::test_1[非整除]
    XXxX
    1 xfailed, 3 xpassed in 0.12s
    
    Process finished with exit code 0

    2)condition  为False执行标记的代码块,True则标记 xpass

    @pytest.mark.xfail(condition=True, reason='xfail标记')
    @pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), pytest.param(1, 0, 0, marks=pytest.mark.xfail), (6, 8, 0)], ids=['整除', '被除数为0', '除数为0', '非整除'])
    def test_1(a, b, c):
        res = division(a, b)
        assert res == c

    执行:

    E:personalpython38python.exe E:/personal/GitWorkSpace/pytest_basic/main.py
    test_demo_10_xfail.py::test_1[整除]
    test_demo_10_xfail.py::test_1[被除数为0]
    test_demo_10_xfail.py::test_1[除数为0]
    test_demo_10_xfail.py::test_1[非整除]
    test_demo_10_xfail.py::test_2
    XXxX.
    1 passed, 1 xfailed, 3 xpassed in 0.14s
    
    Process finished with exit code 0

    3)raises  具体原因,python标准错误类型,可以在raises参数中指定单个异常或异常组

    • 如果测试失败且没有提到指定的异常,则报告failed
    • 如果测试失败,raises 报告的异常与失败异常一致,则标记为 xfailed
    @pytest.mark.xfail(raises=AssertionError)
    def test_03():
        assert 3 == 4
    
    @pytest.mark.xfail(raises=ValueError)
    def test_04():
        if isinstance('1234', int) is False:
            raise TypeError("传入参数非整数")
    E:personalpython38python.exe E:/personal/GitWorkSpace/pytest_basic/main.py
    test_demo_10_xfail.py::test_03
    test_demo_10_xfail.py::test_04
    xF
    ================================== FAILURES ===================================
    ___________________________________ test_04 ___________________________________
    
        @pytest.mark.xfail(raises=ValueError)
        def test_04():
            if isinstance('1234', int) is False:
    >           raise TypeError("传入参数非整数")
    E           TypeError: 传入参数非整数
    
    test_demo_10_xfail.py:41: TypeError
    =========================== short test summary info ===========================
    FAILED test_demo_10_xfail.py::test_04 - TypeError: 传入参数非整数
    1 failed, 1 xfailed in 0.14s
    
    Process finished with exit code 0

    4)run=False 直接标记为xfail,且不执行,防止在xfail死循环(默认为True执行用例)

    @pytest.mark.xfail(run=False)
    @pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), pytest.param(1, 0, 0, marks=pytest.mark.xfail), (6, 8, 0)], ids=['整除', '被除数为0', '除数为0', '非整除'])
    def test_1(a, b, c):
        res = division(a, b)
        assert res == c
    E:personalpython38python.exe E:/personal/GitWorkSpace/pytest_basic/main.py
    test_demo_10_xfail.py::test_1[整除]
    test_demo_10_xfail.py::test_1[被除数为0]
    test_demo_10_xfail.py::test_1[除数为0]
    test_demo_10_xfail.py::test_1[非整除]
    xxxx
    4 xfailed in 0.27s
    
    Process finished with exit code 0

    5)strict=True  会使产生了 xpass 的用例,标记为测试失败(默认False则标记通过)

    @pytest.mark.xfail(strict=True)
    def test_05():
        assert 3 == 3

    执行结果(只要 strict=True 的用例均会失败):

    E:personalpython38python.exe E:/personal/GitWorkSpace/pytest_basic/main.py
    test_demo_10_xfail.py::test_05
    F
    ================================== FAILURES ===================================
    ___________________________________ test_05 ___________________________________
    [XPASS(strict)] 
    =========================== short test summary info ===========================
    FAILED test_demo_10_xfail.py::test_05
    1 failed in 0.07s
    
    Process finished with exit code 0

    5)在 parametrize 中使用  marks=pytest.mark.xfail

    @pytest.mark.parametrize('a, b, c', [(4, 2, 2), (0, 2, 0), pytest.param(1, 0, 0, marks=pytest.mark.xfail), (6, 8, 0)], ids=['整除', '被除数为0', '除数为0', '非整除'])
    def test_1(a, b, c):
        res = division(a, b)
        assert res == c
    E:personalpython38python.exe E:/personal/GitWorkSpace/pytest_basic/main.py
    test_demo_10.py::test_1[整除]
    test_demo_10.py::test_1[被除数为0]
    test_demo_10.py::test_1[除数为0]
    test_demo_10.py::test_1[非整除]
    ..x.
    3 passed, 1 xfailed in 0.14s
    喜时之言,多失信;怒时之言,多失体
  • 相关阅读:
    广域网(ppp协议、HDLC协议)
    0120. Triangle (M)
    0589. N-ary Tree Preorder Traversal (E)
    0377. Combination Sum IV (M)
    1074. Number of Submatrices That Sum to Target (H)
    1209. Remove All Adjacent Duplicates in String II (M)
    0509. Fibonacci Number (E)
    0086. Partition List (M)
    0667. Beautiful Arrangement II (M)
    1302. Deepest Leaves Sum (M)
  • 原文地址:https://www.cnblogs.com/xiaohuboke/p/13533601.html
Copyright © 2011-2022 走看看