What Is Mocking?

00:00 All right, let’s first start with a definition of what mocking is.

00:05 You may have heard of this word mocking in a context like, “Oh, that person is mocking me. They’re trying to imitate me or trying to replicate some behavior that I have.”

00:17 It’s essentially creating a fake object that represents a real object.

00:23 Mocking is used in testing environments. A lot of times you have a part of your program that does something really important, like for example, a bank may have a function that updates someone’s account balance. When you’re testing this function—which you obviously want to test it to make sure it works correctly—when you’re testing it, you don’t want to actually change a person’s account balance, so you can create a mock of a person’s account balance, and you can alter that mock.

00:55 Or you can create a mock of that function and have it update some other piece of data that’s not an actual person’s account. Mocking allows more control over your code’s behavior. In Python, mock objects provide deeper insight into your code.

01:13 You can use mock objects to see when functions were called in your program, how many times they were called—sometimes your function may be called less than you expect or more than you expect—and you can also see what arguments were passed to a function when it was called. So as you’re going to see throughout this course, mock objects are really powerful not only for imitating certain behavior to avoid actual real-world changes, but to even provide deeper insight into your code so you can have a better understanding of how your program runs. In the next video, you’ll learn how to import the mock library and start creating mock objects.

Become a Member to join the conversation.