Created
February 3, 2015 13:29
-
-
Save ctheune/45c118a2be815009ba00 to your computer and use it in GitHub Desktop.
Timetravel in unit tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_something(now): | |
# now is a mock returned by a custom pytest fixture | |
with TimeTravel(now, datetime(2010, 1, 1)): | |
do_something_in_the_past() | |
do_something_at_the_regular_mocked_time() | |
class TimeTravel(object): | |
def __init__(self, mock, whereto): | |
self.mock = mock | |
self.whereto = whereto | |
def __enter__(self): | |
self.old = self.mock.return_value | |
self.mock.return_value = self.whereto | |
def __exit__(self, exc_type, exc_value, exc_tb): | |
self.mock.return_value = self.old | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment