Posted on 26 Apr 2020
In Python, date, time and datetime classes provides a number of function to deal with dates, times and time intervals. Date and datetime are an object in Python, so when you manipulate them, you are actually manipulating objects and not string or timestamps. Whenever you manipulate dates or time, you need to import datetime function.
Consider the following python file, which has two methods
get_date_time() one returns the today date and other return today date with time now.
from datetime import date, datetime def get_today_date(): return date.today() def get_date_time(): return datetime.now()
We need to consider few thing before writing unittest for the function.
What i mean in the above is that, if we dont mock the datetime, our unittest will fail the next day since the datetime has changed and we didnt mock the functions.
freezegunis a awesome package.
FreezeGun is a library that allows your Python tests to travel through time by mocking the datetime module.
Once the decorator or context manager have been invoked, all calls to
time.strftime() will return the time that has been frozen.
When we use as a decorator using
mock, it replaces the given function/class. But its need more code and maintanence. But with freeze we can define the vaule easily.
Let your Python tests travel through time
pip install freezegun
from datetime import date, datetime from freezegun import freeze_time def get_today_date(): return date.today() def get_date_time(): return datetime.now() @freeze_time("2020-04-26") def test_get_today_date(): assert get_today_date() == date(2020, 4, 27) @freeze_time("2020-04-26") def test_get_date_time(): assert get_date_time() == datetime(2020, 4, 27, 0, 0)
Note: Works, but freezegun seems to be slow, especially if you have complicated logic with multiple calls for current time.
Check freezegun for more information.
Share your thoughts via twitter