spulec / freezegun

Let your Python tests travel through time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FreezeGun: Let your Python tests travel through time

image

image

image

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. time.monotonic() and time.perf_counter() will also be frozen, but as usual it makes no guarantees about their absolute value, only their changes over time.

Decorator

Context manager

Raw use

Timezones

Nice inputs

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

Function and generator objects

FreezeGun is able to handle function and generator objects.

tick argument

FreezeGun has an additional tick argument which will restart time at the given value, but then time will keep ticking. This is an alternative to the default parameters which will keep time stopped.

auto_tick_seconds argument

FreezeGun has an additional auto_tick_seconds argument which will autoincrement the value every time by the given amount from the start value. This is an alternative to the default parameters which will keep time stopped. Note that given auto_tick_seconds the tick parameter will be ignored.

Manual ticks

FreezeGun allows for the time to be manually forwarded as well.

Moving time to specify datetime

FreezeGun allows moving time to specific dates.

Parameter for move_to can be any valid freeze_time date (string, date, datetime).

Default arguments

Note that FreezeGun will not modify default arguments. The following code will print the current date. See here for why.

Installation

To install FreezeGun, simply:

On Debian systems:

Ignore packages

Sometimes it's desired to ignore FreezeGun behaviour for particular packages (i.e. libraries). It's possible to ignore them for a single invocation:

By default FreezeGun ignores following packages:

It's possible to set your own default ignore list:

Please note this will override default ignore list. If you want to extend existing defaults please use:

About

Let your Python tests travel through time

License:Apache License 2.0


Languages

Language:Python 99.7%Language:Makefile 0.3%