繁体   English   中英

Python更改打印的pytest测试格式

[英]Python change pytest tests format for print

我希望改变 pytest 将测试结果打印到屏幕的方式。

这是我的代码:

@pytest.mark.parametrize('equation, result',
                         [('4-3', True), ('3*(50+2)', True)])
def test_check_somethingv2(equation, result):
    assert equation_validation.check_string_validity(equation) == result

现在,当我在终端中使用“pytest -v -s”时,输出如下所示:

> test_calculator.py::test_check_somethingv2[4-3-True] PASSED

我希望输出看起来像这样:

> test_calculator.py::test_check_somethingv2[4-3: True] PASSED

我知道我可以使用 "ids=['4~3: True',...]" 为每个测试手动设置它,但由于我将处理许多测试,我希望有一个比这更简单的方法来做到这一点。

另外,是否有获得这样的输出的选项?

>  test_check_somethingv2[4-3: True] PASSED

一种方法是围绕pytest.param编写一个包装器,例如:

def eqparam(eq, result):
    return pytest.param(eq, result, id=f'{eq}: {result}')


@pytest.mark.parametrize('equation, result',
                         [eqparam('4-3', True), eqparam('3*(50+2)', True)])
def test_check_somethingv2(equation, result):
    assert equation_validation.check_string_validity(equation) == result

这导致以下结果:

$ pytest --collect-only t.py
============================== test session starts ==============================
platform linux -- Python 3.6.8, pytest-5.3.2, py-1.8.1, pluggy-0.13.1
rootdir: /home/asottile/workspace/pygments-pre-commit
collected 2 items                                                               
<Module t.py>
  <Function test_check_somethingv2[4-3: True]>
  <Function test_check_somethingv2[3*(50+2): True]>

============================= no tests ran in 0.01s =============================

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM