# DeprecationWarning: datetime.datetime.utcnow() 已弃用并计划在未来版本中移除。
E   DeprecationWarning: datetime.datetime.utcnow() 已弃用

- **ID:** `python/pytest-deprecationwarning-as-error`
- **领域:** python
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

pytest 配置了 filterwarnings = error，将 DeprecationWarning 变为测试失败；代码或依赖仍调用 utcnow()。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   from datetime import datetime, timezone
now = datetime.now(timezone.utc)
   ```
2. **** (80% 成功率)
   ```
   # pytest.ini
# filterwarnings =
#   error
#   ignore:datetime.datetime.utcnow:DeprecationWarning:legacy_lib
   ```

## 无效尝试

- **** — Hides all warnings and defeats the purpose; future breakages go unnoticed. (60% 失败率)
- **** — Ignores all deprecations including ones from your own code; not targeted. (55% 失败率)
