pytest里面用xfail标记用例为预期失败的用例,报告详情不会报错

pytest.xfail(self,condition=None, reason=None, raises=None, run=True, strict=False)

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

reason 原因 

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

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

import pytest

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


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


if __name__ == "__main__":
    pytest.main(["-s","-v", "test_01.py"])

执行结果:

collecting ... collected 2 items

test_01.py::test_01 FAILED
test_01.py::test_02 XFAIL

=================================== FAILURES ===================================
___________________________________ test_01 ____________________________________
[XPASS(strict)] 
=========================== short test summary info ============================
FAILED test_01.py::test_01
========================= 1 failed, 1 xfailed in 0.07s =========================

Process finished with exit code 0

 2、结合skipif 条件,条件为Ture运行xfail

import pytest

# 不满足条件,不执行@pytest.mark.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

# 预期失败,但用例执行成功,结果显示xpass,意料之外的成功
@pytest.mark.xfail(1 <= 5, reason='test')
def test_03():
    assert 1 == 1

# 预期失败,用例执行失败,结果显示xfail,意料之内的失败
@pytest.mark.xfail(1 <= 5, reason='test')
def test_04():
    assert 1 == 2


if __name__ == "__main__":
    pytest.main(["-s","-v", "test_01.py"])

执行结果:

collecting ... collected 4 items

test_01.py::test_01 PASSED
test_01.py::test_02 FAILED
test_01.py::test_03 XPASS
test_01.py::test_04 XFAIL

=================================== FAILURES ===================================
___________________________________ test_02 ____________________________________

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

test_01.py:9: AssertionError
=========================== short test summary info ============================
FAILED test_01.py::test_02 - assert 1 == 5
============== 1 failed, 1 passed, 1 xfailed, 1 xpassed in 0.07s ===============

Process finished with exit code 0

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

import pytest

@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:
        print("参数类型不是整数")
        raise TypeError("传入参数非整数")


if __name__ == "__main__":
    pytest.main(["-s","-v", "test_01.py"])

 执行结果:

collecting ... collected 2 items

test_01.py::test_01 XFAIL
test_01.py::test_02 参数类型不是整数
FAILED

=================================== FAILURES ===================================
___________________________________ test_02 ____________________________________

    @pytest.mark.xfail(raises=ValueError)
    def test_02():
        if isinstance('1234', int) is not True:
            print("参数类型不是整数")
>           raise TypeError("传入参数非整数")
E           TypeError: 传入参数非整数

test_01.py:12: TypeError
=========================== short test summary info ============================
FAILED test_01.py::test_02 - TypeError: 传入参数非整数
========================= 1 failed, 1 xfailed in 0.07s =========================

Process finished with exit code 0

 

当用例a失败的时候,如果用例b和用例c都是依赖于第一个用例a的结果,那可以直接跳过用例b和c的测试,直接给他标记失败xfail
用到的场景,登录是第一个用例,登录之后的操作b是第二个用例,登录之后操作c是第三个用例,很明显三个用例都会走到登录。
如果登录都失败了,那后面2个用例就没测试必要了,直接跳过,并且标记为失败用例,这样可以节省用例时间。

import pytest

'''
从结果可以看出用例1失败了,用例2和3没执行,直接标记为xfail了
'''

canshu = [{"user": "amdin", "psw": ""}]

@pytest.fixture(scope="module")
def login(request):
    user = request.param["user"]
    psw = request.param["psw"]
    print("正在操作登录,账号:%s, 密码:%s" % (user, psw))
    if psw:
        return True
    else:
        return False

@pytest.mark.parametrize("login", canshu, indirect=True)
class Test_xx():

    def test_01(self, login):
        '''用例1登录'''
        result = login
        print("用例1:%s" % result)
        assert result == True

    def test_02(self, login):
        result = login
        print("用例2,登录结果:%s" % result)
        if not result:
            print(not result)
            pytest.xfail("登录不成功, 标记为xfail")
        # pytest.xfail执行后,后面的代码不会执行
        print("执行断言")
        assert 1 == 1

    def test_03(self, login):
        result = login
        print("用例3,登录结果:%s" % result)
        if not result:
            pytest.xfail("登录不成功, 标记为xfail")

        assert 1 == 1

class TestClass():

    def test_one(self):
        print("----------start-----")
        pytest.xfail(reason="该功能尚未完善")
        # pytest.xfail执行后,后面的代码不会执行
        print("test_one方法执行")
        assert  1==2
    @pytest.mark.xfail
    def test_two(self):
        print("test_two方法执行")
        assert  'o' in 'love'

    @pytest.mark.xfail(reason="预期失败")
    def test_three(self):
        print("test_three方法执行")
        assert 3-2==1


if __name__ == "__main__":
    pytest.main(["-s","-v", "test_01.py"])

 执行结果:

collecting ... collected 6 items

test_01.py::Test_xx::test_01[login0] 正在操作登录,账号:amdin, 密码:
用例1:False
FAILED
test_01.py::Test_xx::test_02[login0] 用例2,登录结果:False
True
XFAIL
test_01.py::Test_xx::test_03[login0] 用例3,登录结果:False
XFAIL
test_01.py::TestClass::test_one ----------start-----
XFAIL
test_01.py::TestClass::test_two test_two方法执行
XPASS
test_01.py::TestClass::test_three test_three方法执行
XPASS

=================================== FAILURES ===================================
___________________________ Test_xx.test_01[login0] ____________________________

self = <demo.test_01.Test_xx object at 0x7fce68a86828>, login = False

    def test_01(self, login):
        '''用例1登录'''
        result = login
        print("用例1:%s" % result)
>       assert result == True
E       assert False == True
E         +False
E         -True

test_01.py:26: AssertionError
=========================== short test summary info ============================
FAILED test_01.py::Test_xx::test_01[login0] - assert False == True
=================== 1 failed, 3 xfailed, 2 xpassed in 0.07s ====================

Process finished with exit code 0

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值