• Stars
    star
    447
  • Rank 97,040 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Travel through time in your tests.

time-machine

https://img.shields.io/github/actions/workflow/status/adamchainz/time-machine/main.yml?branch=main&style=for-the-badge https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge https://img.shields.io/pypi/v/time-machine.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

Travel through time in your tests.

A quick example:

import datetime as dt
from zoneinfo import ZoneInfo
import time_machine

hill_valley_tz = ZoneInfo("America/Los_Angeles")


@time_machine.travel(dt.datetime(1985, 10, 26, 1, 24, tzinfo=hill_valley_tz))
def test_delorean():
    assert dt.date.today().isoformat() == "1985-10-26"

For a bit of background, see the introductory blog post and the benchmark blog post.

Installation

Use pip:

python -m pip install time-machine

Python 3.7 to 3.11 supported. Only CPython is supported at this time because time-machine directly hooks into the C-level API.


Testing a Django project? Check out my book Speed Up Your Django Tests which covers loads of ways to write faster, more accurate tests. I created time-machine whilst writing the book.


Usage

If you’re coming from freezegun or libfaketime, see also the below section on migrating.

travel(destination, *, tick=True)

travel() is a class that allows time travel, to the datetime specified by destination. It does so by mocking all functions from Python's standard library that return the current date or datetime. It can be used independently, as a function decorator, or as a context manager.

destination specifies the datetime to move to. It may be:

  • A datetime.datetime. If it is naive, it will be assumed to have the UTC timezone. If it has tzinfo set to a zoneinfo.ZoneInfo instance, the current timezone will also be mocked.
  • A datetime.date. This will be converted to a UTC datetime with the time 00:00:00.
  • A float or int specifying a Unix timestamp
  • A string, which will be parsed with dateutil.parse and converted to a timestamp. Again, if the result is naive, it will be assumed to have the UTC time zone.

Additionally, you can provide some more complex types:

  • A generator, in which case next() will be called on it, with the result treated as above.
  • A callable, in which case it will be called with no parameters, with the result treated as above.

tick defines whether time continues to "tick" after travelling, or is frozen. If True, the default, successive calls to mocked functions return values increasing by the elapsed real time since the first call. So after starting travel to 0.0 (the UNIX epoch), the first call to any datetime function will return its representation of 1970-01-01 00:00:00.000000 exactly. The following calls "tick," so if a call was made exactly half a second later, it would return 1970-01-01 00:00:00.500000.

Mocked Functions

All datetime functions in the standard library are mocked to move to the destination current datetime:

  • datetime.datetime.now()
  • datetime.datetime.utcnow()
  • time.gmtime()
  • time.localtime()
  • time.clock_gettime() (only for CLOCK_REALTIME)
  • time.clock_gettime_ns() (only for CLOCK_REALTIME)
  • time.strftime()
  • time.time()
  • time.time_ns()

The mocking is done at the C layer, replacing the function pointers for these built-ins. Therefore, it automatically affects everywhere those functions have been imported, unlike use of unittest.mock.patch().

Usage with start() / stop()

To use independently, create an instance, use start() to move to the destination time, and stop() to move back. For example:

import datetime as dt
import time_machine

traveller = time_machine.travel(dt.datetime(1985, 10, 26))
traveller.start()
# It's the past!
assert dt.date.today() == dt.date(1985, 10, 26)
traveller.stop()
# We've gone back to the future!
assert dt.date.today() > dt.date(2020, 4, 29)

travel() instances are nestable, but you'll need to be careful when manually managing to call their stop() methods in the correct order, even when exceptions occur. It's recommended to use the decorator or context manager forms instead, to take advantage of Python features to do this.

Function Decorator

When used as a function decorator, time is mocked during the wrapped function's duration:

import time
import time_machine


@time_machine.travel("1970-01-01 00:00 +0000")
def test_in_the_deep_past():
    assert 0.0 < time.time() < 1.0

You can also decorate asynchronous functions (coroutines):

import time
import time_machine


@time_machine.travel("1970-01-01 00:00 +0000")
async def test_in_the_deep_past():
    assert 0.0 < time.time() < 1.0

Beware: time is a global state - see below.

Context Manager

When used as a context manager, time is mocked during the with block:

import time
import time_machine


def test_in_the_deep_past():
    with time_machine.travel(0.0):
        assert 0.0 < time.time() < 1.0

Class Decorator

Only unittest.TestCase subclasses are supported. When applied as a class decorator to such classes, time is mocked from the start of setUpClass() to the end of tearDownClass():

import time
import time_machine
import unittest


