簡體   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