|
| 1 | +# Testing Guide |
| 2 | + |
| 3 | +This document outlines the testing standards and practices for the Helm Values Manager project. |
| 4 | + |
| 5 | +## Test Organization |
| 6 | + |
| 7 | +``` |
| 8 | +tests/ |
| 9 | +├── unit/ # Unit tests |
| 10 | +│ ├── models/ # Tests for core models |
| 11 | +│ │ └── test_value.py |
| 12 | +│ └── backends/ # Tests for backend implementations |
| 13 | +├── integration/ # Integration tests |
| 14 | +│ └── backends/ # Backend integration tests |
| 15 | +└── conftest.py # Shared test fixtures |
| 16 | +``` |
| 17 | + |
| 18 | +## Test Categories |
| 19 | + |
| 20 | +### Unit Tests |
| 21 | + |
| 22 | +Unit tests focus on testing individual components in isolation. They should: |
| 23 | +- Test a single unit of functionality |
| 24 | +- Mock external dependencies |
| 25 | +- Be fast and independent |
| 26 | +- Cover both success and error cases |
| 27 | + |
| 28 | +Example from `test_value.py`: |
| 29 | +```python |
| 30 | +def test_value_init_local(): |
| 31 | + """Test Value initialization with local storage.""" |
| 32 | + value = Value(path="app.replicas", environment="dev") |
| 33 | + assert value.path == "app.replicas" |
| 34 | + assert value.environment == "dev" |
| 35 | + assert value.storage_type == "local" |
| 36 | +``` |
| 37 | + |
| 38 | +### Integration Tests |
| 39 | + |
| 40 | +Integration tests verify component interactions, especially with external services: |
| 41 | +- Test actual backend implementations |
| 42 | +- Verify configuration loading |
| 43 | +- Test end-to-end workflows |
| 44 | + |
| 45 | +## Testing Standards |
| 46 | + |
| 47 | +### 1. Test Organization |
| 48 | +- Group related tests in classes |
| 49 | +- Use descriptive test names |
| 50 | +- Follow the file structure of the implementation |
| 51 | +- Keep test files focused and manageable |
| 52 | + |
| 53 | +### 2. Test Coverage |
| 54 | +- Aim for 100% code coverage |
| 55 | +- Test all code paths |
| 56 | +- Include edge cases |
| 57 | +- Test error conditions |
| 58 | + |
| 59 | +### 3. Test Quality |
| 60 | +- Follow Arrange-Act-Assert pattern |
| 61 | +- Keep tests independent |
| 62 | +- Use appropriate assertions |
| 63 | +- Write clear test descriptions |
| 64 | + |
| 65 | +Example: |
| 66 | +```python |
| 67 | +def test_set_invalid_type(): |
| 68 | + """Test setting a non-string value.""" |
| 69 | + value = Value(path="app.replicas", environment="dev") |
| 70 | + with pytest.raises(ValueError, match="Value must be a string"): |
| 71 | + value.set(3) |
| 72 | +``` |
| 73 | + |
| 74 | +### 4. Fixtures and Mocks |
| 75 | +- Use fixtures for common setup |
| 76 | +- Mock external dependencies |
| 77 | +- Keep mocks simple and focused |
| 78 | +- Use appropriate scoping |
| 79 | + |
| 80 | +Example: |
| 81 | +```python |
| 82 | +@pytest.fixture |
| 83 | +def mock_backend(): |
| 84 | + """Create a mock backend for testing.""" |
| 85 | + backend = Mock(spec=ValueBackend) |
| 86 | + backend.get_value.return_value = "mock_value" |
| 87 | + return backend |
| 88 | +``` |
| 89 | + |
| 90 | +## Running Tests |
| 91 | + |
| 92 | +### Local Development |
| 93 | +```bash |
| 94 | +# Run all tests |
| 95 | +python -m pytest |
| 96 | + |
| 97 | +# Run specific test file |
| 98 | +python -m pytest tests/unit/models/test_value.py |
| 99 | + |
| 100 | +# Run with coverage |
| 101 | +python -m pytest --cov=helm_values_manager |
| 102 | +``` |
| 103 | + |
| 104 | +### Using Tox |
| 105 | +```bash |
| 106 | +# Run tests in all environments |
| 107 | +tox |
| 108 | + |
| 109 | +# Run for specific Python version |
| 110 | +tox -e py39 |
| 111 | +``` |
| 112 | + |
| 113 | +## Test Documentation |
| 114 | + |
| 115 | +Each test should have: |
| 116 | +1. Clear docstring explaining purpose |
| 117 | +2. Well-structured setup and teardown |
| 118 | +3. Clear assertions with messages |
| 119 | +4. Proper error case handling |
| 120 | + |
| 121 | +Example: |
| 122 | +```python |
| 123 | +def test_from_dict_missing_path(): |
| 124 | + """Test deserializing with missing path field.""" |
| 125 | + data = { |
| 126 | + "environment": "dev", |
| 127 | + "storage_type": "local", |
| 128 | + "value": "3" |
| 129 | + } |
| 130 | + with pytest.raises(ValueError, match="Missing required field: path"): |
| 131 | + Value.from_dict(data) |
| 132 | +``` |
| 133 | + |
| 134 | +## Best Practices |
| 135 | + |
| 136 | +1. **Test Independence** |
| 137 | + - Each test should run in isolation |
| 138 | + - Clean up after tests |
| 139 | + - Don't rely on test execution order |
| 140 | + |
| 141 | +2. **Test Readability** |
| 142 | + - Use clear, descriptive names |
| 143 | + - Document test purpose |
| 144 | + - Keep tests simple and focused |
| 145 | + |
| 146 | +3. **Test Maintenance** |
| 147 | + - Update tests when implementation changes |
| 148 | + - Remove obsolete tests |
| 149 | + - Keep test code as clean as production code |
| 150 | + |
| 151 | +4. **Performance** |
| 152 | + - Keep unit tests fast |
| 153 | + - Use appropriate fixtures |
| 154 | + - Mock expensive operations |
| 155 | + |
| 156 | +## Continuous Integration |
| 157 | + |
| 158 | +Our CI pipeline: |
| 159 | +- Runs all tests |
| 160 | +- Checks code coverage |
| 161 | +- Enforces style guidelines |
| 162 | +- Runs security checks |
| 163 | + |
| 164 | +## Further Reading |
| 165 | + |
| 166 | +- [Code Structure](code-structure.md) |
| 167 | +- [Contributing Guide](../../CONTRIBUTING.md) |
| 168 | +- [Pre-commit Hooks](pre-commit-hooks.md) |
0 commit comments