This comprehensive pytest test suite validates the Trading Platform Configuration Loader across Python 3.9.6+ environments. The test suite ensures robustness through extensive coverage of happy-path scenarios, missing keys validation, invalid enum handling, and edge cases.
| Category | Test Count | Coverage Areas |
|---|---|---|
| Happy Path | 6 tests | Valid configurations, environment resolution, CLI overrides |
| Missing Keys | 6 tests | Missing files, credentials, environment variables, malformed JSON |
| Invalid Enums | 5 tests | Invalid environment types, strategies, log levels, order types |
| Edge Cases | 8 tests | Boundaries, field lengths, regex patterns, URL validation |
| Integration | 3 tests | Full workflows, export functionality, precedence information |
| Utilities | 2 tests | Convenience functions, default configurations |
| Performance | 2 tests | Large datasets, deep nested structures |
| Configuration | 3 tests | Core loader functionality |
Total: 35 comprehensive test cases
- ✅ Loading valid configuration files
- ✅ Environment variable resolution (
${VAR}syntax) - ✅ CLI override precedence
- ✅ Rate limiting configuration
- ✅ Cryptocurrency exchange setup
- ✅ Default configuration creation
- ✅ Non-existent configuration files
- ✅ Missing required credentials (API keys/secrets)
- ✅ Missing individual credential components
- ✅ Malformed JSON configuration
- ✅ Undefined environment variables
- ✅ Invalid environment types (
development,testing,staging,production) - ✅ Invalid backoff strategies (
exponential,linear,fixed) - ✅ Invalid log levels (
DEBUG,INFO,WARNING,ERROR,CRITICAL) - ✅ Invalid order types (
MARKET,LIMIT,STOP, etc.) - ✅ Invalid cryptocurrency types (
spot,margin,future)
- ✅ Minimum/maximum field length validation
- ✅ Rate limiting boundaries (0.1-1000 requests/sec)
- ✅ Risk limits boundaries (0.001-1.0 position size)
- ✅ Port number validation (1-65535)
- ✅ Regex pattern validation (file paths, versions)
- ✅ URL validation for endpoints
- ✅ Empty/null value handling
- ✅ Environment variable default values (
${VAR:-default}) - ✅ Credential auto-discovery via environment prefixes
- ✅ Production environment security requirements
- ✅ Complete configuration workflow
- ✅ Configuration export with sensitive data masking
- ✅ Configuration precedence information
- ✅ Utility function validation
- ✅ Large symbol lists (10,000+ symbols)
- ✅ Deep nested CLI overrides
- Type Annotations: Full support for
Dict[str, Any],Optional,Union - Pydantic v2: Compatible with advanced validation and serialization
- Environment Variables: Robust
os.environhandling - File I/O: Temporary file management with proper cleanup
- JSON Handling: Advanced parsing with error handling
- Mock/Patch: Comprehensive mocking for isolated testing
pip install pytest>=6.2.0
pip install pydantic>=2.0.0
pip install python-dotenv>=0.19.0# Run all tests
python -m pytest test_config_loader.py -v
# Run with detailed output
python -m pytest test_config_loader.py -v --tb=long
# Run specific test category
python -m pytest test_config_loader.py::TestHappyPath -v
python -m pytest test_config_loader.py::TestMissingKeys -v
python -m pytest test_config_loader.py::TestInvalidEnums -v
python -m pytest test_config_loader.py::TestEdgeCases -v# Run with coverage report
python -m pytest test_config_loader.py --cov=config_loader --cov-report=html
# Run specific test method
python -m pytest test_config_loader.py::TestHappyPath::test_load_valid_config_file -v
# Run tests matching pattern
python -m pytest test_config_loader.py -k "environment" -v
# Run tests with parallel execution
python -m pytest test_config_loader.py -n auto# Ensure Python 3.9.6+ is active
python --version
# Install dependencies
pip install -r requirements.txt
# Run tests with environment variables
TEST_MODE=1 python -m pytest test_config_loader.py -vtest_config_loader.py
├── Global Fixtures (temp files, config data)
├── TestHappyPath (6 tests)
├── TestMissingKeys (6 tests)
├── TestInvalidEnums (5 tests)
├── TestEdgeCases (8 tests)
├── TestConfigurationIntegration (3 tests)
├── TestUtilityFunctions (2 tests)
├── TestPerformanceEdgeCases (2 tests)
└── TestConfigurationLoader (base class)
@pytest.fixture
def valid_config_data() -> Dict[str, Any]:
"""Provides valid configuration data for testing"""
@pytest.fixture
def temp_config_file(valid_config_data):
"""Creates temporary JSON config file with cleanup"""
@pytest.fixture
def temp_env_file():
"""Creates temporary .env file with cleanup"""- Credential Validation: API keys (8-128 chars), secrets (16-256 chars)
- Environment Variables: Secure resolution and masking
- Production Requirements: Security and monitoring validation
- Data Masking: Sensitive information protection in exports
- Multi-environment:
development,testing,staging,production - Variable Resolution:
${VAR},${VAR:-default}patterns - Precedence Order: Environment → CLI → Config defaults
- Auto-discovery: Credential detection via prefixes
- Field Constraints: Min/max lengths, numeric boundaries
- Enum Validation: Strict enum value enforcement
- URL/Pattern Validation: WebSocket URLs, file paths, version formats
- Type Safety: Strong typing with Pydantic models
| Exception Type | Test Coverage |
|---|---|
FileNotFoundError |
Missing config files |
ValueError |
Invalid configurations, enum values, credentials |
JSONDecodeError |
Malformed JSON files |
ValidationError |
Pydantic model validation failures |
All error messages are tested for:
- Clarity: Descriptive error descriptions
- Specificity: Exact field and validation failures
- Actionability: Clear guidance on resolution
- Lazy Loading: Fixtures created only when needed
- Resource Cleanup: Automatic temporary file removal
- Memory Efficiency: Large dataset handling without memory leaks
- Fast Execution: Optimized test execution (< 0.2 seconds)
- Large Configurations: 10,000+ symbol lists
- Deep Nesting: Complex hierarchical overrides
- Concurrent Access: Thread-safe configuration loading
# Example GitHub Actions configuration
name: Configuration Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.9, 3.10, 3.11, 3.12]
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install -r requirements.txt
- name: Run tests
run: |
python -m pytest test_config_loader.py -v --cov=config_loader- Follow Naming Convention:
test_<feature>_<scenario> - Use Appropriate Fixtures: Reuse existing fixtures where possible
- Test Boundaries: Include min/max value testing
- Error Scenarios: Test both success and failure paths
- Documentation: Add clear docstrings explaining test purpose
- Isolation: Tests don't depend on external resources
- Repeatability: Tests produce consistent results
- Speed: Individual tests complete in < 50ms
- Clarity: Test names clearly indicate what's being tested
- Coverage: Edge cases and error conditions included
| Issue | Solution |
|---|---|
| Import errors | Ensure config_loader.py is in Python path |
| Fixture not found | Check fixture scope and naming |
| Validation errors | Verify test data matches current schema |
| Temporary file issues | Ensure proper cleanup in fixtures |
# Run with verbose output
python -m pytest test_config_loader.py -v -s
# Run single test with debugging
python -m pytest test_config_loader.py::TestHappyPath::test_load_valid_config_file -v -s --pdb
# Check fixture values
python -m pytest test_config_loader.py --fixtures-per-test- Add Tests: New features require corresponding tests
- Maintain Coverage: Keep test coverage above 95%
- Update Documentation: Include test descriptions
- Run Full Suite: Ensure all tests pass before submission
| Priority | Focus Area |
|---|---|
| P0 (Critical) | Configuration loading, security validation |
| P1 (High) | Environment handling, error scenarios |
| P2 (Medium) | Edge cases, performance |
| P3 (Low) | Convenience features, utilities |
Test Suite Statistics:
- Total Tests: 35
- Test Categories: 8
- Python Compatibility: 3.9.6+
- Execution Time: ~0.1 seconds
- Coverage: Configuration workflow validation
- Maintenance: Automated fixture cleanup