Skip to main content

Python time mocking

Project description Documentation Status

time-travel - time and I/O mocking library

time-travel is a python library that helps users write deterministic tests for time sensitive and I/O intensive code.

time-travel supports python 2.7, 3.4, 3.5, 3.6 and pypy2 on both Linux and Windows.


$ pip install time_travel

Testing Time Sensitive Code

Imagine testing a state machine that times out after some time passes. One way to test it would be:

def test_state_timeout():
    assert sm.state == TIMEOUT

This is bad for several reasons:

  • Your test takes 5 seconds to run. That’s a no-no.
  • time.sleep() promises that the process will sleep x seconds at most. This test might fail randomly, depending on how sensitive your state machine is.

There’s nothing worse than a heisenbuild (well, perhaps a SLOW heisenbuild). Here’s a better way to do this using time-travel:

def test_state_timeout():
    with TimeTravel() as tt:
        tt.clock.time += 5
        assert sm.state == TIMEOUT

When the handle_event method is called it will probably check the time using one of time or datetime modules. These modules are patched by time-travel and return the value stored in TimeTravel.clock.time.

From now on, your time sensitive tests will run faster, accurately, and your build will be consistent.

Testing I/O Code

time-travel also mocks I/O event interfaces such as select and poll.

Testing code that uses select is easy - you just inject a real socket object and send data to it from your test code. But what about timeouts? Testing behaviour that occurs on timeout forces you to actually wait! That’s bananas!

Here’s how you’d do it with time-travel:

def test_select_timeout():
    with TimeTravel() as tt:
        sock = socket.socket()
        tt.add_future_event(2, sock,
        start = time.time()
        assert[sock], [sock], []) == ([], [sock], [])  # This will be satisfied after "2 seconds"
        assert time.time() == start + 2  # You see? 2 seconds!
        assert[sock], [sock], [], 100) == ([], [], [])  # This is the "timeout"
        assert time.time() == start + 2 + 100

Once again, this code will run instantly.

Oh yes, sock doesn’t even have to be a socket object :)

For detailed information and usage examples, see the full documentation. You know you want to.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for time_travel, version 1.1.2
Filename, size File type Python version Upload date Hashes
Filename, size time_travel-1.1.2-py3-none-any.whl (12.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size time_travel-1.1.2.tar.gz (9.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page