@time_machine.travel(0.0)
class DeepPastTests(TestCase):
    def test_in_the_deep_past(self):
        assert 0.0 < time.time() < 1.0

Note this is different to unittest.mock.patch()'s behaviour, which is to mock only during the test methods. For pytest-style test classes, see the pattern documented below.

Timezone mocking

If the destination passed to time_machine.travel() or Coordinates.move_to() has its tzinfo set to a zoneinfo.ZoneInfo instance, the current timezone will be mocked. This will be done by calling time.tzset(), so it is only available on Unix. The zoneinfo module is new in Python 3.8 - on older Python versions use the backports.zoneinfo package, by the original zoneinfo author.

time.tzset() changes the time module’s timezone constants and features that rely on those, such as time.localtime(). It won’t affect other concepts of “the current timezone”, such as Django’s (which can be changed with its timezone.override()).

Here’s a worked example changing the current timezone:

import datetime as dt
import time
from zoneinfo import ZoneInfo
import time_machine

hill_valley_tz = ZoneInfo("America/Los_Angeles")


@time_machine.travel(dt.datetime(2015, 10, 21, 16, 29, tzinfo=hill_valley_tz))
def test_hoverboard_era():
    assert time.tzname == ("PST", "PDT")
    now = dt.datetime.now()
    assert (now.hour, now.minute) == (16, 29)

Coordinates

The start() method and entry of the context manager both return a Coordinates object that corresponds to the given "trip" in time. This has a couple methods that can be used to travel to other times.

move_to(destination, tick=None)

move_to() moves the current time to a new destination. destination may be any of the types supported by travel.

tick may be set to a boolean, to change the tick flag of travel.

For example:

import datetime as dt
import time
import time_machine

with time_machine.travel(0, tick=False) as traveller:
    assert time.time() == 0

    traveller.move_to(234)
    assert time.time() == 234

shift(delta)

shift() takes one argument, delta, which moves the current time by the given offset. delta may be a timedelta or a number of seconds, which will be added to destination. It may be negative, in which case time will move to an earlier point.

For example:

import datetime as dt
import time
import time_machine

with time_machine.travel(0, tick=False) as traveller:
    assert time.time() == 0

    traveller.shift(dt.timedelta(seconds=100))
    assert time.time() == 100

    traveller.shift(-dt.timedelta(seconds=10))
    assert time.time() == 90

pytest plugin

time-machine also works as a pytest plugin. It provides a function-scoped fixture called time_machine that has one method, move_to(), which has the same signature as Coordinates.move_to(). This can be used to mock your test at different points in time and will automatically be un-mock when the test is torn down.

For example:

import datetime as dt


def test_delorean(time_machine):
    time_machine.move_to(dt.datetime(1985, 10, 26))

    assert dt.date.today().isoformat() == "1985-10-26"

    time_machine.move_to(dt.datetime(2015, 10, 21))

    assert dt.date.today().isoformat() == "2015-10-21"

If you are using pytest test classes, you can apply the fixture to all test methods in a class by adding an autouse fixture:

import time

import pytest


class TestSomething:
    @pytest.fixture(autouse=True)
    def set_time(self, time_machine):
        time_machine.move_to(1000.0)

    def test_one(self):
        assert int(time.time()) == 1000.0

    def test_two(self, time_machine):
        assert int(time.time()) == 1000.0
        time_machine.move_to(2000.0)
        assert int(time.time()) == 2000.0

escape_hatch

The escape_hatch object provides functions to bypass time-machine. These allow you to call the real datetime functions, without any mocking. It also provides a way to check if time-machine is currently time travelling.

These capabilities are useful in rare circumstances. For example, if you need to authenticate with an external service during time travel, you may need the real value of datetime.now().

The functions are:

  • escape_hatch.is_travelling() -> bool - returns True if time_machine.travel() is active, False otherwise.
  • escape_hatch.datetime.datetime.now() - wraps the real datetime.datetime.now().
  • escape_hatch.datetime.datetime.utcnow() - wraps the real datetime.datetime.utcnow().
  • escape_hatch.time.clock_gettime() - wraps the real time.clock_gettime().
  • escape_hatch.time.clock_gettime_ns() - wraps the real time.clock_gettime_ns().
  • escape_hatch.time.gmtime() - wraps the real time.gmtime().
  • escape_hatch.time.localtime() - wraps the real time.localtime().
  • escape_hatch.time.strftime() - wraps the real time.strftime().
  • escape_hatch.time.time() - wraps the real time.time().
  • escape_hatch.time.time_ns() - wraps the real time.time_ns().

