|
| 1 | +# Testing Guide |
| 2 | + |
| 3 | +## Test Performance Optimization |
| 4 | + |
| 5 | +This project has been optimized for fast test execution during development while maintaining comprehensive coverage for CI/CD pipelines. |
| 6 | + |
| 7 | +## Available Test Commands |
| 8 | + |
| 9 | +### Quick Commands |
| 10 | + |
| 11 | +```bash |
| 12 | +# Fast development tests (recommended for daily use) |
| 13 | +make test # 2.56s - Parallel execution, excludes slow tests |
| 14 | +make test-fast # 2.56s - Same as above (explicit alias) |
| 15 | + |
| 16 | +# Comprehensive testing |
| 17 | +make test-all # 8.20s - All tests including slow ones |
| 18 | +make test-parallel # 3.52s - All tests in parallel |
| 19 | +make coverage # 8.60s - Full coverage report |
| 20 | +``` |
| 21 | + |
| 22 | +### Manual pytest Commands |
| 23 | + |
| 24 | +```bash |
| 25 | +# Fast tests only |
| 26 | +uv run pytest -n auto -m "not slow" |
| 27 | + |
| 28 | +# All tests in parallel |
| 29 | +uv run pytest -n auto |
| 30 | + |
| 31 | +# All tests sequential |
| 32 | +uv run pytest |
| 33 | + |
| 34 | +# Coverage report |
| 35 | +uv run pytest --cov=src/mcp_shell_server --cov-report=xml --cov-report=term-missing tests |
| 36 | +``` |
| 37 | + |
| 38 | +## Test Categories |
| 39 | + |
| 40 | +### Fast Tests (Default) |
| 41 | +- **Count**: 114 tests |
| 42 | +- **Execution**: ~2.56 seconds |
| 43 | +- **Parallel**: 8 workers |
| 44 | +- **Usage**: Daily development, quick feedback |
| 45 | + |
| 46 | +### Slow Tests |
| 47 | +- **Marked with**: `@pytest.mark.slow` |
| 48 | +- **Purpose**: Timeout testing, integration scenarios |
| 49 | +- **Usage**: CI/CD, comprehensive validation |
| 50 | + |
| 51 | +## Performance Improvements Applied |
| 52 | + |
| 53 | +### 1. Parallel Execution |
| 54 | +- **Tool**: pytest-xdist |
| 55 | +- **Workers**: Auto-detected (typically 8 on modern systems) |
| 56 | +- **Speed gain**: ~4x faster execution |
| 57 | + |
| 58 | +### 2. Sleep Optimization |
| 59 | +- Reduced `asyncio.sleep(10)` → `asyncio.sleep(0.01)` (999x faster) |
| 60 | +- Reduced `asyncio.sleep(1)` → `asyncio.sleep(0.1)` (10x faster) |
| 61 | +- **Files affected**: |
| 62 | + - `tests/test_server.py:473` |
| 63 | + - `tests/test_process_manager_macos.py:46` |
| 64 | + |
| 65 | +### 3. Test Categorization |
| 66 | +- Timeout tests marked as `@pytest.mark.slow` |
| 67 | +- Selective execution with `-m "not slow"` |
| 68 | +- Development focus on fast feedback |
| 69 | + |
| 70 | +## Development Workflow |
| 71 | + |
| 72 | +### During Development |
| 73 | +```bash |
| 74 | +# Quick validation (recommended) |
| 75 | +make test # 2.56s, 114 tests |
| 76 | + |
| 77 | +# If you need specific test |
| 78 | +uv run pytest tests/test_specific.py -v |
| 79 | +``` |
| 80 | + |
| 81 | +### Before Commits |
| 82 | +```bash |
| 83 | +# Full validation |
| 84 | +make test-all # All tests including slow ones |
| 85 | +make coverage # Ensure coverage standards |
| 86 | +``` |
| 87 | + |
| 88 | +### CI/CD Pipeline |
| 89 | +```bash |
| 90 | +# Comprehensive validation |
| 91 | +make test-parallel # Fast but complete |
| 92 | +make coverage # Coverage reporting |
| 93 | +``` |
| 94 | + |
| 95 | +## Coverage Standards |
| 96 | + |
| 97 | +- **Target**: 95% minimum coverage |
| 98 | +- **Current**: 95% (598 statements, 29 missing) |
| 99 | +- **High-coverage modules**: |
| 100 | + - `command_validator.py`: 100% |
| 101 | + - `command_preprocessor.py`: 98% |
| 102 | + - `shell_executor.py`: 97% |
| 103 | + - `server.py`: 96% |
| 104 | + |
| 105 | +## Adding New Tests |
| 106 | + |
| 107 | +### Fast Tests (Default) |
| 108 | +```python |
| 109 | +import pytest |
| 110 | + |
| 111 | +@pytest.mark.asyncio |
| 112 | +async def test_my_feature(): |
| 113 | + # Regular test - will run in fast mode |
| 114 | + assert True |
| 115 | +``` |
| 116 | + |
| 117 | +### Slow Tests |
| 118 | +```python |
| 119 | +import pytest |
| 120 | + |
| 121 | +@pytest.mark.slow # Mark as slow test |
| 122 | +@pytest.mark.asyncio |
| 123 | +async def test_timeout_behavior(): |
| 124 | + # Test that involves timeouts or long waits |
| 125 | + await asyncio.sleep(2) # This is OK in slow tests |
| 126 | + assert True |
| 127 | +``` |
| 128 | + |
| 129 | +## Troubleshooting |
| 130 | + |
| 131 | +### Tests Taking Too Long? |
| 132 | +1. Check if you're using `make test` (fast) vs `make test-all` (comprehensive) |
| 133 | +2. Ensure pytest-xdist is installed: `uv pip install pytest-xdist` |
| 134 | +3. Use `-v` flag for verbose output to identify slow tests |
| 135 | + |
| 136 | +### Coverage Issues? |
| 137 | +```bash |
| 138 | +# Generate detailed coverage report |
| 139 | +make coverage |
| 140 | + |
| 141 | +# View in browser (if HTML report generated) |
| 142 | +open htmlcov/index.html |
| 143 | +``` |
| 144 | + |
| 145 | +### Test Failures in CI? |
| 146 | +- CI runs `make test-parallel` (all tests including slow ones) |
| 147 | +- Local development uses `make test` (excludes slow tests) |
| 148 | +- Ensure slow tests are properly marked with `@pytest.mark.slow` |
| 149 | + |
| 150 | +## Performance Metrics |
| 151 | + |
| 152 | +| Test Category | Tests | Time | Workers | Usage | |
| 153 | +|---------------|--------|------|---------|--------| |
| 154 | +| `make test` | 114 | 2.56s | 8 | Development | |
| 155 | +| `make test-fast` | 114 | 2.56s | 8 | Development | |
| 156 | +| `make test-parallel` | 120 | 3.52s | 8 | CI/CD | |
| 157 | +| `make test-all` | 120 | 8.20s | 1 | Full validation | |
| 158 | +| `make coverage` | 120 | 8.60s | 1 | Coverage analysis | |
| 159 | + |
| 160 | +## Best Practices |
| 161 | + |
| 162 | +1. **Use `make test` for development** - Fast feedback loop |
| 163 | +2. **Mark timeout tests as slow** - Keep development tests fast |
| 164 | +3. **Run full tests before commits** - Ensure nothing breaks |
| 165 | +4. **Monitor coverage** - Maintain 95% minimum |
| 166 | +5. **Optimize sleep calls** - Use minimal delays in tests |
| 167 | +6. **Leverage parallel execution** - Take advantage of multi-core systems |
0 commit comments