Warp time
Project description
Travel through time in your tests.
A quick example:
import datetime as dt
import time_machine
@time_machine.travel(0.0)
def test_unix_epoch_timestamp():
assert dt.date.today().isoformat() == "1970-01-01"
Installation
Use pip:
python -m pip install time-machine
Python 3.6 to 3.8 supported (CPython only).
Usage
travel(destination)
travel() is a class that allows movement to a given time specified by destination. It can be used independently, as a function decorator, or as a context manager.
destination specifies the time to move to. It may be:
A datetime.datetime. If it is naive, it will be assumed to have the UTC timezone.
A float or int specifying a Unix timestamp
A string, which will be parsed with dateutil.parse and converted to a timestamp.
To use independently, instantiate, then use start() to move to the destination time, and stop() to move back.
import datetime as dt
import time_machine
traveller = time_machine.travel(dt.datetime(1985, 11, 5))
traveller.start()
# It's the past!
assert dt.date.today() == dt.date(1985, 11, 5)
traveller.stop()
# We've gone back to the future!
assert dt.date.today() > dt.date(2020, 4, 29)
Once started, all datetime functions in the standard library are mocked to pretend the current time is that time:
datetime.datetime.now()
datetime.datetime.utcnow()
time.time()
time.gmtime()
time.localtime()
time.strftime()
At least two functions are currently missing:
time.clock_gettime
time.time_ns()
This mocking is at the C layer, replacing the function pointers for these built-ins. Therefore, it automatically affects everywhere those functions were imported.
Any other functions that make system calls to retrieve the clock time will not be affected, but these are rare. Most Python libraries use the above standard library functions.
Beware that time is global state. Any concurrent threads or async functions will also be affected. Some aren’t ready for time to move so rapidly and may crash or produce unexpected results. But other processes are not affected, for example if you use datetime functions in a client/server database, they will still return the real time.
Time “continues ticking,” so two calls to time.time() will return results separated by the time elapsed between them.
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
When used as a context manager, time is mocked during the with block:
def test_time_time():
with time_machine.travel(0.0):
assert EPOCH < time.time() < EPOCH + 1.0
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 datetime or time imports 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 call sites checking the time, you may need several mocks.
freezegun
Steve Pulec’s freezegun library is a popular solution. It provides a nice 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 to mock is linear to the number of loaded modules, making it several seconds to start in large projects.
It also can’t affect C extensions that call the standard library functions, including Cython. And it can be subverted even in Python by code that stores the standard library functions in data structures or local scopes.
libfaketime
Simon Weber’s python-libfaketime wraps the LD_PRELOAD library libfaketime. 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 it comes with the limitations of LD_PRELOAD (explanation). First, this is only available on Unix platforms, which prevents it from working on Windows. Seccond, you either use its reexec_if_needed() function, which restarts (re-execs) your tests’ process once while loading, or manually manage the LD_PRELOAD environment variable everywhere you run your tests. Re-execing breaks profilers, use of python -m pdb and similar, and other things that might wrap your test process. Manually managing the environment variable is a bit of overhead for each environment you want to run your tests in.
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 - all Python libraries I’ve seen use the standard library functions. And other python libraries can probably be added to the set detected and 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 use different mocking mechanisms there.
History
1.0.0b1 (2020-05-04)
First release on PyPI.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for time_machine-1.0.0b1-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e997e0a81cc3490dfce4ac6178cb455389a604210b8f1475e6c463b78df4e918 |
|
MD5 | 3c912c14c9788ef6614131390beda03a |
|
BLAKE2b-256 | 574ee12cda7916ba648f915cd0722f1f8f69a874d08acf8bc0ac6a1ca9b4cc49 |
Hashes for time_machine-1.0.0b1-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e3eb4a16421d9f961093c382ae6cbb1dc3f8b6ad29513e4c161b14a1d25d53b |
|
MD5 | 70231fd66c3089238b30cc1eb794fca2 |
|
BLAKE2b-256 | 5d1b0af304a114688e277963b416b13e31ee66ffd48f55ffac63867e5556fa49 |
Hashes for time_machine-1.0.0b1-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5c1ced4401385398be30df99df08680de635ead1860e17f96bd5d08b660e90f |
|
MD5 | a6b157d8af260cd56e189b2a53ce5a14 |
|
BLAKE2b-256 | 7d5fe366fce45c51dd2073bc5856d4115ab79301e7a7297308210a230958b054 |