Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

unittest.skipIf

Skips a test when its supplied condition is true.

Version: Python 3.14; repository image, production runtime not attested

Syntax

@unittest.skipIf(condition, reason)

Skips a test when its supplied condition is true.

A false condition leaves the test active. The condition is evaluated when the decoration is applied.

The expression supplied as condition is evaluated when the method or class is decorated. A true result marks it for skipping with reason. Changing the variable later does not remove that mark. False leaves the test runnable, as in the example with one run, zero skips and a successful result.

import unittest
disabled=True
events=[]
class Example(unittest.TestCase):
    @unittest.skipIf(disabled,'unavailable')
    def test_a(self): events.append('a')
    @unittest.skipIf(False,'disabled')
    def test_b(self): events.append('b')
disabled=False
r=unittest.TestResult()
unittest.defaultTestLoader.loadTestsFromTestCase(Example).run(r)
assert events==['b'] and r.testsRun==2 and len(r.skipped)==1
assert r.skipped[0][1]=='unavailable' and r.wasSuccessful()

Examples

import unittest
class Check(unittest.TestCase):
    @unittest.skipIf(False, "disabled")
    def test_total(self): self.assertEqual(4, 4)

r = unittest.TestResult()
Check("test_total").run(r)
print(r.testsRun, len(r.skipped), r.wasSuccessful())

Result

1 0 True

The expression supplied as condition is evaluated when the method or class is decorated. A true result marks it for skipping with reason. Changing the variable later does not remove that mark. False leaves the test runnable, as in the example with one run, zero skips and a successful result.

Sources