Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Return Value (Part 1)

00:00 One of the main reasons of using mock objects is to control the behavior of your code during tests. And one way to do this is to control the return value of a function, whether it’s one of your own or one of your dependencies.

00:16 Let’s look at an example of how we can control the return value of a function. Create a new file. I called mine calendar.py. And we’re going to import datetimerather from datetime import datetime.

00:31 It’s not really relevant or important in this course about mocks to learn about the datetime package, which is a whole other video course in itself, but we’re going to use one or two of these methods to help us explore this idea of changing the return value of a function.

00:47 Create a method called is_weekday(), and this is just going to basically tell us if today is a weekday or not. Let’s say today = datetime.today() and for exploratory purposes, let’s just go ahead and print today.

01:06 And we’ll call our function is_weekday() down here. Let’s head down into the terminal and run this program calendar.py and we’ll see that this returns today’s date, which is December 1st, 2019.

01:23 And we’ll create another variable called day_of_the_week and this is going to be today.weekday(). The .weekday() method will return an integer from 0 to 6.

01:35 0 is going to be Monday, and then 6 will be Sunday.

01:41 And let’s just verify that. We’ll print day_of_the_week,

01:46 run the program again, and today is 6, which is Sunday, and yeah—it is actually Sunday where I am right now. So the purpose of this method is to return True if today is a weekday and False if it’s a weekend today.

02:01 So essentially, we want the day of the week to be between 0 and 5.

02:07 So let’s say return, 0

02:11 so, day_of_the_week is greater than or equal to 0, so Monday. And we want it to be less than Saturday, which is 5.

02:23 I’m going to clean up here and delete these two lines—the comment and the print statement. And then let’s go ahead and print this return value of is_weekday().

02:34 It is Sunday today, so I expect this to be False. I’m going to clear the screen and run the calendar.py and it’s False. Yours might return True depending on what day of the week you’re writing this program. Cool.

02:49 Let’s write a test for is_weekday(), so instead of printing it I’m just going to say assert is_weekday(). Save that and run calendar.py again, and we get an AssertionError because is_weekday() is returning False because today is Sunday. Again, your results may be different. When you’re writing tests, you want the results to be predictable.

03:16 Whether it’s a weekend or a weekday, we want our results to be the same every time we run our tests. And that’s really important, especially when you’re using something like continuous integration and continuous delivery—you don’t want your test to fail just because today is Sunday. In the next video, we’ll look at how we can use mocks to set the return value of this function.

Become a Member to join the conversation.