For example:

import time_machine


with time_machine.travel(...):
    if time_machine.escape_hatch.is_travelling():
        print("We need to go back to the future!")

    real_now = time_machine.escape_hatch.datetime.datetime.now()
    external_authenticate(now=real_now)

Caveats

Time is a global state. Any concurrent threads or asynchronous functions are also be affected. Some aren't ready for time to move so rapidly or backwards, and may crash or produce unexpected results.

Also beware that other processes are not affected. For example, if you use SQL datetime functions on a database server, they will return the real time.

Comparison

There are some prior libraries that try to achieve the same thing. They have their own strengths and weaknesses. Here's a quick comparison.

unittest.mock

The standard library's unittest.mock can be used to target imports of datetime and time to change the returned value for current time. Unfortunately, this is fragile as it only affects the import location the mock targets. Therefore, if you have several modules in a call tree requesting the date/time, you need several mocks. This is a general problem with unittest.mock - see Why Your Mock Doesn't Work.

It's also impossible to mock certain references, such as function default arguments:

def update_books(_now=time.time):  # set as default argument so faster lookup
    for book in books:
        ...

Although such references are rare, they are occasionally used to optimize highly repeated loops.

freezegun

Steve Pulec's freezegun library is a popular solution. It provides a clear API which was much of the inspiration for time-machine.

The main drawback is its slow implementation. It essentially does a find-and-replace mock of all the places that the datetime and time modules have been imported. This gets around the problems with using unittest.mock, but it means the time it takes to do the mocking is proportional to the number of loaded modules. In large projects, this can take several seconds, an impractical overhead for an individual test.

It's also not a perfect search, since it searches only module-level imports. Such imports are definitely the most common way projects use date and time functions, but they're not the only way. freezegun won’t find functions that have been “hidden” inside arbitrary objects, such as class-level attributes.

It also can't affect C extensions that call the standard library functions, including (I believe) Cython-ized Python code.

python-libfaketime

Simon Weber's python-libfaketime wraps the libfaketime library. libfaketime replaces all the C-level system calls for the current time with its own wrappers. It's therefore a "perfect" mock for the current process, affecting every single point the current time might be fetched, and performs much faster than freezegun.

Unfortunately python-libfaketime comes with the limitations of LD_PRELOAD. This is a mechanism to replace system libraries for a program as it loads (explanation). This causes two issues in particular when you use python-libfaketime.

First, LD_PRELOAD is only available on Unix platforms, which prevents you from using it on Windows.

Second, you have to help manage LD_PRELOAD. You either use python-libfaketime's reexec_if_needed() function, which restarts (re-execs) your test process while loading, or manually manage the LD_PRELOAD environment variable. Neither is ideal. Re-execing breaks anything that might wrap your test process, such as profilers, debuggers, and IDE test runners. Manually managing the environment variable is a bit of overhead, and must be done for each environment you run your tests in, including each developer's machine.

time-machine

time-machine is intended to combine the advantages of freezegun and libfaketime. It works without LD_PRELOAD but still mocks the standard library functions everywhere they may be referenced. Its weak point is that other libraries using date/time system calls won't be mocked. Thankfully this is rare. It's also possible such python libraries can be added to the set mocked by time-machine.

One drawback is that it only works with CPython, so can't be used with other Python interpreters like PyPy. However it may possible to extend it to support other interpreters through different mocking mechanisms.

Migrating from libfaketime or freezegun

freezegun has a useful API, and python-libfaketime copies some of it, with a different function name. time-machine also copies some of freezegun's API, in travel()'s destination, and tick arguments, and the shift() method. There are a few differences:

  • time-machine's tick argument defaults to True, because code tends to make the (reasonable) assumption that time progresses whilst running, and should normally be tested as such. Testing with time frozen can make it easy to write complete assertions, but it's quite artificial. Write assertions against time ranges, rather than against exact values.
  • freezegun interprets dates and naive datetimes in the local time zone (including those parsed from strings with dateutil). This means tests can pass when run in one time zone and fail in another. time-machine instead interprets dates and naive datetimes in UTC so they are fixed points in time. Provide time zones where required.
  • freezegun's tick() method has been implemented as shift(), to avoid confusion with the tick argument. It also requires an explicit delta rather than defaulting to 1 second.
  • freezegun's tz_offset argument is not supported, since it only partially mocks the current time zone. Time zones are more complicated than a single offset from UTC, and freezegun only uses the offset in time.localtime(). Instead, time-machine will mock the current time zone if you give it a datetime with a ZoneInfo timezone.

