Pytest - error vs fail

走远了吗. 提交于 2020-05-26 10:37:14

问题


Im migrating from PyUnit to Pytest, and I found, that Pytest, unlike PyUnit, does not distinguish fails and errors in test report in quick report while running tests (where dots are printed). How to teach Pytest do do it?

UPDATE

Seems like it is valid only for PyUnit tests executed with Pytest, thanks to flub for the clue.

Code:

import unittest

class TestErrorFail(unittest.TestCase):
    def test_error(self):
        raise Exception('oops')

    def test_fail(self):
        self.assertTrue(False)

Output:

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

回答1:


For pytest, any uncaught exception thrown in a test function is a failure, including but not limited to assertion errors.

Error is reserved for a failure in a fixture.
Uncaught exceptions in a named pytest fixture, as in flub's example, or in xUnit style setup/teardown fixtures, result in an Error instead of a failure.

I personally like the distinction.
A Failure indicates that the test failed in some way.
An Error indicates that you couldn't get to the point of doing a proper test.

Note that an Error will happen even in the case where the exception is in the teardown.
In this case, you completed the test, and the teardown failed in some way.




回答2:


To the best of my knowledge py.test does distinguish failures and errors, consider this example:

import pytest

def test_fail():
    assert 1 == 2

@pytest.fixture
def fix():
    raise Exception('oops')

def test_error(fix):
    assert fix == 2

Running this test module gives one failure and one error:

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

Note however that py.test considers any exception raised during the exception itself a normal failure. This is actually a good thing, generally you want to be able to fail your test with an exception which is not an AssertionError (or subclass thereof). In the example above you will find that the error condition is triggered by raising the exception in the fixture rather then during the test.

However trying this out with a UnitTest class it turns out that raising an exception in the .setUp() method is does result in a failure rather then error. This is probably a bug and you could report it as such if you like.



来源:https://stackoverflow.com/questions/21513718/pytest-error-vs-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!