Skip to content

Instantly share code, notes, and snippets.

@komuw
Created June 2, 2015 09:17
Show Gist options
  • Save komuw/2cbb0982528365b91bab to your computer and use it in GitHub Desktop.
Save komuw/2cbb0982528365b91bab to your computer and use it in GitHub Desktop.
fake(mock) datetime in python (django)
class DateTimefaker(datetime):
"""
fake datetime to use your own custom datetimes
"""
def __new__(cls, *args, **kwargs):
return datetime.__new__(datetime, *args, **kwargs)
class TestSomething(TestCase):
fixtures = ['test_data']
@mock.patch('module.tasks.datetime', DateTimefaker)
def test_something(self):
DateTimefaker.today = classmethod(lambda cls: datetime(2014, 2, 28))
client = Client()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment