使用pytest记录成功的测试用例断言

时间:2019-02-05 09:04:25

标签: python pytest

当pytest测试失败时,它为我提供了一个很好的描述,确切说明了测试失败的确切位置以及确切的值。

虽然这对于失败的测试确实非常有用,但我也想从没有失败的测试中获得这份报告。

例如:

    def test_one():
        a =  0
>       assert 0 < a <= 1  # to see what was printed
E       assert 0 < 0 <= 1

test_sample.py:6: AssertionError
--------------------------- Captured stdout call ---------------------------
first
1 failed in 0.12 seconds

如果测试没有失败,我也希望看到'a'的值。

例如:

    def test_one():
        a = 0.5
>       assert 0 < a <= 1  # to see what was printed
E       assert 0 < 0.5 <= 1

test_sample.py:6: AssertionSuccess
--------------------------- Captured stdout call ---------------------------
first
1 passed in 0.12 seconds

1 个答案:

答案 0 :(得分:-1)

我想,第一个测试用例中a的值为0。 那是因为您指定了

def test_one():
    a=0

此外,您还应该在参数中添加self以执行测试。

当我们通过将a的值设置为0进行测试时,比较时会发生什么,即a小于1还是大于0。两者都将结果设为false。因此,它给出了一个错误。

第二种情况,

def test_one():
    a=0.5

当我们进行比较时,无论a小于1还是大于0.5,都会发生什么。两者都使结果正确。因此,它可以使测试成功通过。

--------编辑-------- 您可以添加记录器以获取记录详细信息。

import logging
# Your code
logger = logging.logger()
logging.debug(a)

希望能有所帮助! :)

相关问题