pytest-freezegun

0.4.2 · active · verified Sat Apr 11

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.

Warnings

Install

Imports

Quickstart

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)

view raw JSON →