Skip to content

Latest commit

 

History

History
22 lines (14 loc) · 683 Bytes

20221202140835.md

File metadata and controls

22 lines (14 loc) · 683 Bytes

testing datetimes

Mocking out datetimes can be tough!

Say you'd use @patch('datetime.date', your_datetime_mock_object) in your test, only to have it break when the import in the code changes from "import datetime" to "from datetime import date, timedelta" for example.

Luckily there is a library called FreezeGun that works around this issue by thoroughly faking Python datetimes. All you have to do is use its @freeze_time decorator.

import datetime

from freezegun import freeze_time

# function that returns tomorrow's date
from tomorrow import tomorrow

@freeze_time('2022-12-01')
def test_tomorrow():
    assert tomorrow() == datetime.date(2022, 12, 2)

#datetime