Skip to main content

A fast alternative to freezegun that wraps libfaketime.

Project description

python-libfaketime: fast date/time mocking

python-libfaketime is a wrapper of libfaketime for python.

import datetime

from libfaketime import fake_time, reexec_if_needed

# libfaketime needs to be preloaded by the dynamic linker.
# This will exec the same command, but with the proper environment variables set.
# You can also skip this and manually manage your env (see "How to avoid re-exec").
reexec_if_needed()

def get_tomorrow():
    return datetime.date.today() + datetime.timedelta(days=1)


@fake_time('2014-01-01 00:00:00')
def test_get_tomorrow():
    assert get_tomorrow() == datetime.date(2014, 1, 2)

It serves as a fast drop-in replacement for freezegun. Here’s the output of a totally unscientific benchmark on my laptop:

$ python benchmark.py
re-exec with libfaketime dependencies
timing 1000 executions of <class 'libfaketime.fake_time'>
0.021755 seconds

$ python benchmark.py freezegun
timing 1000 executions of <function freeze_time at 0x10aaa1140>
6.561472 seconds

Some brief details:

  • linux and osx, python 2/3

  • microsecond resolution

  • accepts datetimes and strings that can be parsed by dateutil

  • not threadsafe

  • will break profiling. A workaround: use libfaketime.{begin, end}_callback to disable/enable your profiler.

To install: pip install libfaketime.

Use with py.test

It’s easiest to reexec from inside the pytest_configure hook:

# conftest.py
from libfaketime import reexec_if_needed

def pytest_configure():
    reexec_if_needed()

How to avoid re-exec

Sometimes, re-exec does unexpected things. You can avoid those problems by preloading libfaketime yourself. The environment variables you need can be found by running python-libfaketime on the command line:

$ python-libfaketime
export LD_PRELOAD="/home/foo/<snip>/vendor/libfaketime/src/libfaketime.so.1"
export FAKETIME_DID_REEXEC=true

You can use them as such:

eval $(python-libfaketime)
nosetests  # ...or any other code that imports libfaketime

Changelog

Semantic versioning is used.

0.4.0

released 2016-04-02

  • freezegun’s tick() is now supported; see their docs for usage.

0.3.0

released 2016-03-04

  • invoking libfaketime from the command line will now print the necessary environment to avoid a re-exec.

0.2.1

released 2016-03-01

  • python 3 support

0.1.1

released 2015-09-11

0.1.0

released 2015-06-23

  • add global start/stop callbacks

0.0.3

released 2015-03-28

  • initial packaged release

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

libfaketime-0.4.0.tar.gz (37.4 kB view hashes)

Uploaded Source

Supported by

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