freezegun

Let your Python tests travel through time by mocking datetime, time, and uuid calls to a fixed or ticking moment.

Library
PyPI
v1.5.5
4,526 stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
46 /100 Fair
Development Activity 0
Maintenance 20
Community 64
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
75 /100 Good
Architecture 74
Code Quality 78
Innovation 62
Learning Curve 85

FreezeGun is a Python testing utility that patches the standard library’s time-related calls — datetime.datetime.now(), datetime.date.today(), time.time(), time.localtime(), time.gmtime(), time.strftime(), time.monotonic(), time.perf_counter(), and UUID1 generation — so tests can assert against a fixed, known point in time instead of racing the real clock.

The core freeze_time() entry point works as a decorator on functions, unittest.TestCase classes, or any callable class, as well as a context manager or a manually started/stopped object. It accepts natural-language and structured inputs (ISO strings, datetime objects, date objects, or a callable/generator that yields successive datetimes) via python-dateutil, plus a tz_offset for simulating non-UTC test environments.

Beyond simple freezing, it supports tick=True and auto_tick_seconds to let frozen time advance automatically, and a returned FrozenDateTimeFactory object with .tick() and .move_to() methods for manually stepping or jumping the frozen clock mid-test. A real_asyncio=True flag keeps asyncio event-loop scheduling on real monotonic time even while time.monotonic() is frozen elsewhere, avoiding broken asyncio.sleep() calls in async test suites.

A configurable ignore list (nose.plugins, selenium, threading, gi, etc.) excludes specific modules from time mocking, since blindly patching every consumer of time.time() can break test runners and other libraries that rely on real timestamps internally.

What You Get

  • @freeze_time(...) decorator usable on plain functions, async coroutines, unittest.TestCase classes, and arbitrary classes
  • A context-manager and manual start()/stop() API for freezing time around arbitrary code blocks
  • Ticking modes (tick=True, auto_tick_seconds) so frozen time can advance realistically instead of staying perfectly static
  • A FrozenDateTimeFactory handle exposing .tick(delta) and .move_to(target) for step-by-step or jump-based time control mid-test
  • Natural-language date parsing via python-dateutil, plus support for callables and generators as the time source
  • A configurable default ignore list (and freezegun.configure()) to exempt specific modules like threading or selenium from mocking

Common Use Cases

  • Asserting business logic that depends on datetime.datetime.now() or date.today() without waiting for real time to pass
  • Testing time-based expiration, scheduling, or TTL logic (tokens, caches, subscriptions) at exact simulated instants
  • Verifying behavior across timezone or DST boundaries by freezing to a specific UTC instant with a tz_offset
  • Simulating multi-step time progression in a single test using tick() or move_to() to check state transitions over time
  • Freezing an entire unittest.TestCase class so every test method and setUp/tearDown runs against the same fixed date

Under The Hood

Architecture The library centers on a small factory hierarchy in freezegun/api.py — FrozenDateTimeFactory, TickingDateTimeFactory, and StepTickTimeFactory — each producing fake replacements for datetime.datetime, datetime.date, and the relevant time module functions, pushed onto module-level stacks (freeze_factories, tz_offsets, ignore_lists, tick_flags) so nested freeze_time() calls compose correctly; _freeze_time.start()/.stop() swap these fakes into datetime, time, calendar, and uuid at the module level and restore the originals captured at import time (real_time, real_datetime, etc.) on exit, with _async.py providing a small coroutine-wrapping shim so the decorator can also wrap async def test functions.

Tech Stack A pure-Python library (99.75% Python by bytes) with a single runtime dependency, python-dateutil>=2.7, used for flexible date-string parsing; it optionally integrates with maya (only on Python <3.12) if installed; packaging is plain setuptools via setup.cfg/setup.py with no build step beyond that, and pyproject.toml is used only to configure mypy --strict type checking.

