-
Notifications
You must be signed in to change notification settings - Fork 2
16. Skipping tests and expected failures
Unittest supports skipping individual test methods and even whole classes of tests. In addition, it supports marking a test as an “expected failure,” a test that is broken and will fail, but shouldn’t be counted as a failure on a TestResult.
Skipping a test is simply a matter of using the skip() decorator or one of its conditional variants, calling TestCase.skipTest() within a setUp() or test method, or raising SkipTest directly.
@unittest.skip("Skip Test")
def test_method2(self):
print("test method2")
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
def test_method3(self):
print("test method3")
@unittest.skipIf(int(obj.strftime("%d"))<15,"Only after 5th you can execute this test")
def test_04(self):
print("test04")
@unittest.expectedFailure
def test_method4(self):
self.assertEqual(33, 27, "not equal")
Unconditionally skip the decorated test. reason should describe why the test is being skipped.
Skip the decorated test if condition is true.
Skip the decorated test unless condition is true.
Mark the test as an expected failure. If the test fails it will be considered a success. If the test passes, it will be considered a failure.
This exception is raised to skip a test.
Usually you can use TestCase.skipTest() or one of the skipping decorators instead of raising this directly.
Skipped tests will not have setUp() or tearDown() run around them. Skipped classes will not have setUpClass() or tearDownClass() run. Skipped modules will not have setUpModule() or tearDownModule() run.
In the setUp method, you can decide to skip a test. If skipped, the test will not be run. Also, the tearDown method will not be run.
def setUp(self):
print("setUP")
currentTest = self.id().split('.')[-1]
if currentTest == 'test_method2':
self.skipTest('reason for skipping')