Python Glossary Русский

all Python 3unittestfunction

unittest.expectedFailure()

skipping tests

A decorator that marks a test as known to fail: its failure does not spoil the run, while an unexpected success counts as a problem.

Syntax

@unittest.expectedFailure
def test_known_bug(self): ...

It is a marker for a known bug that is not fixed yet. The catch: once the bug is fixed, the test starts passing and reports an 'unexpected success', which since Python 3.4 makes wasSuccessful() false, so the run turns red and the decorator must be removed. It swallows any exception, not just AssertionError, so a typo inside the test also passes as an 'expected failure'.

Examples

import unittest
class Tests(unittest.TestCase):
    @unittest.expectedFailure
    def test_known_bug(self):
        self.assertEqual(round(2.5), 3)
    @unittest.expectedFailure
    def test_already_fixed(self):
        self.assertEqual(round(3.5), 4)
result = unittest.TestResult()
unittest.TestLoader().loadTestsFromTestCase(Tests).run(result)
print(len(result.expectedFailures), len(result.unexpectedSuccesses))  # → 1 1
print(result.wasSuccessful())  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy