unittest is Python’s standard-library framework for organizing tests, checking program behavior, and reporting failures. A test suite is a class whose test methods begin with test_. section 36.0 section 36.1
A basic unittest workflow is:
- Import unittest and create a TestCase subclass for the tests. section 36.1
- Write descriptive test_ methods and use assertions such as assertEqual and assertNotEqual. section 36.1
- Read failure output for the failing suite, method, source line, expected value, and received value. section 36.1
- Keep program code and tests in separate files, then rerun tests after each incremental change. section 36.2
Testing functions
A module can define functions such as is_prime and absolute_value, while a separate test file imports that module and checks several inputs. For example, test_prime_5 expects is_prime(5) to be True, and test_abs_neg5 expects absolute_value(-5) to be 5. When a buggy function returns None instead of 0, unittest identifies the exact test and shows “None != 0.”



