Return Value (Part 2)
Okay, so let’s first create a couple days so we have a solid source of truth of a weekend and a weekday. Let’s say
tuesday is a
00:15 The year is 2019, the month is January, the day—the 1st.
And just to prove to you that January 1st, 2019, was a Tuesday I’m going to use the
cal utility and look at
2019 1. So this is—oh, I guess
19? Okay, um,
01 2019. There we go. Okay, so January, 2019, the 1st was a Tuesday.
We’re creating a
datetime object that represents Tuesday, and then let’s do a weekend, so let’s say
saturday is going to be the 5th of January.
There we go. So now we have a Tuesday and a Saturday—a weekday and a weekend—captured as
datetime objects. In this case, we want our test to succeed whether it’s a weekday or not, so when we run our test on a weekend, we can change it to be a weekday by patching this
In other words, we want to say “
today is always going to be a weekday.” So first, let’s go ahead and mock
datetime. We’ll say
datetime is a
Mock object. And let’s print the
dir() representation of that
datetime. For now, I’m just going to comment out this assertion. And we’ll clear the screen and run the program again.
And I have not imported
Mock, so let me do that real quick,
from unittest.mock import Mock. And run our program again.
We’ve seen this before, all these methods here. The one we’re going to use is
'return_value' to set the return value of
datetime.today(). I’m going to clean up and delete this
And the way we do this is you say
datetime.today—so that’s the method we called here—
.return_value, we want it to be a weekday, and that’s why we created this
tuesday object up here. So let’s set the
.today to be
So, knowing that today is Sunday where I am right now, we’ve changed the return value to be
tuesday, so now if we assert that
today is a weekday, it should be
Let’s just go ahead and save this, we’ll clear the screen, and run our program again. And we get no output, which is good, which means that an exception didn’t get raised because
is_weekday() is now returning
So we’ve changed the return value of
datetime.today by patching the entire
datetime module to be a
Mock object and setting its
.return_value to a weekday,
And let’s verify that this is working by changing the
saturday to represent a weekend, and we’ll see if our assertion fails.
There we go—the
AssertionError. Now you’ve learned how you can use this
.return_value attribute of a
Mock object to allow more control of your code’s behavior when you’re running your tests.
Thank you for comprehensive course.
I am wondering why
today.weekday() (line 10) worked fine (i.e. returned values in range 0-6) despite you actually mocked the enclosing datetime object ?
I’d expected that it returned
None as in previous lessons.
today is a mocked object, Lee told it to return a predefined
datetime instance he created before:
tuesday = datetime(year=2019, month=1, day=1) ⋮ datetime.today.return_value = tuesday
When he calls
datetime.today().weekday(), a legit
datetime object, i.e.
tuesday, handles the job.
Become a Member to join the conversation.
Tristan on June 20, 2020
How would you set the return value of multiple chained method calls?