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

标签 python parameters pytest

我有一个参数化测试:

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

这是我通过编辑参数化得到的:

pytest.param([[],[1],['a']], id='[], [1], ["a"]'),

有没有办法让 pytest 只获取参数并将其设为 id = str(param)? (基本上自动化了我在测试中自己编辑的内容)

最佳答案

您可以传递ID function到 @pytest.mark.parametrize 装饰器的 ids 参数:

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/

相关文章:

python - "__requires__"在 python 中是什么意思?

c# - 从 C# 将参数传递给 powershell

python - 具有条件导入和可选要求的 pytest 覆盖范围

python - python在哪里存储str的原始值

Python 修剪/过滤掉点

arrays - 使用字符串数组作为子字符串参数的 VBA InStr 函数 (Excel)

sql - 更新时允许空值,使用 SQL Server 存储过程将记录插入数据库

python - 尝试访问命令行变量时出现 Pytest KeyError

Django异步测试: Cannot operate on a closed database

python - 创建协作白板绘图应用程序