CKTIS / Python
unittest.skip
Marks a test to be skipped unconditionally and records the reason.
Version: Python 3.14; repository image, production runtime not attested
Syntax
@unittest.skip(reason)Marks a test to be skipped unconditionally and records the reason.
A skipped test body does not execute. The result still counts the attempted test and lists the skip separately.
Decorate a test method or a test class with a reason. For a method marked before execution, its setUp, body and tearDown are skipped. The default result counts the test and records (test, reason) in skipped without a failure. The sample prints 1 offline 0. Skipping one test method leaves the class fixture active. Skipping the class omits setUpClass and tearDownClass as well.
import unittest
calls=[]
class Example(unittest.TestCase):
def setUp(self): calls.append('up')
def tearDown(self): calls.append('down')
@unittest.skip('offline')
def test_network(self): calls.append('body')
r=unittest.TestResult()
case=Example('test_network')
case.run(r)
assert not calls and r.skipped==[(case,'offline')]
assert r.testsRun==1 and r.wasSuccessful()
class Individual(Example):
@classmethod
def setUpClass(cls): calls.append('class')
@classmethod
def tearDownClass(cls): calls.append('after')
@unittest.skip('whole')
class Whole(Individual): pass
for cls in (Individual,Whole):
unittest.defaultTestLoader.loadTestsFromTestCase(cls).run(unittest.TestResult())
assert calls==['class','after']
Examples
import unittest
class Check(unittest.TestCase):
@unittest.skip("offline")
def test_network(self): self.fail("ran")
r = unittest.TestResult()
Check("test_network").run(r)
print(r.testsRun, r.skipped[0][1], len(r.failures))
Result
1 offline 0
Decorate a test method or a test class with a reason. For a method marked before execution, its setUp, body and tearDown are skipped. The default result counts the test and records (test, reason) in skipped without a failure. The sample prints 1 offline 0. Skipping one test method leaves the class fixture active. Skipping the class omits setUpClass and tearDownClass as well.