Month: November 2019

Mock time in Python unit tests

Published on: 27.11.2019

From my point of view, unit tests are useful, especially for refactoring functions that have clearly defined inputs and outputs.

One thing that is impossible to test is anything regarding time/date code because whenever you run it is a different time 🙂 .

For example, if you have a function that calculates the number of days between two dates, the only way to test is to somehow mock current time/date.

in Python, there is FreezeGun.

It is implemented as a decorator, which is handy and readable in code.

So far I have not found any problem with FreezeGun.