# 断言错误：在 caplog.records 中，'WARNING' 级别的记录数断言 0 == 1

- **ID:** `python/pytest-caplog-level-mismatch`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

测试使用 caplog 检查日志消息，但被测试代码中设置的日志级别与预期级别不匹配，或者记录器未正确配置。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.10 | active | — | — |

## 解决方案

1. **** (85% 成功率)
   ```
   Set the logging level in the test using caplog.set_level(logging.WARNING) before calling the code, and ensure the code logs at that level.
   ```
2. **** (80% 成功率)
   ```
   Check the logger name in the code under test and configure caplog to capture logs from that specific logger: caplog.set_level(logging.WARNING, logger='my_logger')
   ```

## 无效尝试

- **** — Setting caplog.set_level(logging.DEBUG) globally may capture too many logs and mask the specific level issue. (50% 失败率)
- **** — Ignoring the level and checking for any log message may pass but does not validate the intended behavior. (40% 失败率)
