gpt4 book ai didi

python - 如何自动设置 pytest.param id?

转载 作者:行者123 更新时间:2023-12-04 16:45:22 25 4
gpt4 key购买 nike

我有一个参数化测试:

import pytest
@pytest.mark.parametrize('args', [
[1,2,3],
['a'],
[[],[1],['a']],
])
def test_bla(args):
assert all(args)
运行它时,我得到以下输出:
platform linux -- Python 3.6.12, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /home/bla/venv/bin/python
cachedir: .pytest_cache
metadata: {'Python': '3.6.12', 'Platform': 'Linux-4.15.0-132-generic-x86_64-with-Ubuntu-16.04-xenial', 'Packages': {'pytest': '6.0.2', 'py': '1.9.0', 'pluggy': '0.13.1'}, 'Plugins': {'metadata': '1.10.0'}}
rootdir: ...
plugins: metadata-1.10.0
collected 3 items

my_test.py::test_bla[args0] PASSED
my_test.py::test_bla[args1] PASSED
my_test.py::test_bla[args2] FAILED <---------- Note that this is not very descriptive :(

=================================================================================================== FAILURES ===================================================================================================
_______________________________________________________________________________________________ test_bla[args2] ________________________________________________________________________________________________

args = [[], [1], ['a']]

@pytest.mark.parametrize('args', [
[1,2,3],
['a'],
[[],[1],['a']],
])
def test_bla(args):
> assert all(args)
E AssertionError: assert False
E + where False = all([[], [1], ['a']])

my_test.py:8: AssertionError
=========================================================================================== short test summary info ============================================================================================
FAILED my_test.py::test_bla[args2] - AssertionError: assert False
========================================================================================= 1 failed, 2 passed in 0.11s ==========================================================================================
我想要的是在输出中获取使用的实际参数,例如:
my_test.py::test_bla[args0] PASSED
my_test.py::test_bla[args1] PASSED
my_test.py::test_bla[[], [1], ["a"]] FAILED <---------- Note the change here
这是我通过编辑 parameterization 得到的:
pytest.param([[],[1],['a']], id='[], [1], ["a"]'),
pytest 有没有办法只获取参数并使其为 id = str(param) ? (基本上自动化我刚刚在测试中编辑的内容)

最佳答案

您可以通过 ID functionids @pytest.mark.parametrize 装饰器的参数:

import pytest

@pytest.mark.parametrize('args', [
[1,2,3],
['a'],
[[],[1],['a']],
], ids=str)
def test_bla(args):
assert all(args)

关于python - 如何自动设置 pytest.param id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66024679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com