【官方解释】
@_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或者缺少的特性。
你在调测测试用例过程中,对尚未实现的函数的测试用例、尚未修复的错误,需要使用这个函数标记一下,执行完成后,它会在结果中显示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