fix(test): replace caplog with custom handler for parallel execution#21282
fix(test): replace caplog with custom handler for parallel execution#21282
Conversation
The cost calculation log level tests were failing when run with pytest-xdist parallel execution because caplog doesn't work reliably across worker processes. This causes "ValueError: I/O operation on closed file" errors. Solution: Replace caplog fixture with a custom LogRecordHandler that directly attaches to the logger. This approach works correctly in parallel execution because each worker process has its own handler instance. Fixes test failures in PR #21277 when running with --dist=loadscope. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryReplaces
Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| tests/test_litellm/test_cost_calculation_log_level.py | Replaces caplog with custom LogRecordHandler for pytest-xdist compatibility. Correct approach with proper cleanup in finally blocks. Minor issues: unused pytest import and missing trailing newline. |
Sequence Diagram
sequenceDiagram
participant Test as Test Function
participant Handler as LogRecordHandler
participant Logger as verbose_logger
participant CostCalc as completion_cost / batch_cost_calculator
Test->>Handler: Create handler instance
Test->>Logger: Save original level
Test->>Logger: setLevel(DEBUG)
Test->>Logger: addHandler(handler)
Test->>CostCalc: Call cost calculation
CostCalc->>Logger: verbose_logger.debug(...)
Logger->>Handler: emit(record)
Handler->>Handler: Append to records[]
Test->>Handler: Filter records by message
Test->>Test: Assert records are DEBUG level
Test->>Logger: removeHandler(handler)
Test->>Logger: Restore original level
Last reviewed commit: 4f2c7d3
Additional Comments (1)
With the removal of the |
- Removed unused pytest import (caplog fixture was removed) - Added missing trailing newline at end of file Addresses Greptile feedback (minor style issues). Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Summary
Fixes
test_cost_calculation_uses_debug_levelandtest_batch_cost_calculation_uses_debug_levelfailures when running with pytest-xdist parallel execution (--dist=loadscope).Problem
The tests were failing with:
Root cause:
caplogfixture doesn't work reliably with pytest-xdist parallel execution because log capture happens in the main process while tests run in worker processes. This causes file handle issues and missing log records.Solution
Replaced
caplogwith a customLogRecordHandlerthat directly attaches to theverbose_logger. This approach works correctly in parallel execution because:Testing
pytest tests/test_litellm/test_cost_calculation_log_level.py -vpytest tests/test_litellm/test_cost_calculation_log_level.py -n 4 --dist=loadscope -vRelated
🤖 Generated with Claude Code