Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pytest: mocking specific timestamp with datetime

This function will return one of 3 strings based on the date: new_year on January 1st, leap_day on February 29, and regular on every other day. How can we test it?

import datetime

def daily_task():
    now = datetime.datetime.now()
    print(now)
    if now.month == 1 and now.day == 1:
        return 'new_year'
    if now.month == 2 and now.day == 29:
        return 'leap_day'
    return 'regular'
import app

task_name = app.daily_task()
print(task_name)