Some features aren't supported like the auto_tick_seconds argument. These may be added in a future release.

If you are only fairly simple function calls, you should be able to migrate by replacing calls to freezegun.freeze_time() and libfaketime.fake_time() with time_machine.travel().

More Repositories

1

django-cors-headers

Django app for handling the server headers required for Cross-Origin Resource Sharing (CORS)
Python
5,087
star
2

django-htmx

Extensions for using Django with htmx.
JavaScript
866
star
3

django-upgrade

Automatically upgrade your Django projects.
Python
641
star
4

django-mysql

🐬 🐴 Extensions to Django for use with MySQL/MariaDB
Python
535
star
5

blacken-docs

Run `black` on python code blocks in documentation files
Python
513
star
6

flake8-comprehensions

❄️ A flake8 plugin to help you write better list/set/dict comprehensions.
Python
446
star
7

django-perf-rec

Keep detailed records of the performance of your Django code.
Python
330
star
8

django-browser-reload

Automatically reload your browser in development.
Python
296
star
9

mac-ansible

🐄 Configuring my mac with Ansible
Shell
170
star
10

patchy

⚓ Patch the inner source of python functions at runtime.
Python
163
star
11

apig-wsgi

Wrap a WSGI application in an AWS Lambda handler function for running on API Gateway or an ALB.
Python
146
star
12

django-linear-migrations

Ensure your migration history is linear.
Python
136
star
13

treepoem

Barcode rendering for Python supporting QRcode, Aztec, PDF417, I25, Code128, Code39 and many more types.
PostScript
115
star
14

ec2-metadata

An easy interface to query the EC2 metadata API, with caching.
Python
102
star
15

django-rich

Extensions for using Rich with Django.
Python
90
star
16

django-permissions-policy

Set the draft security HTTP header Permissions-Policy (previously Feature-Policy) on your Django app.
Python
81
star
17

django-watchfiles

Use watchfiles in Django’s autoreloader.
Python
81
star
18

django-read-only

Disable Django database writes.
Python
75
star
19

django-minify-html

Use minify-html, the extremely fast HTML + JS + CSS minifier, with Django.
Python
73
star
20

flake8-tidy-imports

❄️ A flake8 plugin that helps you write tidier imports.
Python
60
star
21

heroicons

Use heroicons in your Django and Jinja templates.
Python
58
star
22

SublimeFiglet

Add in ASCII text art from "figlet"
Python
46
star
23

lifelogger

📅 Track your life like a pro on Google Calendar via your terminal.
Python
40
star
24

pip-lock

Check for differences between requirements.txt files and your environment
Python
36
star
25

django-capture-on-commit-callbacks

Capture and make assertions on transaction.on_commit() callbacks.
Python
35
star
26

django-version-checks

System checks for your project's environment.
Python
34
star
27

django-jsonfield

(Maintenance mode only) Cross-database JSON field for Django models.
Python
30
star
28

unittest-parametrize

Parametrize tests within unittest TestCases.
Python
29
star
29

scripts

Useful little scripts that I use on commandline. Work in OS-X + zsh at least.
Shell
27
star
30

multilint

✅ Run multiple python linters easily
Python
27
star
31

flake8-no-pep420

A flake8 plugin to ban PEP-420 implicit namespace packages.
Python
22
star
32

django-startproject-templates

Python
22
star
33

pytest-is-running

pytest plugin providing a function to check if pytest is running.
Python
21
star
34

SublimeHTMLMustache

✏️ Adds HTML Mustache as a language to Sublime Text 2/3, with snippets.
19
star
35

pytest-reverse

Pytest plugin to reverse test order.
Python
19
star
36

owela-club

Play the Namibian game of Owela against a terrible AI. Built using Django and htmx.
Python
18
star
37

nose-randomly

👃 Nose plugin to randomly order tests and control `random.seed`
Python
17
star
38

talk-how-to-hack-a-django-website

JavaScript
14
star
39

dynamodb_utils

A toolchain for Amazon's DynamoDB to make common operations (backup, restore backups) easier.
Python
12
star
40

sound-resynthesis

🔈 Sound Resynthesis with a Genetic Algorithm - my final year project from university
Java
12
star
41

mariadb-dyncol

💾 Python dicts <-> MariaDB Dynamic Column binary format
Python
11
star
42

pre-commit-oxipng

Mirror of oxipng for pre-commit.
Rust
11
star
43

pytest-flake8dir

❄️ A pytest fixture for testing flake8 plugins.
Python
11
star
44

logentries-cli

📒 Get your logs from Logentries on the comandline.
Python
10
star
45

