Skip to content
Advertisement

How to use patch.object() correctly

I’m trying to get my head around Mock and patch(), but I’m stuck on a somewhat simple example. Say I have the following function in main.py, which tests if it’s a weekday or not.

JavaScript

I want to run my test with two possible outcomes: either False if I mock Saturday or Sunday or True if it’s a weekday. Now, I’m clearly not mocking anyting when calling main.is_weekday so my test currently fails as it’s the weekend. How can I fix that?

JavaScript

Advertisement

Answer

The essential problem is that you’re not patching datetime in your main module. The first argument to patch.object is the thing you want to patch, and since you’re passing in your datetime Mock object, that doesn’t do you any good.

I would restructure your test like this:

JavaScript

Here, we’re replacing main.datetime with a mock object, and then configuring it such that calling datetime.today() in the main module will return a specific date.

Then we test that the code works as expected for both weekdays and weekends.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement