Pytest - error vs fail

Gill Bates picture Gill Bates · Feb 2, 2014 · Viewed 12.8k times · Source

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

Answer

Okken picture Okken · Aug 19, 2015

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.