unittest
The Python unittest
module provides a framework for creating and running unit tests, allowing developers to ensure their code behaves as expected.
It supports test automation, sharing setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework.
Here’s a quick example:
>>> import unittest
>>> class TestSum(unittest.TestCase):
... def test_sum(self):
... self.assertEqual(sum([1, 2, 3]), 6)
...
>>> unittest.main(exit=False)
Key Features
- Organizes tests into test suites
- Supports test fixtures for setup and teardown operations
- Provides automated test discovery capabilities
- Includes a wide range of built-in assertions for verifying test results
- Allows for test skipping, expected failures, and subtests
- Enables running tests from the command line or programmatically
- Integrates with test runners, continuous integration (CI) tools, and IDEs for comprehensive test reporting
Frequently Used Classes and Functions
Object | Type | Description |
---|---|---|
unittest.TestCase |
Class | Serves as the base class for creating new test cases |
unittest.main() |
Function | Runs a set of tests as a command-line program |
unittest.TestCase.assertEqual() |
Method | Checks whether two objects are equal |
unittest.TestCase.setUp() |
Method | Prepares the test fixture before each test method runs |
unittest.TestCase.tearDown() |
Method | Cleans up after the test method runs |
unittest.TestLoader |
Class | Loads test cases from modules, classes, or test methods |
Examples
Creating a test case:
>>> import unittest
>>> class TestMax(unittest.TestCase):
... def test_max(self):
... self.assertEqual(max([1, 5, 3]), 5)
...
Using .setUp()
and .tearDown()
methods for test preparation and cleanup:
>>> class TestExample(unittest.TestCase):
... def setUp(self):
... self.value = 5
...
... def tearDown(self):
... self.value = 0
...
... def test_increment(self):
... self.value += 1
... self.assertEqual(self.value, 6)
...
Common Use Cases
- Writing and organizing unit tests for functions and classes
- Automating test runs with CI tools
- Ensuring code changes do not break existing functionality
- Documenting the expected behavior of code through test cases
- Testing edge cases and error handling
- Writing regression tests to prevent bugs from reappearing
- Refactoring code confidently by ensuring all tests pass
Real-World Example
Say that you’re building a basic calculator and want to test it to ensure it works correctly. Here’s how you could use the unittest
module to achieve this:
>>> import unittest
>>> def add(a, b):
... return a + b
...
>>> class TestCalculator(unittest.TestCase):
... def test_add(self):
... self.assertEqual(add(2, 3), 5)
... self.assertEqual(add(-1, 1), 0)
... self.assertEqual(add(-1, -1), -2)
...
>>> unittest.main(exit=False)
In this example, you use the unittest
module to verify that the add()
function produces the expected results for different input values, ensuring reliability and correctness.
Related Resources
Tutorial
Python's unittest: Writing Unit Tests for Your Code
In this tutorial, you'll learn how to use the unittest framework to create unit tests for your Python code. Along the way, you'll also learn how to create test cases, fixtures, test suites, and more.
For additional information on related topics, take a look at the following resources:
- Python's assert: Debug and Test Your Code Like a Pro (Tutorial)
- Effective Python Testing With pytest (Tutorial)
- Python's doctest: Document and Test Your Code at Once (Tutorial)
- Python's unittest: Writing Unit Tests for Your Code (Quiz)
- Using Python's assert to Debug and Test Your Code (Course)
- Testing Your Code With pytest (Course)
- Effective Testing with Pytest (Quiz)
By Leodanis Pozo Ramos • Updated July 29, 2025