pytest fixtures函数参数化如何理解,常规用法是什么
Admin 2022-08-17 群英技术资讯 1092 次浏览
这篇文章主要介绍“pytest fixtures函数参数化如何理解,常规用法是什么”,有一些人在pytest fixtures函数参数化如何理解,常规用法是什么的问题上存在疑惑,接下来小编就给大家来介绍一下相关的内容,希望对大家解答有帮助,有这个方面学习需要的朋友就继续往下看吧。Pytest会在以下几个级别启用测试参数化:
对测试函数的参数进行参数化,直接使用内置的装饰器pytest.mark.parameterized即可。
import pytest
@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])
def test_eval(test_input, expected):
assert eval(test_input) == expected
从代码里可以看出,在装饰器里定义了三个不同的元组。我们把("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])
拆开看:
============================= test session starts =============================
platform win32 -- Python 3.9.4, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: D:\PycharmProjects\wms-api\interface, configfile: pytest.inicollected 3 items
test_module1.py ..F
demo\test_module1.py:3 (test_eval[6*9-42])
54 != 42
Expected :42
Actual :54
<Click to see difference>
test_input = '6*9', expected = 42
@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])
def test_eval(test_input, expected):
> assert eval(test_input) == expected
E AssertionError: assert 54 == 42
E + where 54 = eval('6*9')
test_module1.py:6: AssertionError
运行结果可以看到最后一次失败了,因为第三次运行测试函数取的参数是 ("6*9", 42),54不等于42,所以断言失败。
在参数化中标记单个测试实例,比如之前提到过的mark.xfail,这个可以标记测试函数为失败。那么在参数化中,如果想让其中的某个参数运行
的时候测试失败,就可以这样用:
import pytest
@pytest.mark.parametrize(
"test_input,expected",
[("3+5", 8), ("2+4", 6), pytest.param("6*9", 42, marks=pytest.mark.xfail)],
)
def test_eval(test_input, expected):
assert eval(test_input) == expected
运行一下:
test_module1.py [100%] ======================== 2 passed, 1 xfailed in 0.05s =========================..x
如果在测试函数上加了多个参数化装饰器,那么得到的参数组合是一个笛卡尔积:
import pytest
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
print("\nx:", x)
print("y:", y)
应该会组合成4组数据x=0/y=2, x=1/y=2, x=0/y=3, 和x=1/y=3,测试函数执行4次:
test_module1.py .
x: 0
y: 2
.
x: 1
y: 2
.
x: 0
y: 3
.
x: 1
y: 3
[100%]
============================== 4 passed in 0.01s ==============================
如果有些场景需要动态的确定参数或者fixture的使用范围,那么可以使用pytest_generate_tests这个钩子函数,该函数会在收集测试函数时候被调用。
通过传入的metafunc对象,可以检查请求测试函数的上下文,还可以进一步的调用metafunc.parameterize()来实现参数化。
举例,有个测试函数需要接受输入的字符串作为参数,而且通过pytest命令行获取到,那么就要编写一个获取参数的fixture函数来给测试函数调用。
# content of test_strings.py
def test_valid_string(stringinput):
assert stringinput.isalpha()
新建conftest.py文件,fixture函数写在这里:
# content of conftest.py
def pytest_addoption(parser):
parser.addoption(
"--stringinput",
action="append",
default=[],
help="list of stringinputs to pass to test functions",
)
def pytest_generate_tests(metafunc):
if "stringinput" in metafunc.fixturenames:
metafunc.parametrize("stringinput", metafunc.config.getoption("stringinput"))
现在用命令行方式来运行这个测试函数:
pytest -q --stringinput="hello" --stringinput="world" test_strings.py
会运行2次。
D:\PycharmProjects\wms-api\interface\demo>pytest -q --stringinput="hello" --stringinput="world" test_strings.py .. [100%] 2 passed in 0.01s
再换个输入参数,让测试函数失败:
pytest -q --stringinput="!" test_strings.py
FAILED test_strings.py::test_valid_string[!] - AssertionError: assert False1 failed in 0.04s
如果没有字符串输入,那么测试函数它将被跳过。因为metafunc.parameterize()被调用时,传过去的是一个列表:
pytest -q -rs test_strings.py
SKIPPED [1] test_strings.py: got empty parameter set ['stringinput'], function test_valid_string at $REGENDOC_TMPDIR/test_strings.py:2 1 skipped in 0.12s
注意,在调用metafunc时, 如果使用不同的参数集进行多次参数化,这些参数集上的所有参数名称都不能重复,否则将会报错。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
之前我们也了解很多python函数,今天给大家分享的函数是os.path.join()是拼接文件路径函数,那么os.path.join()函数怎样使用呢?接下来跟随小编来学习一下吧。
这篇文章主要介绍了python中的多cpu并行编程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
如何删除python字符串中指定的字符?方法一,使用pop()方法删除特定位置的字符;方法二,使用replace()方法删除指定字符,方法三,使用re.sub()方法可以替换特定模式的字符
python在 Python 中字符串也可以使用三个单引号或三个双引号来表示字符串,这样字符串中的内容就可以多行书写,并且被多行输出。
本文给大家分享了作者整理的五个python游戏开发的案例,通过具体设计思路,代码等方面详细了解python游戏开发的过程,非常的详细,希望大家能够喜欢
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008