pytest-错误与失败



im从pyunit迁移到pytest,我发现,与Pyunit不同的Pytest不会在运行测试时快速报告中的测试报告中的失败和错误区分(打印点)。如何教pytest会这样做?

更新

似乎仅适用于使用Pytest执行的Pyunit测试,这要归功于Flub的线索。

代码:

import unittest
class TestErrorFail(unittest.TestCase):
    def test_error(self):
        raise Exception('oops')
    def test_fail(self):
        self.assertTrue(False)

输出:

================================ test session starts =================================
platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2
plugins: django
collected 2 items 
sometests.py FF
====================================== FAILURES ======================================
______________________________ TestErrorFail.test_error ______________________________
self = <sometests.TestErrorFail testMethod=test_error>
    def test_error(self):
>       raise Exception('oops')
E       Exception: oops
sometests.py:5: Exception
______________________________ TestErrorFail.test_fail _______________________________
self = <sometests.TestErrorFail testMethod=test_fail>
    def test_fail(self):
>       self.assertTrue(False)
E       AssertionError: False is not true
sometests.py:8: AssertionError
============================== 2 failed in 0.69 seconds ==============================

对于pytest,在测试功能中抛出的任何未知的例外都是失败,包括但不限于断言错误。

错误用于固定装置中的故障。
在命名的pytest固定装置中,如Flub的示例或Xunit样式设置/拆卸固定装置中的未接收例外,导致错误而不是故障。

我个人喜欢这种区别。
失败表明该测试以某种方式失败。
错误表明您无法进行适当的测试。

请注意,即使在例外拆卸中,也会发生错误。
在这种情况下,您完成了测试,而拆解以某种方式失败了。

在我的知识上,py.test确实区分了失败和错误,请考虑以下示例:

import pytest
def test_fail():
    assert 1 == 2
@pytest.fixture
def fix():
    raise Exception('oops')
def test_error(fix):
    assert fix == 2

运行此测试模块会给一个故障和一个错误:

================ test session starts =========================
platform linux2 -- Python 2.7.5 -- py-1.4.20 -- pytest-2.5.2
plugins: timeout, capturelog, xdist
collected 2 items 
../../tmp/test_foo.py FE
======================= ERRORS ===============================
_________________ ERROR at setup of test_error _______________
    @pytest.fixture
    def fix():
>       raise Exception('oops')
E       Exception: oops
/tmp/test_foo.py:8: Exception
====================== FAILURES ==============================
__________________________ test_fail ____________________________
    def test_fail():
>       assert 1 == 2
E       assert 1 == 2
/tmp/test_foo.py:4: AssertionError
============= 1 failed, 1 error in 0.12 seconds ================

update

注意,py.test认为在例外本身中提出的任何例外是正常的失败。这实际上是一件好事,通常您希望能够在不是一个异常的情况下未能使测试失败,这不是一个essertionerror(或其子类)。在上面的示例中,您会发现错误条件是通过在固定装置中而不是在测试期间提高异常来触发的。

然而,通过Unitest类尝试此操作,事实证明,在.setUp()方法中提出例外确实会导致故障而不是错误。这可能是一个错误,如果您喜欢的话,您可以这样报告。

相关内容

最新更新