Code Quality The tests/ directory contains roughly 20 focused test modules (test_datetimes.py, test_ticking.py, test_asyncio.py, test_pickle.py, test_uuid.py, test_sqlite3.py, etc.) run via pytest with pytest-cov and Coveralls reporting in CI, and the package ships a py.typed marker with full type annotations enforced by mypy --strict; error handling is explicit (a dedicated ConfigurationError for misuse of configure()), and the CI workflow runs the suite across the supported Python versions and PyPy.

What Makes It Unique Rather than requiring application code to accept an injectable clock, FreezeGun patches the standard library itself — including edge cases like time.monotonic(), time.perf_counter(), nanosecond-resolution time functions, and even C-extension-backed UUID1 generation — while still offering an escape hatch (the ignore list and real_asyncio flag) for the small set of subsystems where faking time would break correctness. This breadth of coverage across the datetime, time, and uuid surfaces, combined with composable nesting and manual tick/move_to control, is why it has become the standard tool for this problem rather than a bespoke per-project mock.

Used by 9 apps in this directory

Python
47%
Other

Airbyte

Data Engineering · Developer Tools

22,143

Open-source ELT platform with 600+ connectors for moving data from any source to warehouses, lakes, and AI agents.

View details
95
Repo Health
80
Technical
67
Dependency
Built with
Python 47%
Kotlin 43%
Updated 4 days ago
Python
47%
MIT

Docs

CMS · File Storage

16,868

Open-source collaborative knowledge platform with real-time editing, AI writing tools, and full self-hosting control — built by the French and German governments.

View details
88
Repo Health
81
Technical
68
Dependency
Built with
Python 47%
TypeScript 45%
Updated 4 days ago
Python
64%
Other

Keep

Automation · Devops · Monitoring

12,359

The open-source AIOps and alert management platform that unifies 130+ monitoring tools into a single pane of glass with AI-powered correlation, deduplication, and workflow automation.

View details
89
Repo Health
79
Technical
66
Dependency
Built with
Python 64%
TypeScript 36%
Updated 1 weeks ago
Python
51%
AGPL 3.0

Khoj

AI Assistants · Knowledge Management · Productivity

37,526

A self-hostable AI second brain that chats with your documents, searches the web, builds custom agents, and runs entirely on your own LLM.

View details
63
Repo Health
82
Technical
66
Dependency
Built with
Python 51%
TypeScript 36%
Updated 2 months ago
TypeScript
39%
Apache 2.0

Label Studio

AI Development · Data Engineering

28,358

Label Studio is an open-source, multi-type data labeling platform that lets teams annotate images, text, audio, video, and time series data with a configurable XML-based UI and export annotations in formats ready for any ML framework.

View details
93
Repo Health
87
Technical
67
Dependency
Built with
TypeScript 39%
JavaScript 27%
Python 25%
Updated 4 days ago
Python
52%
Other

Odoo

CRM · ERP · Productivity

54,709

The open source ERP platform that integrates CRM, accounting, inventory, manufacturing, and 60+ business apps into one seamlessly connected suite.

View details
87
Repo Health
82
Technical
64
Dependency
Built with
Python 52%
JavaScript 44%
Updated 5 days ago
TypeScript
95%
Other

OpenHands

AI Code Assistants · AI Development

89,328

The self-hosted developer control center for running AI coding agents — locally, in Docker, on VMs, or across cloud backends — with automation workflows for GitHub, Slack, and more.

View details
91
Repo Health
82
Technical
67
Dependency
Built with
TypeScript 95%
Updated 5 days ago
TypeScript
51%
Other

Phase Console

Devops · Security

924

End-to-end encrypted secrets management for engineering teams — from local dev to Kubernetes production.

View details
84
Repo Health
73
Technical
63
Dependency
Built with
TypeScript 51%
Python 48%
Updated 5 days ago
Python
99%
Apache 2.0

Rasa Open Source

AI Assistants · AI Development · Voice AI

21,332

Rasa Open Source is a Python machine learning framework for building contextual, multi-turn chatbots and voice assistants that understand natural language and maintain conversation state.

View details
64
Repo Health
78
Technical
63
Dependency
Built with
Python 99%
Updated 2 months ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers