The Real Python Podcast cover image

PDFs in Python and Projects on the Raspberry Pi

The Real Python Podcast

00:00

How to Test a Function That Uses DateTime.Now

The subtitle of this article is Practical Dependency Injection in Python. It talks about how you would approach testing a function that uses DateTime.Now. You can't predict when you're going to run the test and what time DateTime. now should be returning. So there's a couple ways to do it, one would be using something like a mock where you could monkey patch the date time dot today function. That gets a little messy and may not be a good solution for you. Hecke recommends removing the dependency of the function on that date time dotToday function and turning it into a parameter.

Transcript
Play full episode

The AI-powered Podcast Player

Save insights by tapping your headphones, chat with episodes, discover the best highlights - and more!
App store bannerPlay store banner
Get the app