mardi 22 octobre 2019

How to test python methods that include datetime.now() for different dates?

I have some methods that make use of datetime.datetime.now() from the datetime library. I want to write unit tests for those methods as if now() is several different times (for example, 5th day of the month, 15th day of the month).

For example, let's say I have this method (really just an example, I don't have a method like this)

import datetime

def days_left_this_month():
    return 30 - datetime.datetime.now().day

I'd want to test that method for the 5th day of the month ie: days_left_this_month() == 25 if now() is on the 5th day of the month.

What is the best way to do this?

Aucun commentaire:

Enregistrer un commentaire