Overview
Pyrig enforces a structured approach to testing that ensures comprehensive test coverage and maintainability. The test infrastructure is built on pytest and provides automatic fixture discovery, test skeleton generation, and project validation.Test Directory Structure
Tests mirror the source code structure with thetest_ prefix:
Naming Conventions
Pyrig follows strict naming conventions:Modules
- Source:
my_module.py - Test:
test_my_module.py
Functions
- Source:
def calculate(): - Test:
def test_calculate():
Classes
- Source:
class DataProcessor: - Test:
class TestDataProcessor:
Methods
- Source:
def process(self): - Test:
def test_process(self):
Test Skeleton Generation
Pyrig automatically generates test skeletons for untested code. This is enforced by theassert_all_modules_tested autouse fixture.
Automatic Generation
When you run tests, pyrig:- Scans all source modules for functions, classes, and methods
- Checks for corresponding tests using naming conventions
- Generates skeletons for missing tests
- Fails the test run with a list of generated files
Example
Given this source code:pytest generates this test skeleton:
Manual Generation
Generate test skeletons manually using themktests command:
Mirror Testing Pattern
The mirror testing pattern ensures every source module has a corresponding test module. This is implemented by theMirrorTestConfigFile class.
How It Works
- Module Discovery - Scans all source modules
- Path Derivation - Calculates test file path from source path
- Content Analysis - Identifies untested functions/classes/methods
- Skeleton Generation - Creates test skeletons for missing tests
- Non-Destructive Merge - Preserves existing test code
Example: Creating Mirror Tests
Batch Processing
Generate tests for multiple modules:Generate Tests for Entire Package
Test Configuration
Pyrig uses pytest plugins for automatic fixture discovery.conftest.py
Every project has atests/conftest.py that loads pyrig’s test infrastructure:
- Automatic fixture discovery
- Autouse fixtures for validation
- Test utilities and helpers
Fixture Discovery
Pyrig automatically discovers fixtures from:- pyrig’s fixtures - Built-in fixtures from
pyrig.rig.tests.fixtures - Dependent package fixtures - Fixtures from packages depending on pyrig
- Project fixtures - Fixtures in your project’s
tests/directory
Writing Tests
Basic Test
Testing Classes
Using Fixtures
Testing ConfigFiles
Use theconfig_file_factory fixture to test ConfigFile subclasses:
Running Tests
Run All Tests
Run Specific Tests
Run Tests by Pattern
Run Tests with Markers
Test Organization
Group Related Tests
Use Fixtures for Setup
Test Coverage Enforcement
Pyrig’s autouse fixtures enforce test coverage:assert_all_modules_tested- Every module must have a test moduleassert_root_is_correct- Project structure must be correctassert_no_namespace_packages- All packages must have__init__.pyassert_src_does_not_use_rig- Source code cannot import rig code
Next Steps
Autouse Fixtures
Learn about automatic test validation
Best Practices
Testing best practices and patterns