Python Glossary Русский

all Python 3unittestfunction

unittest.skipIf()

skipping tests

A decorator that skips a test when a condition is true, for example on an unsuitable OS or Python version.

Syntax

@unittest.skipIf(condition, reason)

The condition is evaluated once, when the class is defined, that is at import time, not right before the test runs: it cannot see a value that appears later in setUp or another test. For a check at run time use self.skipTest(reason). Unlike skip, the reason is required; leaving it out raises TypeError at import time.

Examples

import sys
import unittest
class Tests(unittest.TestCase):
    @unittest.skipIf(sys.version_info < (99, 0), 'нужен Python 99')
    def test_future(self):
        pass
    def test_now(self):
        pass
result = unittest.TestResult()
unittest.TestLoader().loadTestsFromTestCase(Tests).run(result)
print(result.testsRun, len(result.skipped))  # → 2 1

See also

Open in the full glossarydocs.python.orgReport an inaccuracy