File size: 8,768 Bytes
5a81b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# pytest-error-handler

**Purpose**: Detect test failures, edge cases, and error handling bugs
**Status**: 🟑 Working (Windows workaround required)
**Version**: 1.2.0+
**Maintainer**: QA Team (Block 5)

## What It Does

Pytest-error-handler extends pytest to:
- Capture unhandled exceptions in tests
- Detect async/await issues (race conditions, deadlocks)
- Find missing error handlers
- Track exception propagation through code
- Report edge cases not covered by tests

## Installation

```bash
# Already installed in project
# To update:
pip install pytest-error-handler --upgrade

# Verify installation
pytest --version
pytest --help | grep error-handler
```

## Windows-Only Workaround

### Problem
pytest-error-handler fails on Windows with cp1252 encoding conflicts

### Solution
**Always run with explicit UTF-8 encoding**:

```bash
# Windows - REQUIRED
pytest --encoding=utf-8 --error-handler-mode strict

# NOT this (will fail on Windows)
pytest --error-handler-mode strict

# Alternative: Set environment variable
set PYTEST_ENCODING=utf-8
pytest --error-handler-mode strict
```

### Why This Happens
- Windows default encoding: cp1252
- pytest-error-handler needs UTF-8 for unicode symbols
- Simple fix: Specify encoding explicitly

## Quick Start

### 1. Run on Your Code
```bash
# Run all tests with error detection
pytest tests/ --encoding=utf-8 --error-handler-mode strict

# Run specific test file
pytest tests/auth_test.py --encoding=utf-8 --error-handler-mode strict

# Run with verbose output
pytest tests/ --encoding=utf-8 --error-handler-mode strict -v
```

### 2. Expected Output
```
tests/auth_test.py::test_invalid_token PASSED
tests/auth_test.py::test_missing_auth FAILED [ERROR CAUGHT]
  Error: TypeError: 'NoneType' object is not subscriptable
  Location: auth.js:45 in validate_token()
  Status: UNHANDLED - needs try/except

tests/api_test.py::test_async_race FAILED [ERROR CAUGHT]
  Error: RuntimeError: Task already running
  Type: ASYNC_ISSUE - likely race condition
  Recommendation: Add mutex or async.Lock()
```

### 3. Interpret Results

**PASSED** βœ…
- Test executed successfully
- All exceptions caught and handled
- Good error handling

**FAILED [ERROR CAUGHT]** ⚠️
- Exception found but not caught by test
- Need to add try/except or error handler
- Usually indicates missing edge case handling

**BLOCKED [TIMEOUT]** 🚨
- Test hung (likely deadlock or infinite loop)
- Check for circular async dependencies
- Add timeout decorators

## Configuration

**File**: `config/error-detection/pytest-handlers.ini`

```ini
[tool:pytest]
encoding = utf-8
error_handler_mode = strict
timeout = 30
markers =
    error_critical: Critical error that blocks release
    error_security: Security-related error
    error_performance: Performance issue found
    error_async: Async/await related issue
```

### Activation
Config is **automatic**. To use:

```bash
# Uses config automatically
pytest tests/

# Override config
pytest tests/ --config-file=my-pytest.ini --encoding=utf-8
```

## Usage Patterns

### Pattern 1: Find All Error Cases
```bash
# Comprehensive scan
pytest tests/ --encoding=utf-8 --error-handler-mode strict -v

# Save results to file
pytest tests/ --encoding=utf-8 --error-handler-mode strict > error-report.txt 2>&1
```

### Pattern 2: Focus on Specific Errors
```bash
# Only show critical errors
pytest tests/ --encoding=utf-8 --error-handler-mode strict -m error_critical

# Only show security errors
pytest tests/ --encoding=utf-8 --error-handler-mode strict -m error_security

# Only show async issues
pytest tests/ --encoding=utf-8 --error-handler-mode strict -m error_async
```

### Pattern 3: Fix and Re-verify
```bash
# 1. Run initial scan
pytest tests/ --encoding=utf-8 --error-handler-mode strict > errors.txt

# 2. Fix issues (add error handlers, fix async)
# ... edit your code ...

# 3. Re-run to confirm
pytest tests/ --encoding=utf-8 --error-handler-mode strict

# Should show PASSED for previous FAILED tests
```

## Common Errors and Fixes

### Error: "AttributeError: module 'pytest' has no attribute 'encoding'"
**Cause**: Old pytest version or incorrect installation
**Fix**:
```bash
pip uninstall pytest-error-handler
pip install pytest-error-handler --upgrade
pytest --encoding=utf-8 --error-handler-mode strict
```

