AFAIK, you can't mock builtin methods.
One approach I have often done is to change my code a bit to not use datetime
directly to obtain the date, but a wrapper function somewhere:
# mymodule.py
def get_today():
return datetime.date.today()
This makes it trivial to just mock
it in your test:
def test_something():
with mock.patch('mymodule.get_today', return_value=datetime.date(2014, 6, 2)):
...
You can also use the freezegun module.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…