1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
# Inquest Examples
This document provides practical examples of using inquest with various test frameworks and configurations.
## Basic .testr.conf Examples
### Python unittest
```ini
[DEFAULT]
test_command=python -m subunit.run discover -t . -s tests $LISTOPT
test_id_list_default=tests
test_list_option=--list
```
### Python pytest with subunit
```ini
[DEFAULT]
test_command=pytest --subunit-trace $IDOPTION
test_id_option=--test-list=$IDFILE
test_list_option=--collect-only -q
```
### Rust with cargo-subunit
```ini
[DEFAULT]
test_command=cargo test --quiet -- --format=subunit $LISTOPT
test_list_option=--list
```
### Node.js with tape and tap-subunit
```ini
[DEFAULT]
test_command=node test/*.js | tap-subunit
test_id_list_default=test/
```
## Advanced Configurations
### Test Grouping by Module
```ini
[DEFAULT]
test_command=python -m subunit.run $IDOPTION
test_id_option=$IDLIST
group_regex=^(.*\.)?(?P<module>[^.]+)\.
```
This groups tests by their module name, useful for running related tests together.
### Custom Test Discovery
```ini
[DEFAULT]
test_command=./scripts/run-tests.sh $IDOPTION
test_id_option=--tests=$IDFILE
test_id_list_default=all
```
### Dynamic Concurrency
```ini
[DEFAULT]
test_command=python -m subunit.run $IDOPTION
test_id_option=$IDLIST
# Automatically detect CPU count
test_run_concurrency=nproc
```
For more complex scenarios:
```ini
[DEFAULT]
test_command=python -m subunit.run $IDOPTION
test_id_option=$IDLIST
# Custom script to determine concurrency
test_run_concurrency=./scripts/get-worker-count.sh
```
### Instance Provisioning
For tests that need isolated environments (e.g., separate databases, ports):
```ini
[DEFAULT]
test_command=python -m subunit.run $IDOPTION
test_id_option=$IDLIST
# Provision N test databases and return their IDs
instance_provision=./scripts/provision-db.sh $INSTANCE_COUNT
# Execute tests against a specific instance
instance_execute=DB_ID=$INSTANCE_ID python -m subunit.run $IDOPTION
# Clean up the test database
instance_dispose=./scripts/dispose-db.sh $INSTANCE_ID
```
The provision script should output one instance ID per line:
```bash
#!/bin/bash
# provision-db.sh
for i in $(seq 1 $1); do
db_id="test-db-$i"
# Create database
createdb $db_id
echo $db_id
done
```
The dispose script receives each instance ID:
```bash
#!/bin/bash
# dispose-db.sh
dropdb $1
```
## Common Usage Patterns
### Initial Setup
```bash
# Initialize repository
inq init
# Run all tests
inq run
# View results
inq last
```
### Debugging Failures
```bash
# Run only failing tests
inq run --failing
# Run tests in isolation to find interactions
inq run --failing --isolated
# Analyze which tests cause isolation failures
inq analyze-isolation my_module.test_flaky
# Run until failure to catch flaky tests
inq run --until-failure
```
### Performance Testing
```bash
# Run tests in parallel
inq run -j 4
# View slowest tests
inq slowest
# View all test timings
inq slowest --all
```
### Continuous Integration
```bash
# Run tests and create repository if needed
inq run --force-init
# Run subset of tests from a file
inq run --load-list changed-tests.txt
# Get statistics
inq stats
```
### Advanced Workflows
```bash
# Parallel execution until failure (stress testing)
inq run -j 8 --until-failure
# Isolated execution of failing tests
inq run --failing --isolated
# Partial runs (additive failing test tracking)
inq run --partial
# Get list of failing test IDs for scripting
inq failing --list > failing.txt
```
## Integration Examples
### GitHub Actions
```yaml
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: |
pip install subunit python-subunit
cargo install inquest
- name: Run tests
run: inq run --force-init -j 4
- name: Show results
if: always()
run: inq last
```
### GitLab CI
```yaml
test:
script:
- inq run --force-init
- inq stats
artifacts:
when: always
paths:
- .testrepository/
```
### Pre-commit Hook
```bash
#!/bin/bash
# .git/hooks/pre-commit
# Run only changed tests
git diff --cached --name-only --diff-filter=AM | \
grep test_ | \
sed 's/\.py$//' | \
tr '/' '.' > /tmp/tests.txt
if [ -s /tmp/tests.txt ]; then
inq run --load-list /tmp/tests.txt
fi
```
## Performance Optimization
### Balancing Parallel Workers
The optimal number of workers depends on your test suite:
```bash
# CPU-bound tests: use core count
inq run -j $(nproc)
# I/O-bound tests: use more workers
inq run -j $(($(nproc) * 2))
# Mixed workload: start conservative
inq run -j 4
```
### Test Duration Tracking
Inquest automatically tracks test durations in `.testrepository/times.dbm`:
```bash
# First run (no timing data)
inq run -j 4
# Second run (uses timing for better load balancing)
inq run -j 4
```
The second run will distribute tests more evenly based on historical durations.
## Troubleshooting
### Tests Don't Run
```bash
# Check configuration
cat .testr.conf
# Test command manually
python -m subunit.run discover --list
# Check repository
inq stats
```
### Subunit Format Issues
```bash
# Verify subunit output
python -m subunit.run discover | python -m subunit.stats
# Check for binary corruption
file .testrepository/0
```
### Parallel Execution Issues
```bash
# Run in serial to isolate the issue
inq run
# Run in isolated mode to check for test interactions
inq run --isolated
# Check worker-specific failures
inq last | grep worker-
```
### Test Isolation Failures
When a test passes in isolation but fails when run with other tests:
```bash
# Step 1: Verify the test fails with others
inq run
# Step 2: Verify it passes in isolation
inq run --isolated test_module.test_flaky
# Step 3: Find the minimal set of tests causing the issue
inq analyze-isolation test_module.test_flaky
# The command will output which tests cause the failure
# Example output:
# Found minimal set of 2 tests causing isolation failure:
# - test_module.test_setup_state
# - test_module.test_cleanup_missing
```
|