In some of my tests I am having a problem that they fail on Travis because of time and time zone problems, so I want to mock system time for my test. How can I do this?
There are two ways you can accomplish that:
Create function which you will call instead of datetime.datetime.now()
as suggested by Bruno, but here is different implementation:
import os
import datetime
def mytoday():
if 'MYDATE' in os.environ:
return datetime.datetime.strptime(os.getenv('MYDATE'), '%m-%d-%Y').date()
else:
return datetime.date.today()
Then, in your test, you just monkeypatch environment variable:
import datetime
def test_patched_date(monkeypatch):
monkeytest.setenv('MYDATE', '05-31-2014')
assert datetime.date.today() == datetime.date(2014, 5, 31)
Monkeypatch the datetime
function:
import datetime
import pytest
FAKE_TIME = datetime.datetime(2020, 12, 25, 17, 05, 55)
@pytest.fixture
def patch_datetime_now(monkeypatch):
class mydatetime:
@classmethod
def now(cls):
return FAKE_TIME
monkeypatch.setattr(datetime, 'datetime', mydatetime)
def test_patch_datetime(patch_datetime_now):
assert datetime.datetime.now() == FAKE_TIME