How To Unit Test Time Based Functions Without Adding Parameters
I created a function that returns seconds left until the next occurrence of that time, but I came across a problem writing a unit test for it. How do people test this type of funct
Solution 1:
You need to use mocking framework to isolate your UT from dependencies so your UT will have a consistence behavior.
Freezegun - is a good mocking library for times which I have been used.
Just install this library: pip install freezegun
The in your UT used the decorator @freeze_time
as the following:
@freeze_time("2018-06-03") deftest_time_left(self):
dt_now = datetime.now()
tm_5sec_future = (dt_now + timedelta(0, 5)).time()
self.assertEqual(dtime.get_time_left(tm_5sec_future), -5.0)
Post a Comment for "How To Unit Test Time Based Functions Without Adding Parameters"