pre-commit-dprint

Mirror of dprint for pre-commit.
9
star
46

sublime-rst-improved

Python
8
star
47

h

Python
8
star
48

talk-improve-startup-time

“How to profile and improve startup time” talk
JavaScript
8
star
49

sublime_text_settings

✏️ My settings for sublime text 3 - as in Packages/User
Python
8
star
50

talk-django-and-htmx

JavaScript
7
star
51

django-settings-file

Python
7
star
52

tox-py

Adds the --py flag to tox to run environments matching a given Python interpreter.
Python
6
star
53

kwargs-only

A decorator to make a function accept keyword arguments only, on both Python 2 and 3.
Python
6
star
54

pytest-super-check

🔒 Pytest plugin to ensure all your TestCase classes call super() in setUp, tearDown, etc.
Python
6
star
55

django_atomic_celery

Atomic transaction aware Celery tasks for Django
Python
6
star
56

django-coverage-example

Python
5
star
57

django-pymysql-backend

A Django database backend for MySQL using PyMySQL.
Python
5
star
58

pytest-restrict

🔒 Pytest plugin to restrict the test types allowed
Python
5
star
59

talk-data-oriented-django

JavaScript
4
star
60

talk-speed-up-your-tests-with-setuptestdata

JavaScript
4
star
61

talk-django-and-web-security-headers

JavaScript
3
star
62

fluentd.tmLanguage

Syntax highlighting for Fluentd configuration files
3
star
63

django-ticket-33153

https://code.djangoproject.com/ticket/33153
Python
3
star
64

pytest-flake8-path

A pytest fixture for testing flake8 plugins.
Python
3
star
65

pygments-git

Pygments lexers for Git output and files
Python
3
star
66

django_atomic_signals

Signals for atomic transaction blocks in Django 1.6+
Python
3
star
67

flake8-no-types

A flake8 plugin to ban type hints.
Python
3
star
68

SublimeMoveTabs

✏️ A short plugin for Sublime Text 2 that allows rearrangement of tabs/'views' with the keyboard.
Python
3
star
69

dynamodb_local_utils

Automatically run DynamoDB Local on Mac OS X
Shell
3
star
70

workshop-evenergy-concurrency-and-parallelism

Python
2
star
71

talk-how-complex-systems-fail

Talk for the Papers We Love London meetup
TeX
2
star
72

google_lifelog

Making a lifelog on google calendar.
Python
2
star
73

talk-building-interactive-pages-with-htmx

JavaScript
2
star
74

workshop-idiomatic-python

Python
2
star
75

ansible-talk-custom-template-filters

My talk for the Ansible London Meetup in March 2015
TeX
2
star
76

talk-django-vs-flask

JavaScript
2
star
77

django-talk-factory-boy

Talk for London Django Meetup
TeX
2
star
78

ProgrammingInterview

Solving the problems posted on ProgrammingInterview on YouTube
Python
2
star
79

example-pre-commit-ci-lite

example
2
star
80

django-server-push-demo

Python
2
star
81

talk-django-capture-on-commit-callbacks

JavaScript
2
star
82

techblog

Filled with little coding notes and fixes.
2
star
83

adamchainz

👋
Python
2
star
84

SublimeCowsay

✏️🐮 A silly little Sublime Text plugin for 2 and 3 to allow you to quickly convert a text selection to a cow speech bubble via the brilliant cowsay utility.
Python
2
star
85

djceu2019-workshop

Python
2
star
86

talk-what-happens-when-you-run-manage.py-test

JavaScript
2
star
87

talk-technologies-that-will-be-around-in-21-years

JavaScript
2
star
88

django-demo-constraint-single-column-not-null

Python
2
star
89

django-harlequin

Launch Harlequin, the SQL IDE for your Terminal, with your Django database configuration.
Python
2
star
90

channels-bug-connection-closed

Reproduction for Channels bug
Python
1
star
91

workshop-concurrency-and-parallelism

Python
1
star
92

django-talk-duth

Django Under The Hood 2015 Summary
TeX
1
star
93

workshop-rest-api-django

Python
1
star
94

workshop-recommended-practices

Python
1
star
95

talk-django-3.2-test-features

JavaScript
1
star
96

workshop-profiling-and-debugging

Python
1
star
97

django-feature-policy-shim

1
star
98

kvkit

high-level python toolkit for ordered key/value stores
Python
1
star
99

phabricator-csv-import

Python
1
star
100

django-blue-green-example

Reproducing the technique from “Smooth Database Changes in Blue-Green Deployments” by Mariusz Felisiak.
Python
1
star