This document explains how to run tests for the wrapt project and the conventions used for Python version-specific test files.
The tests/core/ directory contains all test files for the wrapt project. Test files follow these conventions:
- Standard test files:
test_*.py- These run on all supported Python versions - Python version-specific test files:
test_*_pyXX.py- These only run on specific Python versions or later
The project uses a special naming convention to ensure certain tests only run on appropriate Python versions. This is handled automatically by the tests/conftest.py configuration.
Test files can include version suffixes to indicate minimum Python version requirements:
test_name_py3.py- Runs only on Python 3.x (skipped on Python 2.x)test_name_py2.py- Runs only on Python 2.x (skipped on Python 3.x)test_name_py36.py- Runs only on Python 3.6 and latertest_name_py37.py- Runs only on Python 3.7 and latertest_name_py38.py- Runs only on Python 3.8 and later
test_class_py37.py- Tests class-related features that require Python 3.7+test_descriptors_py36.py- Tests descriptor features that require Python 3.6+test_inheritance_py37.py- Tests inheritance features that require Python 3.7+test_adapter_py33.py- Tests adapter features that require Python 3.3+ (keyword-only arguments)
The tests/conftest.py file contains a pytest_pycollect_makemodule hook that:
- Parses the filename for version patterns using regex:
_py(\d)(\d*) - Compares the detected version requirement with the current Python version
- Returns a dummy collector (skips the test) if the current version is too old
- Allows normal test collection if the version requirement is met
The project provides several ways to run tests using the just command runner.
just testThis runs the complete test suite across all supported Python versions (3.9-3.14). For each version, it tests three scenarios:
- Without C extensions
- With C extensions
- With C extensions disabled at runtime
just test-version 3.13Replace 3.13 with any supported Python version (3.9, 3.10, 3.11, 3.12, 3.13, 3.14).
just test-toxRuns tests using the traditional tox configuration.
just test-pipRuns tests using pip-installed pytest in the main virtual environment.
just test-mypyRuns mypy type checking across all supported Python versions (3.9-3.14).
just test-mypy-version 3.13Runs mypy type checking for a specific Python version. Replace 3.13 with any supported version.
Each test-version run includes three important test scenarios:
-
Without C Extensions (
WRAPT_INSTALL_EXTENSIONS=false)- Tests the pure Python implementation
- Ensures the fallback code works correctly
-
With C Extensions (
WRAPT_INSTALL_EXTENSIONS=true)- Tests the optimized C implementation
- Verifies performance-critical paths
-
C Extensions Disabled at Runtime (
WRAPT_DISABLE_EXTENSIONS=true)- Tests the ability to disable C extensions after installation
- Useful for debugging and compatibility testing
WRAPT_INSTALL_EXTENSIONS=true/false- Controls whether C extensions are built during installationWRAPT_DISABLE_EXTENSIONS=true/false- Controls whether C extensions are used at runtime
The project includes custom pytest handlers for testing mypy type checking behavior. These tests ensure that the wrapt library's type annotations work correctly and produce expected mypy error messages. Note that mypy must exist in your PATH else the tests related to type checking which be skipped.
Mypy tests follow a specific naming pattern in the tests/mypy/ directory:
- Test files:
mypy_*.py- Python files containing code to be type-checked - Expected output files:
mypy_*.out- Files containing the expected mypy output
The custom test handler in conftest.py automatically discovers pairs of mypy_*.py and mypy_*.out files and:
- Runs
mypy --show-error-codes --python-version X.Yon the.pyfile - Compares the actual output with the expected output in the
.outfile - Fails the test if the outputs don't match
These tests only run on Python 3.9+ to ensure consistent mypy behavior.
To create a new mypy test case:
-
Create the test file: Write a Python file with the code you want to type-check:
# Create tests/mypy/mypy_your_test_name.py -
Review output from test: Review the test output to ensure it contains the expected error messages and type information:
just view-mypy-test mypy_your_test_name
-
Save the expected output: Run mypy to capture the expected output, by running:
just save-mypy-test mypy_your_test_name
-
Verify the output: Check the output from running the test against expected output, by running:
just check-mypy-test mypy_your_test_name
-
Run the test: The test will automatically be discovered and run with pytest:
uv run pytest tests/ -k mypy_your_test_name
The existing mypy_function_wrapper.py test demonstrates type checking for FunctionWrapper:
from wrapt import FunctionWrapper
def f(a: bool, b: str) -> int:
return 1
def standard_wrapper(wrapped, instance, *args, **kwargs):
pass
f1 = FunctionWrapper(f, standard_wrapper)
reveal_type(f1) # Should reveal the original function's type
result1a: int = f1(True, "test") # Valid usage
result1b: str = f1(1, None) # Invalid usage - should errorThe corresponding mypy_function_wrapper.out file contains the expected mypy output, including:
- Type revelation notes
- Assignment errors
- Argument type errors
- Error codes for each issue
This approach ensures that the wrapt library's type annotations behave consistently and provide helpful error messages to users.
For quick testing during development, you can:
-
Run all tests against all Python versions:
just test -
Test a specific Python version you're working with:
just test-version 3.11
-
Run legacy tests if needed:
just test-tox
-
Run type checking across all Python versions:
just test-mypy
-
Run type checking for a specific Python version:
just test-mypy-version 3.11
When adding new test files:
- Standard unit tests: Use the
test_*.pynaming convention for general tests - Version-specific tests: Use the
test_*_pyXX.pyconvention if your test requires features from a specific Python version - Mypy type tests: Use the
mypy_*.pynaming convention for type checking tests (see the Mypy Type Checking Tests section above) - The version detection is automatic - no additional configuration needed
Test dependencies are managed in pyproject.toml. The main requirements are:
pytest- For running unit tests
If running tox or mypy tests, it is assumed that these are installed as a tool using uv tool install command (or any other available method) and available in your PATH.
The project uses GitHub Actions for automated testing. Tests are triggered on:
- Release tags
- Pull requests
- Workflow dispatch
The CI system runs the full test suite across all supported Python versions and platforms.