Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Return Value (Part 2)

00:00 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 datetime object.

00:15 The year is 2019, the month is January, the day—the 1st.

00:24 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 01. Uh, 19? Okay, um, 01 2019. There we go. Okay, so January, 2019, the 1st was a Tuesday.

00:53 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.

01:08 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 .today() method.

01:30 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 Mock object, datetime. For now, I’m just going to comment out this assertion. And we’ll clear the screen and run the program again.

01:56 And I have not imported Mock, so let me do that real quick, from unittest.mock import Mock. And run our program again.

02:08 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 print() line.

02:22 And the way we do this is you say datetime.todayso that’s the method we called here—

02:32 and then .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 .return_value of .today to be tuesday.

02:47 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 True.

02:59 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 True!

03:12 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, tuesday.

03:27 And let’s verify that this is working by changing the .return_value to saturday to represent a weekend, and we’ll see if our assertion fails.

03:38 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.

Avatar image for Tristan

Tristan on June 20, 2020

How would you set the return value of multiple chained method calls?

Avatar image for Valeriy Pomulev

Valeriy Pomulev on Aug. 8, 2020

Hello Lee,

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.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Aug. 10, 2020

While 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.

Avatar image for kamenyovchev

kamenyovchev on March 22, 2024

Hello, Your example doesn’t works here. This is the code:

from unittest.mock import Mock from datetime import datetime

tuesday = datetime(year=2019,month=1,day=1) saturday = datetime(year=2019,month=2,day=5)

datetime = Mock()

def is_weekday(): today = datetime.today() day_of_the_week = today.weekday() return (0 <= day_of_the_week < 5)

datetime.today.return_value = saturday assert is_weekday()

When I execute it, it doesn’t thrown an error.It’s looks like that datetime isn’t mokced in the is_wwekday() function.

Would you explain me why it doesn’t thrown an error if I set satarday as a in return value.

Thanks in advance!

Become a Member to join the conversation.