zoukankan      html  css  js  c++  java
  • pytest 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. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. See http://doc.pytest.org/en/latest/skipping.html """ ```

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

    @pytest.mark.xfail
    def test_01():
        assert 1 == 1
    
    
    @pytest.mark.xfail
    def test_02():
        assert 1 == 2

    运行结果为

     使用xfail标记用例预期失败,如果用例运行成功则显示Xpassed,失败则显示xfailed。xfail标记并不会影响用例的运行

    2.strict参数 :设置strict=True以确保XPASS时,测试的记录为失败

    同样使用该代码

    @pytest.mark.xfail(strict=True)
    def test_01():
        assert 1 == 1
    
    
    @pytest.mark.xfail
    def test_02():
        assert 1 == 2

    运行结果

     xpass运行的结果现在显示为failed

    3,结合skipif 条件,条件为Ture运行xfail

    @pytest.mark.xfail(1 >= 5, reason='test')
    def test_01():
    assert 1 == 1

    @pytest.mark.xfail(1 >= 5, reason='test')
    def test_02():
    assert 1 == 5


    @pytest.mark.xfail(1 <= 5, reason='test')
    def test_03():
    assert 1 == 1


    @pytest.mark.xfail(1 <= 5, reason='test')
    def test_04():
    assert 1 == 2

    运行结果

     用例01、02,因为条件为false所以不运行xfail,结果显示为pass、fail

    03、04条件为true,运行xfail,结果显示为xpass、xfail

    4raises参数

    具体的说明测试失败的原因。可以在raises参数中指定单个异常或异常组,如果测试失败且没有提到指定的异常,那么测试将被报告为常规失败raises

    @pytest.mark.xfail(raises=AssertionError)
    def test_01():
        assert 1 == 2
    
    
    @pytest.mark.xfail(raises=ValueError)
    def test_02():
        if isinstance('1234', int) is not True:
            raise TypeError("传入参数非整数")

    运行结果:

     01测试用例,异常为指定异常,xfail运行,结果显示xfailed

    02测试用例,异常为非指定异常,xfail不运行,结果显示failed



  • 相关阅读:
    12.链式法则
    11.感知机
    10.1激活函数及其梯度(softmax求梯度)
    10.损失函数及其梯度(均方差梯度[使用线性函数y=w*x+b作为激活函数])
    9:常见函数以及激活函数的梯度
    8:梯度
    小程序 scroll-view
    小程序swiper
    view 标签
    微信小程序 tab选项卡
  • 原文地址:https://www.cnblogs.com/jescs/p/12107178.html
Copyright © 2011-2022 走看看