Skip to main content
100%

A gist illustrating testing in Python using unittest

✓ Published0🌍 Public
IIanHopkinson
Last edited Nov 20, 2020
Created on Nov 20, 2020

This example demonstrates how to structure and execute a Python test suite using the built-in `unittest` framework, alongside `doctest` for embedded documentation checks. It shows the definition of a `factorial` function and a `TestFactorial` class that validates expected outputs, error handling, and edge cases. The code dynamically generates test methods via `setattr` and uses `subTest` for granular failure reporting. It also illustrates skipping tests with `@unittest.skip`, suppressing console logging via `logging.NullHandler`, and controlling output with `unittest.main(buffer=True)`. The visible data source is the hardcoded test case list and doctest examples within the function docstring.

AI-generated description

Similar vizzes