25.3. unittest — Unit testing framework
pythontestingunittestsoftware-engineering
Abstraction: Python's built-in xUnit testing framework API and concepts
Key points:
- Core concepts: test case (single scenario), test fixture (setUp/tearDown setup code), test suite (collection), test runner (executor)
- Tests are methods named with
test_prefix inside TestCase subclasses; assertEqual, assertTrue, assertRaises are the main assertion methods - Test discovery (Python 2.7+):
python -m unittest discoverrecursively findstest*.pyfiles from a start directory - Decorators @skip, @skipIf, @skipUnless, @expectedFailure control conditional test execution without removing tests
- setUpClass/tearDownClass (class-level) and setUpModule/tearDownModule (module-level) fixtures avoid redundant setup when many tests share state
Connections: Python · Unit Testing · Software Testing · Test Driven Development