Skip to content

Instantly share code, notes, and snippets.

@pdc
Created September 2, 2011 14:23
Show Gist options
  • Save pdc/1188720 to your computer and use it in GitHub Desktop.
Save pdc/1188720 to your computer and use it in GitHub Desktop.
Mocking time to do time-sensitive unit tests in Python
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