Time related issues are tricky part in software engineering. So they require proper testing.

  • parametrize tested methods/functions with explicit time passed - sometimes a bit kind of test-only usage, because inside anyway datetime.now() is used
  • mocking
  • using specialized library like freezgun for efficent time mocks

Two major options to use freezgun in your tests:

  • conext manager

  • decorator

other cool things

  • support for timezones
  • support for manual ticking and auto ticking with delta

more: https://github.com/spulec/freezegun