简体   繁体   中英

Python change pytest tests format for print

I'm looking to change the way that pytest prints tests results to the screen.

This is my code:

@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

Right now, when I use "pytest -v -s" in the terminal, the output looks like this:

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

I want the output to look like this:

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

I know that I can use "ids=['4~3: True',...]" to set it manually for each of the tests, but since I'll be working with many tests, I was hoping there is an easier way than that to do it.

Also, is there an options to get an output like this?

>  test_check_somethingv2[4-3: True] PASSED

One way is to write a wrapper around pytest.param , for example:

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

This results in the following:

$ 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 =============================

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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