What Is a Side Effect?
Okay, so you just learned how to use this
.return_value attribute from the
Mock object to set the return value of a function. But functions are sometimes really complex, and mocking the return value just isn’t enough to write a good test. Functions often have something called side effects.
A side effect of a function is something that happens—for example, printing to standard output, making a request to a web server, raising an exception—something beyond just the return value can be considered a side effect of a function. So if you have a function like this
is_weekday()—it takes no parameters and it just returns a Boolean—we might have something that says
print('i am in is_weekday'), and say we wanted to print that to the screen.
Become a Member to join the conversation.