# Testing Guide
This document describes the test suite for the quality-agent profiler.
## Test Structure
The project includes comprehensive tests for profiling Python servers and applications:
### Unit Tests (`src/profiler/python.rs`)
Located in the `tests` module of `python.rs`, these test the core profiling functionality:
- **test_profiler_creation** - Verifies profiler instantiation
- **test_profile_simple_function** - Tests basic function detection
- **test_profile_with_classes** - Tests class detection
- **test_profile_with_imports** - Tests import statement detection
- **test_complexity_calculation** - Tests complexity scoring
- **test_empty_file** - Tests handling of empty files
- **test_profile_server_code** - Tests profiling realistic server code with multiple functions
- **test_file_size_calculation** - Verifies file size metrics
- **test_line_count** - Tests line counting
- **test_nested_functions** - Tests nested function detection
- **test_async_functions** - Tests async/sync function handling
### Integration Tests (`tests/python_profiler_tests.rs`)
These test the complete profiler workflow:
#### Static Analysis Tests
- **test_static_analysis_sample_python** - Verifies sample.py analysis
- **test_server_simulation_exists** - Checks server simulation file
- **test_sample_server_exists** - Checks HTTP server file
#### Binary Tests
- **test_profiler_binary_exists** - Verifies compiled binary
- **test_profile_sample_python_static** - Tests static profiling via binary
- **test_profile_server_simulation_static** - Tests server profiling via binary
#### Runtime Profiling Tests (Require py-spy)
- **test_profile_server_with_runtime** - Tests runtime profiling (ignored by default)
- **test_profile_to_json_format** - Tests JSON export (ignored by default)
#### Python Execution Tests
- **test_sample_python_executes** - Verifies Python script execution
- **test_server_simulation_executes** - Verifies server simulation runs
## Test Python Files
### `examples/sample.py`
Basic Python script with functions and classes for testing static analysis.
### `examples/server_simulation.py`
CPU-intensive server simulation that:
- Runs for a configurable duration
- Executes fibonacci, prime finding, and data processing
- Ideal for runtime profiling tests
- Exits automatically after duration
### `examples/sample_server.py`
Full HTTP server with multiple endpoints:
- `/` - Service info
- `/fast` - Quick response
- `/slow` - Simulated slow operation
- `/cpu-intensive` - Heavy computation
- `/fibonacci?n=X` - Compute fibonacci
- `/memory` - Memory allocation test
- `/health` - Health check
## Running Tests
### Run All Tests
```bash
cargo test
```
### Run Only Unit Tests
```bash
cargo test --lib
```
### Run Only Integration Tests
```bash
cargo test --test python_profiler_tests
```
### Run Specific Test
```bash
cargo test test_profile_server_code
```
### Run with Output
```bash
cargo test -- --nocapture
```
### Run Ignored Tests (Require py-spy)
```bash
# Install py-spy first
pip install py-spy
# Run ignored tests
cargo test -- --ignored
```
## Manual Testing
### Test Static Profiling
```bash
# Profile the sample Python file
cargo run -- python examples/sample.py
# Profile the server simulation
cargo run -- python examples/server_simulation.py
```
### Test Runtime Profiling (Requires py-spy)
```bash
# Profile server for 3 seconds
cargo run -- python examples/server_simulation.py --runtime 3
# Note: server_simulation.py takes an argument for duration
# Make sure the profiling duration is less than or equal to the script duration
```
### Test JSON Export
```bash
# Export to JSON format
cargo run -- python examples/server_simulation.py --json output.json
# View the JSON output
## Test Coverage
The test suite covers:
- ✅ Static code analysis (functions, classes, imports)
- ✅ Complexity calculation
- ✅ File metrics (size, lines)
- ✅ Server code profiling
- ✅ Runtime profiling (with py-spy)
- ✅ JSON format export
- ✅ Binary execution
- ✅ Python script execution
## CI/CD Considerations
For CI/CD pipelines:
1. Most tests run without external dependencies
2. Runtime profiling tests are ignored by default (require py-spy installation)
3. Python 3 must be available for execution tests
4. Tests are isolated and can run in parallel
## Troubleshooting
### py-spy Tests Failing
Install py-spy:
```bash
pip install py-spy
# or
pip3 install py-spy
```
Note: py-spy may require elevated permissions on some systems.
### Python Not Found
Ensure Python 3 is installed:
```bash
python3 --version
```
### Tests Timeout
If runtime profiling tests timeout:
- Reduce the duration parameter
- Check that the Python script completes within the timeout
- Verify py-spy is working: `py-spy --version`
## Adding New Tests
When adding new test cases:
1. **Unit tests**: Add to `src/profiler/python.rs` in the `tests` module
2. **Integration tests**: Add to `tests/python_profiler_tests.rs`
3. **Test Python files**: Add to `examples/` directory
4. Use `#[ignore]` for tests requiring external dependencies
5. Document expected behavior in test names and comments
## Performance Benchmarks
The server simulation provides consistent workload for benchmarking:
```bash
# Run for 10 seconds
python3 examples/server_simulation.py 10
# Profile with py-spy
py-spy record --duration 10 -- python3 examples/server_simulation.py 10
```