### Error: "UnicodeDecodeError: 'cp1252' codec can't decode..."
**Cause**: Windows encoding issue (CLASSIC WINDOWS PROBLEM)
**Fix**:
```bash
# Add to command line
pytest tests/ --encoding=utf-8 --error-handler-mode strict

# OR set environment variable
set PYTEST_ENCODING=utf-8
pytest tests/
```

### Error: "No tests ran"
**Cause**: Tests not found or wrong directory
**Fix**:
```bash
# Verify test files exist
dir tests/
# or
find . -name "*test*.py" -path "*/tests/*"

# Run from project root
cd C:/Users/claus/Projects/WidgetTDC
pytest tests/ --encoding=utf-8 --error-handler-mode strict
```

### Error: "BLOCKED [TIMEOUT]"
**Cause**: Test hung (infinite loop or deadlock)
**Fix**:
```python
# Add timeout decorator to test
import pytest

@pytest.mark.timeout(10)  # 10 second timeout
async def test_async_operation():
    # Your async test code
    pass
```

## Example Scenarios

### Scenario 1: Widget Discovery Error Handling
**Block**: 5 (QASpecialist)
**Task**: Verify widget discovery pipeline handles all errors

```bash
# Run tests for discovery pipeline
pytest tests/discovery/ --encoding=utf-8 --error-handler-mode strict

# Expected findings:
# - Missing error handlers for network timeouts
# - Unhandled JSON parsing errors
# - Invalid widget structure handling
```

### Scenario 2: Authentication Edge Cases
**Block**: 1 (Frontend)
**Task**: Find all auth token validation edge cases

```bash
# Run auth tests
pytest tests/auth/ --encoding=utf-8 --error-handler-mode strict -m error_critical

# Expected findings:
# - Null token handling
# - Expired token edge cases
# - Malformed token parsing
```

### Scenario 3: Async Race Conditions
**Block**: 2 (CloudArch)
**Task**: Find MCP communication race conditions

```bash
# Run MCP integration tests
pytest tests/mcp/ --encoding=utf-8 --error-handler-mode strict -m error_async

# Expected findings:
# - Parallel message handling issues
# - Widget trigger ordering problems
# - State synchronization race conditions
```

## Integration with Cascade

pytest-error-handler runs automatically:
- On each agent block execution (if tests available)
- Results logged to: `.claude/logs/test-errors.log`
- Critical failures trigger re-execution of block
- Findings included in daily standup metrics

## Performance Tips

### Tip 1: Run Specific Tests First
```bash
# Don't run everything at first
pytest tests/auth/ --encoding=utf-8 --error-handler-mode strict

# Then expand scope
pytest tests/ --encoding=utf-8 --error-handler-mode strict
```

### Tip 2: Use Parallel Execution
```bash
# Run tests in parallel (faster)
pip install pytest-xdist
pytest tests/ --encoding=utf-8 --error-handler-mode strict -n auto

# Specify number of workers
pytest tests/ --encoding=utf-8 --error-handler-mode strict -n 4
```

### Tip 3: Cache Results
```bash
# Only re-run failed tests
pytest tests/ --encoding=utf-8 --error-handler-mode strict --lf

# Re-run failed + last tested
pytest tests/ --encoding=utf-8 --error-handler-mode strict --ff
```

## Troubleshooting Checklist

- [ ] Using `--encoding=utf-8` flag? (Windows required)
- [ ] Are test files in `tests/` directory?
- [ ] Does your code have error handlers?
- [ ] Are async tests using proper decorators?
- [ ] Is pytest-error-handler installed? (`pip list | grep pytest`)
- [ ] Running from project root directory?

## Success Metrics

**Good error-handler results**:
- βœ… All tests PASSED or marked FAILED [ERROR CAUGHT]
- βœ… Error types clearly identified
- βœ… Actionable fixes suggested
- βœ… No BLOCKED [TIMEOUT] results
- βœ… Async issues identified and documented

**Poor results** (need investigation):
- ❌ Many BLOCKED [TIMEOUT] tests
- ❌ Vague error messages
- ❌ Encoding errors on Windows
- ❌ "No tests ran" with tests present

## Next Steps

1. **Verify**: Run on your block's test suite
2. **Review**: Read error report carefully
3. **Fix**: Address critical errors first
4. **Re-run**: Confirm fixes with pytest again
5. **Report**: Document findings in daily standup

## Questions?

Escalation path:
1. Check this README (solutions for common issues)
2. Check mypy-strict-mode as alternative for type checking
3. File issue in daily standup with:
   - Error message
   - Command you ran
   - Expected vs actual behavior
   - Your block number

---

**Ready to use?**

```bash
cd C:/Users/claus/Projects/WidgetTDC
pytest tests/ --encoding=utf-8 --error-handler-mode strict -v
```

Start with one test directory, then expand scope. πŸš€