gpt4 book ai didi

python - 仅使用自定义名称的动态 pytest 方法生成

转载 作者:太空宇宙 更新时间:2023-11-03 16:51:26 24 4
gpt4 key购买 nike

您好,我正在尝试 pytest 文档中提到的以下示例,

# content of test_expectation.py
@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

当我使用 py.test -v 运行时,它给出如下输出,

test_code.py::test_eval[3+5-8] PASSED
test_code.py::test_eval[2+4-6] PASSED
test_code.py::test_eval[6*9-42] FAILED

这里,当我生成 html 报告时,当我使用很长的输入数据时,名称太长。

在上面的例子中,为了避免获取第一个结果,方法的名称是[3+5-8]。即它采用元组 (3+5,8) 并将其附加到实际的测试用例方法名称。

现在,在我的例子中,元组是(“短名称”,“非常长的字符串”)而不是(3+5,8),所以在我的 html 报告中显示很长。是否可以仅显示“短名称”而不显示第二个值?

最佳答案

请您粘贴当前显示的长名称和您期望的短名称,以便您的问题变得清晰。以防万一它有帮助,您可以使用 @pytest.mark.parametrize 中的“ids”字段来使用参数化值自定义显示的测试名称。

对于 e,g

@pytest.mark.parametrize("test_input,expected", [
("3+5", 8),
("2+4", 6),
("6*9", 42),
], ids=['cust_name_1', 'cust_name_2', 'cust_name_3'])
def test_eval(test_input, expected):
...

会将您的测试名称显示为

test_code.py::test_eval[cust_name_1] PASSED
test_code.py::test_eval[cust_name_1] PASSED
test_code.py::test_eval[cust_name_1] FAILED

关于python - 仅使用自定义名称的动态 pytest 方法生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35811800/

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