Created
September 2, 2011 14:23
-
-
Save pdc/1188720 to your computer and use it in GitHub Desktop.
Mocking time to do time-sensitive unit tests in Python
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
import unittest | |
from mock import * | |
from datetime import datetime, timedelta | |
import time # so we can override time.time | |
mock_time = Mock() | |
mock_time.return_value = time.mktime(datetime(2011, 6, 21).timetuple()) | |
class TestCrawlerChecksDates(unittest.TestCase): | |
@patch('time.time', mock_time) | |
def test_mock_datetime_now(self): | |
self.assertEqual(datetime(2011, 6, 21), datetime.now()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment