Skip to main content

Let your Python tests travel through time

Project description

FreezeGun: Let your Python tests travel through time
==================================


.. image:: https://secure.travis-ci.org/spulec/freezegun.png?branch=master
.. image:: 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(), and datetime.date.today() will return the time that has been frozen.

Decorator
~~~~~~~~~

.. code-block:: python

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
~~~~~~~~~~~~~~~

.. code-block:: python

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
~~~~~~~

.. code-block:: python

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
~~~~~~~~~

.. code-block:: python

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

.. code-block:: python

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

Installation
------------

To install FreezeGun, simply:

.. code-block:: bash

$ pip install 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.1.6.tar.gz (8.8 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for freezegun-0.1.6.tar.gz
Algorithm Hash digest
SHA256 6682cab8b1164696da2cc9f5a8db07d57a7a03b36d101d4b954a8138efc36069
MD5 0cb3a41e4d926124824106d3e648360c
BLAKE2b-256 8fe98fa5abdd2e13eebec983ed09e4baa99e41db596c0cdd6566861fb32c7947

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