簡體   English   中英

Pytest 不在測試報告中顯示元組值

[英]Pytest does not display tuple values in test report

我有一個參數化的 pytest 測試並使用元組作為預期值。

如果我運行測試,這些值不會顯示,自動生成的值 (expected1...expected) 會顯示在報告中。

是否可以在輸出中顯示元組值?

測試樣本:

@pytest.mark.parametrize('test_input, expected',
                         [
                             ('12:00 AM', (0, 0)),
                             ('12:01 AM', (0, 1)),
                             ('11:59 AM', (11, 58))
                         ])
def test_params(test_input, expected):
    assert time_calculator.convert_12h_to_24(test_input) == expected

輸出:

 test_time_converter.py::test_params[12:00 AM-expected0] PASSED test_time_converter.py::test_params[12:01 AM-expected1] PASSED test_time_converter.py::test_params[11:59 AM-expected2] FAILED

如果要查看元組,可以將元組定義為間接參數字符串。 這會調用conftest.py中的夾具,該夾具可以在將字符串傳遞給測試函數之前將它們eval回元組。

它並不完美,但它相當非侵入性且易於理解。

@pytest.mark.parametrize(
    'test_input, expected',
    [
        ('12:00 AM', '(0, 0)'),
        ('12:01 AM', '(0, 1)'),
        ('11:59 AM', '(11, 58)')
    ],
    indirect=["expected"])
def test_params(test_input, expected):
    assert expected != (11, 58)

conftest.py:

@pytest.fixture
def expected(request):
    return eval(request.param)

輸出:

test_print_tuples.py::test_params[12:00 AM-(0, 0)] PASSED
test_print_tuples.py::test_params[12:01 AM-(0, 1)] PASSED 
test_print_tuples.py::test_params[11:59 AM-(11, 58)] FAILED

一些純粹主義者可能會爭辯說使用eval是不好的做法。 如果需要,您可以改用literal_eval 這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM