Skipping Tests and Expected Failures
Skipping tests
Skip a test when:
- a dependency isnβt available
- a feature is not supported on the OS
skip_examples.py
import sys
import unittest
class TestPlatform(unittest.TestCase):
@unittest.skipIf(sys.platform.startswith("win"), "Not supported on Windows")
def test_linux_only_behavior(self):
self.assertTrue(True)
@unittest.skip("Temporarily disabled")
def test_disabled(self):
self.assertTrue(False)skip_examples.py
import sys
import unittest
class TestPlatform(unittest.TestCase):
@unittest.skipIf(sys.platform.startswith("win"), "Not supported on Windows")
def test_linux_only_behavior(self):
self.assertTrue(True)
@unittest.skip("Temporarily disabled")
def test_disabled(self):
self.assertTrue(False)Expected failures
Use when:
- you want to keep a failing test visible
- youβre tracking known issues
expected_failure.py
import unittest
class TestKnownBug(unittest.TestCase):
@unittest.expectedFailure
def test_buggy_behavior(self):
self.assertEqual(1, 2)expected_failure.py
import unittest
class TestKnownBug(unittest.TestCase):
@unittest.expectedFailure
def test_buggy_behavior(self):
self.assertEqual(1, 2)Tip
Donβt overuse skipsβtreat them as temporary.
π§ͺ Try It Yourself
Exercise 1 β Write a unittest TestCase
Exercise 2 β assertRaises
Exercise 3 β setUp and tearDown
If this helped you, consider buying me a coffee β
Buy me a coffeeWas this page helpful?
Let us know how we did
