Skip to content

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

  1. Python Import System: Python's import mechanism caches encoding detection and doesn't always use registered codecs during test runs.
  2. Pytest Integration: Pytest has its own module loading mechanism that can conflict with custom encodings.
  3. Magic Codec: While magic_codec allows 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:

  1. External Test Modules: Created real .py files with # coding: nf_autoid in tests/preprocessor_fixtures/
  2. Manual Processing: Tests manually read, preprocess, and import these modules
  3. 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:

  1. Fixture for Processing: process_and_import fixture that:
  2. Reads the source file
  3. Processes it with the preprocessor
  4. Creates a temporary file with processed content
  5. Imports the processed module

  6. Comprehensive Tests:

  7. Simple module imports
  8. Autoscoped classes
  9. Edge cases (special names, manual overrides)
  10. Real-world examples
  11. 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

  1. Realistic Testing: Tests actual encoded .py files as users would write them
  2. Isolation: Each test gets a fresh module import
  3. Debugging: Easy to inspect preprocessed output
  4. 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.