Freeze the clock to a specific date so code that checks whether a subscription or deadline has expired behaves predictably in tests.
Test retry logic by manually advancing the clock to simulate waiting periods without actually sleeping in the test suite.
Verify timezone-sensitive behavior by pinning the clock to a specific UTC offset and running your time-aware code.
FreezeGun is a Python library that solves a common testing problem: code that checks the current date or time behaves differently depending on when you run it, which makes tests unpredictable. FreezeGun lets you pin the clock to a specific moment for the duration of a test, so the code under test always sees the same date and time no matter when the test actually runs. You use it by adding a decorator above a test function or by wrapping a block of code in a context manager. In both cases, you specify a date and time, and for the duration of that block every standard Python time call returns the frozen value. This includes the usual suspects like getting the current datetime, the current date, the Unix timestamp, and local or UTC time. Once the frozen block exits, the real clock resumes. Beyond simple freezing, the library offers a few variations. You can pass a tick=True argument to start time at the frozen moment but let it move forward normally from there, which is useful when you care about relative time differences rather than absolute values. An auto_tick_seconds argument advances the time by a fixed number of seconds each time you ask for it. Manual ticking lets you advance the clock forward by an explicit amount mid-test, and move_to lets you jump to a completely different date without exiting the frozen context. These variations cover scenarios where you need to test that something happens before or after a deadline, or that a retry waits the right amount of time. FreezeGun accepts a human-friendly date format in addition to the standard ISO format, so writing "Jan 14th, 2012" works the same as "2012-01-14". It also supports timezone offsets when you need to test timezone-sensitive behavior. The library integrates with both pytest-style tests and the older unittest style. Installation is through pip.
← spulec on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.