Testing Modules with nf_autoid Encoding
Overview
While Python's import system can use custom codecs for source file encoding, getting it to work reliably with pytest and the test environment is challenging. We've implemented a robust solution that manually processes encoded modules before importing them.
The Challenge
- Python Import System: Python's import mechanism caches encoding detection and doesn't always use registered codecs during test runs.
- Pytest Integration: Pytest has its own module loading mechanism that can conflict with custom encodings.
- Magic Codec: While
magic_codecallows custom encodings, it requires the codec to be registered before any imports of encoded files.
Our Solution
We've created a comprehensive testing approach that:
- External Test Modules: Created real
.pyfiles with# coding: nf_autoidintests/preprocessor_fixtures/ - Manual Processing: Tests manually read, preprocess, and import these modules
- Temporary Files: Processed content is written to temporary files before import
Test Structure
Test Fixtures Directory
tests/preprocessor_fixtures/
├── __init__.py
├── ensure_codec.py # Ensures codec registration
├── simple_module.py # Basic constructs
├── autoscope_module.py # Autoscoped classes
└── edge_cases_module.py # Edge cases and special patterns
Test Implementation
The test_preprocessor_manual_import.py file contains:
- Fixture for Processing:
process_and_importfixture that: - Reads the source file
- Processes it with the preprocessor
- Creates a temporary file with processed content
-
Imports the processed module
-
Comprehensive Tests:
- Simple module imports
- Autoscoped classes
- Edge cases (special names, manual overrides)
- Real-world examples
- Dynamic module creation
Example Test
def test_simple_module(self, process_and_import):
"""Test importing a simple module with preprocessor."""
module_path = Path(__file__).parent / "preprocessor_fixtures" / "simple_module.py"
module = process_and_import(module_path, "test_simple_module")
# Verify preprocessor injected IDs
assert module.module_app.nf_id == "module_app"
assert module.module_stack.nf_id == "module_stack"
Benefits
- Realistic Testing: Tests actual encoded
.pyfiles as users would write them - Isolation: Each test gets a fresh module import
- Debugging: Easy to inspect preprocessed output
- Comprehensive: Covers all edge cases and real-world scenarios
Running the Tests
# Run all manual import tests
pytest tests/test_preprocessor_manual_import.py -v
# Run specific test
pytest tests/test_preprocessor_manual_import.py::TestPreprocessorManualImport::test_simple_module -v
Future Improvements
If Python's import system or pytest adds better support for custom encodings, we could potentially: 1. Register the codec globally in pytest configuration 2. Use import hooks to intercept and preprocess modules 3. Integrate with pytest's module collection system
For now, the manual processing approach provides reliable and comprehensive testing of the preprocessor functionality.