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
