Python Pytest-错误vs失败

Python Pytest-错误vs失败,python,testing,pytest,python-unittest,Python,Testing,Pytest,Python Unittest,我正在从PyUnit迁移到Pytest,我发现Pytest与PyUnit不同,在运行测试(打印点)时,不会在快速报告中区分测试报告中的失败和错误。如何教你做这件事 更新 似乎它只对使用Pytest执行的PyUnit测试有效,这要感谢提供的线索 代码: import unittest class TestErrorFail(unittest.TestCase): def test_error(self): raise Exception('oops') def

我正在从PyUnit迁移到Pytest,我发现Pytest与PyUnit不同,在运行测试(打印点)时,不会在快速报告中区分测试报告中的失败和错误。如何教你做这件事

更新 似乎它只对使用Pytest执行的PyUnit测试有效,这要感谢提供的线索

代码:

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 ==============================
输出:

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 ==============================
============================================================测试会话开始=================================
平台linux2--Python2.7.3--py-1.4.20--pytest-2.5.2
插件:django
收集2项
一些测试
=========================================================故障======================================
______________________________TestErrorFail.test\u错误______________________________
自我=
def测试_错误(自身):
>引发异常('oops')
E异常:oops
sometests.py:5:异常
______________________________测试仪错误。测试失败_______________________________
自我=
def测试失败(自身):
>self.assertTrue(False)
断言者错误:False不是true
一些测试。py:8:AssertionError
===========================================2在0.69秒内失败==============================

据我所知,Py.TestCo确实区分了错误和错误,请考虑这个例子:

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 ================
更新

但是请注意,py.test将异常期间引发的任何异常视为正常故障。这实际上是一件好事,通常您希望能够通过测试,但异常不是AssertionError(或其子类)。在上面的示例中,您会发现错误条件是通过在夹具中引发异常而不是在测试期间触发的


然而,在UnitTest类中尝试这一点,结果表明在
.setUp()
方法中引发异常会导致失败,而不是错误。这可能是一个bug,如果您愿意,可以这样报告。

对于pytest,测试函数中抛出的任何未捕获异常都是失败的,包括但不限于断言错误

错误是为夹具中的故障保留的。
在命名的中(如flub的示例中)或中,未捕获的异常会导致错误而不是失败

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

请注意,即使异常发生在拆卸过程中,也会发生错误 在本例中,您完成了测试,并且拆卸以某种方式失败