Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Let your Python tests travel through time
Python

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
freezegun
tests
.gitignore
.travis.yml
LICENSE
Makefile
README.rst
requirements.txt
setup.cfg
setup.py

README.rst

FreezeGun: Let your Python tests travel through time

https://secure.travis-ci.org/spulec/freezegun.png?branch=master

FreezeGun is a library that allows you to travel in time with python by mocking the datetime module.

Installation

To install FreezeGun, simply:

$ pip install freezegun

Usage

Simple

from freezegun import freeze_time

freezer = freeze_time("2012-01-14")
freezer.start()
assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)
freezer.stop()

Decorator

from freezegun import freeze_time

@freeze_time("2012-01-14")
def test():
    assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)

Context Manager

from freezegun import freeze_time

def test():
    assert datetime.datetime.now() != datetime.datetime(2012, 01, 14)
    with freeze_time("2012-01-14"):
        assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)
    assert datetime.datetime.now() != datetime.datetime(2012, 01, 14)

Timezones

from freezegun import freeze_time

@freeze_time("2012-01-14", tz_offset=-4)
def test():
    assert datetime.datetime.now() == datetime.datetime(2012, 01, 14) - datetime.timedelta(hours=4)
    # datetime.date.today() uses local time
    assert datetime.date.today() == datetime.datetime(2012, 01, 13)
Something went wrong with that request. Please try again.