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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences 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.

Improve Your Tests With the Python Mock Object Library (Summary)

You’ve learned so much about mocking objects using unittest.mock!

Now, you’re able to:

  • Use Mock to imitate objects in your tests
  • Check usage data to understand how you use your objects
  • Customize your mock objects’ return values and side effects
  • patch() objects throughout your codebase
  • See and avoid problems with using Python mock objects

You have built a foundation of understanding that will help you build better tests. You can use mocks to gain insights into your code that you would not have been able to get otherwise.

Here’s one last disclaimer: Beware of overusing mock objects! It’s easy to take advantage of the power of Python mock objects and mock so much that you actually decrease the value of your tests.

If you’re interested in learning more about unittest.mock, then check out its excellent documentation.


Sample Code (.zip)

951 bytes


Course Slides (.pdf)

283.8 KB

Chris James on May 17, 2020

I learned about ‘autospec’ I’m going to use that in my code, it will be a big time saver.

mock.side_effect takes as list of return_values and exceptions, you can only do one callable side_effect at a time.

Unix has a utility called ‘cal’ and New Year’s Day 3030 is a Friday!

David Alvarez on June 19, 2020

I quite happy with this course. My previous attempt to try to understand mock module were unsuccessful, a lot of magic happening I didn’t understand. But Lee manage to make it accessible an at list get a glimpse of what is going on. Working usually with python as scripting language with heavy dependencies with software who run it, mock can be really useful to make test independent of this dependencies. I’m also like the way how Lee record this course. Have the opportunity of see him solve usual mistakes and difficulties everyone encounters in his day to day I think it help me in my learning journey.

markjohnwilson on June 24, 2020

Really happy with this, a nice explanation of Mock, Patch and Spec.

najmastar8 on July 15, 2020

great course …thank you :)

alexign on Aug. 28, 2020

After that course I can’t wait to start mocking %)! Lee, thank you very much!

Ghani on Oct. 27, 2020

Very informative course; thanks!

matiasm77 on Aug. 24, 2021

Now I know how to make my datetime testing not fail depending on hour, and of course all about mocking. Thanks!

Become a Member to join the conversation.