CPython's testing infrastructure is a multi-layered system designed to ensure the stability of the Python interpreter and its standard library across diverse platforms. The system ranges from low-level test support utilities to a comprehensive regression suite runner and the standard unittest framework.
The primary entry point for running the full suite is the test package, typically invoked via python -m test Lib/test/libregrtest/cmdline.py10-12
The testing stack is organized into three main layers:
libregrtest, which manages test discovery, execution environment, and parallelization Lib/test/libregrtest/main.py33-55unittest and unittest.mock libraries Lib/test/test_support.py18 Lib/unittest/mock.py1-24test.support package provides CPython-specific utilities for handling resources, platform-specific skips, and environment isolation Lib/test/test_support.py21-28The following diagram maps high-level testing concepts to their implementation entities in the codebase.
Testing Infrastructure Entity Map
Sources: Lib/test/libregrtest/main.py33-55 Lib/test/libregrtest/cmdline.py14-15 Lib/test/test_support.py21-28 Lib/test/test_regrtest.py28-34
The libregrtest framework is the engine behind CPython's regression testing. It is responsible for finding tests (usually files prefixed with test_ in Lib/test/) and executing them while monitoring for side effects like memory leaks or environment changes Lib/test/libregrtest/main.py167-184 Lib/test/libregrtest/utils.py142-187
Key features include:
-j flag Lib/test/libregrtest/main.py93-101-R flag, which initializes HuntRefleak to monitor sys.gettotalrefcount() across multiple runs Lib/test/libregrtest/main.py111-116 Lib/test/libregrtest/runtests.py20setup_process and setup_test_dir Lib/test/libregrtest/main.py21 Lib/test/libregrtest/utils.py188-251setup_pgo_tests Lib/test/libregrtest/pgo.py17 or Thread Sanitizer (TSAN) tests via setup_tsan_tests Lib/test/libregrtest/tsan.py23For details, see Regression Test Runner.
test.support)The test.support package is a collection of specialized tools required for testing a language interpreter. It abstracts away platform differences and provides decorators to skip tests based on available features.
| Utility Type | Examples | Purpose |
|---|---|---|
| Resource Guards | requires_working_socket | Ensures tests only run if specific hardware or network access is enabled Lib/test/test_support.py164 |
| Platform Checks | is_android, is_apple_mobile | Identifies the execution environment to apply platform-specific logic Lib/test/test_support.py100 |
| Environment | temp_dir, unlink, rmtree | Isolates I/O and temporary file creation Lib/test/test_support.py116-145 Lib/test/test_support.py184-198 |
| Module Handling | import_module, unload, forget | Manages sys.modules state to ensure clean imports Lib/test/test_support.py87-93 Lib/test/test_support.py110-115 |
Sources: Lib/test/test_support.py21-28 Lib/test/libregrtest/utils.py34-35 Lib/test/libregrtest/utils.py6
CPython uses the standard unittest framework as its base. For isolating components, the unittest.mock library provides Mock, MagicMock, and AsyncMock to replace parts of the system Lib/unittest/mock.py7-24 The patch utility handles the temporary replacement of module and class level attributes Lib/unittest/mock.py10
The interaction between the runner and the framework is depicted below:
Execution Flow: Runner to TestCase
Sources: Lib/test/libregrtest/main.py33-110 Lib/test/libregrtest/result.py18-20 Lib/test/test_support.py18 Lib/unittest/mock.py1-24
For details, see unittest Framework and Mock Library.
Beyond Python-level tests, CPython includes specialized C extension modules to test internal APIs that are not exposed to regular Python code.
_testcapi: Provides get_process_memory_usage to track memory footprint during regression tests Lib/test/libregrtest/utils.py23-25_colorize: Used by the test runner to provide colorized output for test results Lib/test/libregrtest/main.py9difflib: Used extensively in testing for generating human-readable deltas between expected and actual output Lib/difflib.py1-27Sources: Lib/test/libregrtest/utils.py23-25 Lib/test/libregrtest/main.py9 Lib/difflib.py1-27
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.