Skip to main content

Let your Python tests travel through time

Project description

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

FreezeGun is a library that allows your python tests to travel through time by mocking the datetime module.

Usage

Once the decorator or context manager have been invoked, all calls to datetime.datetime.now(), datetime.datetime.utcnow(), datetime.date.today(), time.time(), time.localtime(), time.gmtime(), and time.strftime() will return the time that has been frozen.

Decorator

from freezegun import freeze_time

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

# Or class based

@freeze_time("2012-01-14")
class Tester(object):
    def test_the_class(self):
        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)

Raw use

from freezegun import freeze_time

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

Timezones

from freezegun import freeze_time

@freeze_time("2012-01-14 03:21:34", tz_offset=-4)
def test():
    assert datetime.datetime.utcnow() == datetime.datetime(2012, 01, 14, 03, 21, 34)
    assert datetime.datetime.now() == datetime.datetime(2012, 01, 13, 23, 21, 34)

    # datetime.date.today() uses local time
    assert datetime.date.today() == datetime.datetime(2012, 01, 13)

Nice inputs

FreezeGun uses dateutil behind the scenes so you can have nice-looking datetimes

@freeze_time("Jan 14th, 2012")
def test_nice_datetime():
    assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)

Installation

To install FreezeGun, simply:

$ pip install freezegun

On Debian (Testing and Unstable) systems:

$ sudo apt-get install python-freezegun

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

freezegun-0.3.0.tar.gz (40.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

freezegun-0.3.0-py2-none-any.whl (5.2 kB view details)

Uploaded Python 2

File details

Details for the file freezegun-0.3.0.tar.gz.

File metadata

  • Download URL: freezegun-0.3.0.tar.gz
  • Upload date:
  • Size: 40.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for freezegun-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e03fb71f7cdf437658d048b26a3600eb37e44071462106455407459b315e1838
MD5 79c847ffa8e09c35bf9fe0220c776249
BLAKE2b-256 ec25cecfd81a6a1bd9efccb5b30b9ea52cc922ac1f91c5489330fe721ee669c0

See more details on using hashes here.

File details

Details for the file freezegun-0.3.0-py2-none-any.whl.

File metadata

File hashes

Hashes for freezegun-0.3.0-py2-none-any.whl
Algorithm Hash digest
SHA256 14b06d83266c7d327e5004ac57e6f9e33cecc1279f58cabf7135a1f33192b022
MD5 76052a24758ad242d720cb991426fffa
BLAKE2b-256 2e3242430fb6b8b6ce3e0db01e9e2859dfcdd40042f0553cb7809b547af08da3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page