python
runtime_error
ai_generated
true
AssertionError: assert datetime.date(2025, 1, 1) == datetime.date(2024, 12, 31) # 仅在 UTC 的 CI 中失败
AssertionError: assert datetime.date(2025, 1, 1) == datetime.date(2024, 12, 31) # only fails in CI in UTC
ID: python/pytest-freezegun-timezone
80%修复率
82%置信度
0证据数
2025-09-19首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.5 | active | — | — | — |
根因分析
freezegun 默认冻结 UTC 时间;断言本地时间或使用时区感知代码的测试在开发者机器 TZ 与 CI TZ 不同时失败。
English
freezegun freezes UTC time by default; tests that assert on local time or use timezone-aware code fail when developer machine TZ differs from CI TZ.
解决方案
-
90% 成功率
from freezegun import freeze_time @freeze_time('2025-01-01 00:00:00', tz_offset=0) def test_x(): ... # or use tz-aware datetimes in assertions -
88% 成功率
from datetime import datetime, timezone now = datetime.now(timezone.utc) # assert against UTC-aware values in tests
无效尝试
常见但无效的做法:
-
85% 失败
Makes CI match freezegun but local runs diverge; the underlying timezone-aware bug remains.
-
90% 失败
freezegun interprets the string as UTC; local-time assertions still fail.
-
80% 失败
Tests become flaky around midnight and month boundaries; loses determinism.