Registry / testing / pytest-freezegun

pytest-freezegun

JSON →
library0.4.2pypypi✓ verified 25d ago

pytest-freezegun is a pytest plugin that simplifies testing time-dependent code by integrating the `freezegun` library with pytest fixtures and marks. It allows you to freeze time during test execution using either a decorator or a dedicated fixture. The current version is 0.4.2, and it maintains a moderate release cadence, addressing compatibility and feature improvements.

pip install pytest-freezegun
INSTALL
IMPORT
SIG · PYTEST-FREEZEGUN
P
pytest-freezegun
testingpythonv0.4.2
Install
2.9s avg
Import
Disk
31MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 32.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.9s · import 0.000s · 33MB
31MB installed
● package 31MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pytest.mark.freeze_time
import pytest # accessed via pytest.mark
The 'freeze_time' mark is registered by the plugin and accessed via `pytest.mark`, not directly imported.
freezer (fixture)
def test_example(freezer):
The 'freezer' fixture is automatically provided by the plugin.

This quickstart demonstrates the two primary ways to freeze time with pytest-freezegun: using the `pytest.mark.freeze_time` decorator for a static time freeze on a test function, and using the `freezer` fixture to dynamically control time within a test, including advancing it.

import datetime import pytest @pytest.mark.freeze_time("2023-01-15 12:00:00") def test_frozen_time_decorator(): assert datetime.datetime.now() == datetime.datetime(2023, 1, 15, 12, 0, 0) def test_frozen_time_fixture(freezer): freezer.move_to("2024-02-29 10:30:00") assert datetime.datetime.now() == datetime.datetime(2024, 2, 29, 10, 30, 0) freezer.tick(datetime.timedelta(hours=1)) assert datetime.datetime.now() == datetime.datetime(2024, 2, 29, 11, 30, 0)
Debug
Known issues
breakingPython 3.5 and older are no longer supported. Versions prior to 0.4.1 dropped support for Python < 3.5. Version 0.3.0 dropped support for Python 3.3.
fix
Upgrade to Python 3.6+ to use pytest-freezegun 0.4.1+.
affects: <0.4.1
gotchaThe `freeze_time` mark was not automatically registered as a known mark in versions prior to 0.4.1. This could lead to pytest warnings about unknown marks.
fix
Upgrade to pytest-freezegun 0.4.1+ or explicitly register the mark in your `pytest.ini` using `markers = freeze_time`.
affects: <0.4.1
gotchaWhen both `@pytest.mark.freeze_time` and the `freezer` fixture are used in the same test or test scope, there were issues in older versions where `freeze_time` might not correctly freeze time in fixtures, or `freezer.move_to` might override unexpected values.
fix
Upgrade to pytest-freezegun 0.4.2+ which includes fixes for this interaction. Always ensure `freezer.move_to()` is called after `freeze_time` if dynamic adjustment is needed within a marked test.
affects: <0.4.2
gotchaSupport for class-based tests (e.g., using `@pytest.mark.freeze_time` on a test method within a class) was added in 0.4.1. Earlier versions might not apply the time freeze correctly to methods within test classes.
fix
Upgrade to pytest-freezegun 0.4.1+ for reliable support with class-based tests.
affects: <0.4.1
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pytest_freezegun'
The 'pytest-freezegun' package is not installed in the Python environment.
fix
Install the package using pip: 'pip install pytest-freezegun'.
AttributeError: module 'freezegun' has no attribute 'freeze_time'
The 'freezegun' package is not installed or not properly imported.
fix
Ensure 'freezegun' is installed ('pip install freezegun') and correctly imported: 'from freezegun import freeze_time'.
TypeError: freeze_time() missing 1 required positional argument: 'time_to_freeze'
The 'freeze_time' decorator is used without specifying the time to freeze.
fix
Provide a time string to the decorator: '@freeze_time("2023-01-01")'.
ValueError: Invalid time format: 'invalid-date'
An incorrect date format is passed to the 'freeze_time' decorator.
fix
Use a valid date format, such as 'YYYY-MM-DD': '@freeze_time("2023-01-01")'.
ImportError: cannot import name 'freeze_time' from 'freezegun'
The 'freezegun' package is not installed or the import statement is incorrect.
fix
Ensure 'freezegun' is installed ('pip install freezegun') and import it correctly: 'from freezegun import freeze_time'.
Upgrade
Version history
0.4.2latest on PyPI · released Jul 19, 2020
Audit
Dependencies
pytestrequiredCore testing framework pytest-freezegun extends.
freezegunrequiredThe underlying library used for time freezing.
Agent activity
10 hits · last 30 days
node
8
Resources
pytest-freezegun — pip install pytest-freezegun · libregistry