Pytest: mocking datetime.date.today
The datetime class has other methods to retreive the date (and I could not find how to mock the function deep inside).
examples/pytest/mock-get-today/app.py
import datetime def get_today(): return datetime.date.today()
examples/pytest/mock-get-today/use_app.py
import app today = app.get_today() print(type(today)) print(today)
examples/pytest/mock-get-today/test_app.py
import app import datetime def test_new_year(monkeypatch): mydt = datetime.date class MyDate(): def today(): return mydt(2000, 1, 1) monkeypatch.setattr(app.datetime, 'date', MyDate) today = app.get_today() #print(today) assert str(today) == '2000-01-01' def test_leap_year(monkeypatch): mydt = datetime.date class MyDate(): def today(): return mydt(2004, 2, 29) monkeypatch.setattr(app.datetime, 'date', MyDate) today = app.get_today() #print(today) assert str(today) == '2004-02-29'