freezegun
Library to freeze and travel through time in Python tests so date-dependent code is deterministic.
Official Documentation ↗freezegun 1.5+ / Python 3.8+. Check the official docs for the latest API surface.Installation
pip install freezegunMethods
No methods match this filter.
Overview
freezegun replaces the clock in Python code so tests involving dates, expiry and scheduling are deterministic. It can freeze to a fixed instant or advance time on demand.
Core Methods
Apply freeze_time as a decorator or context manager with a date string, and use the returned object tick to move the clock forward, simulating elapsed time between actions.
Usage Notes
Common Pitfalls
Native extensions with their own cached time are unaffected, causing subtle mismatches. Forgetting to leave the frozen context pollutes later tests. Monotonic time is not frozen, so duration code can behave differently.
FAQ
- Which time functions are patched?
- freezegun patches datetime, time and related functions in Python and many common libraries. Some native extensions cache their own clock, so freezing may not affect them.
- How do I test timezone-sensitive code?
- Pass tz_offset to freeze_time so the frozen instant includes an offset, or set the timezone explicitly in the tested code to avoid hidden local-time assumptions.
- What about monotonic clocks?
- Wall-clock freezing does not freeze monotonic time used for durations. Code that relies on monotonic time for elapsed measurements may not be affected as expected.
Report a problem with this API docOpens a pre-filled issue on GitHub.