[Answer]-Django: testing at different time

1👍

For a start, you code is broken. CURRENT_TIME will never change as long as the server process is running. If you start the server up at 9am, CURRENT_TIME will always be 9am.

You should define a function, get_current_time, which returns datetime.datetime.now().time(), and call that from the view.

Now, in your test, you can monkey-patch that – either manually, or using the excellent mock library – to return the time of your choosing.

Leave a comment