pytest框架[email protected]()

【官方解释】

@_with_exception(XFailed)
def xfail(reason: str = "") -> "NoReturn":
	__tracebackhide__ = True
	    raise XFailed(reason)

Imperatively xfail an executing test or setup function with the given reason.
This function should be called only during testing (setup, call or teardown).
… note::
It is better to use the :ref:pytest.mark.xfail ref marker when
possible to declare a test to be xfailed under certain conditions
like known bugs or missing features.
“”"
【翻译】
强制使用给定的原因来xfail一个正在执行的测试或设置函数。
此函数只能在测试期间调用(设置、调用或拆卸)。
注:
最好使用:pytest.mark.xfail文件引用​​`marker when
可以在某些条件下声明测试失败
比如已知的bug或者缺少的特性。

某大佬的解释:我是没没找到大佬说的源码在哪。
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

你在调测测试用例过程中,对尚未实现的函数的测试用例、尚未修复的错误,需要使用这个函数标记一下,执行完成后,它会在结果中显示xpass,如下test_case_1、12的执行结果:

# coding=utf-8
import pytest

@pytest.fixture()
def test_case_3():
    print('---3号用例完成---')

@pytest.fixture()
def test_case_4():
    print('---4号用例完成---')

@pytest.fixture()
def test_case_5():
    print('---5号用例完成---')

@pytest.fixture()
def test_case_6():
    print('---6号用例完成---')

@pytest.fixture()
def test_case_7():
    print('---7号用例完成---')

@pytest.fixture()
def test_case_8():
    print('---8号用例完成---')

# (1)这里按照【从下到上的顺序】,执行优先级是3、4、5
@pytest.mark.usefixtures('test_case_5')
@pytest.mark.usefixtures('test_case_4')
@pytest.mark.usefixtures('test_case_3')
class Testlogin001:

    # 被pytest.fixture()装饰的函数,函数名可以作为变量传递给测试用例,最终在执行测试用例之前执行这个装饰过的函数
    def test_case_1(self, test_case_8):
        print('---1号用例完成---')

    # # (2)这里按照调用了前面的函数test_case_6,局部的调用,执行优先级是最高的。
    # @pytest.mark.usefixtures('test_case_7')
    # @pytest.mark.usefixtures('test_case_6')
    # def test_case_2(self):
    #     print('---2号用例完成---')
    #
    # # 单参数单值
    # @pytest.mark.parametrize('arg', [1])
    # def test_case_9(self, arg):
    #     print("传入的值为:{}".format(arg))
    #     assert arg == 1
    #
    # # 单参数多值
    # @pytest.mark.parametrize('arg',['abc',1,{'a':1,'b':3},(4,5)])
    # def test_case_10(self, arg):
    #     print(f"传入的值为:{arg}")
    #
    # # 多参数多值
    # @pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("5-2", 3), ("5*2", 10)])
    # def test_case_11(self, test_input, expected):
    #     print(f"原值:{test_input} 期望值{expected}")
    #     assert eval(test_input) == expected

    @pytest.mark.xfail()
    def test_case_12(self):
        print('---12号用例完成---')

if __name__ == "__main__":
    pytest.main(['-vs', 'test_1.py'])


collecting ... collected 2 items
test_1.py::Testlogin001::test_case_1 
启动浏览器
---进入要执行模块的的界面---
---3号用例完成---
---4号用例完成---
---5号用例完成---
---8号用例完成---
---1号用例完成---
PASSED
退出浏览器

test_1.py::Testlogin001::test_case_12 
启动浏览器
---进入要执行模块的的界面---
---3号用例完成---
---4号用例完成---
---5号用例完成---
---12号用例完成---
XPASS
退出浏览器


--------- generated html file: file://D:\se_frame\Reports\report.html ---------
======================== 1 passed, 1 xpassed in 0.13s =========================

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/weixin_45451320/article/details/113855339