简体   繁体   中英

How to write a unit test for datetime functions

I have the following function below that returns the time as 00:00:00 . How can I write a unit test to verify this works?

def get_date() -> datetime:
    return datetime.combine(datetime.today(), datetime.min.time())
print(get_date())

You can do two things here:

  1. Convert the datetime object to a string and assert against it:

     assertEqual("00:00:00", get_date().time().isoformat())

    and If you want to assert against date:

     assertEqual(datetime.today().strftime('%Y-%m-%d'), get_date().date().isoformat())
  2. Directly assert datetime objects:

     assertEqual(datetime.min.time(), get_date().time()) assertEqual(datetime.today().date(), get_date().date())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM