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

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.

Download

Sample Code (.zip)

951 bytes
Download

Course Slides (.pdf)

283.8 KB
Avatar image for Chris James

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!

Avatar image for David Alvarez

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.

Avatar image for markjohnwilson

markjohnwilson on June 24, 2020

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

Avatar image for najmastar8

najmastar8 on July 15, 2020

great course …thank you :)

Avatar image for alexign

alexign on Aug. 28, 2020

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

Avatar image for Ghani

Ghani on Oct. 27, 2020

Very informative course; thanks!

Avatar image for matiasm77

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!

Avatar image for alnah

alnah on Sept. 20, 2023

Hi Lee! ✌️

Thank you for the course! It certainly clarified many aspects of mocking for me, though it also raised several questions. My primary question is this: What’s the advantage of using the autospec or spec Module if it simply mirrors the changes in interfaces or dependencies? While it’s beneficial for catching misspellings, I don’t believe that should be its main purpose.

If I want to be more deliberate about specifying what I expect from my interfaces or which dependencies I’m using, it seems using a spec List would be more appropriate. Relying solely on autospec or spec Module might result in missing unintended changes, thus giving a false sense of robustness to my tests. What’s your take on this?

Thank you for your response.

Become a Member to join the conversation.