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.
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 All right! You’ve made it to the end, the final video in this course, and I just want to say congratulations on making it this far. Hopefully you’ve learned more about testing and the mock library and how to use mocks to imitate things and create a more controlled testing environment.
00:17
You’re now able to use Mock
to imitate objects in your tests. You learned about the mocking library and the Mock
class. You can check usage data to understand how to use your objects—how your methods and your functions are being used throughout your code.
00:36
You can customize your mock objects’ return values and side effects to create a more controlled testing environment. You’re now able to use the patch()
method to patch objects throughout your codebase.
00:49
And hopefully, you can see and avoid problems with using Python Mock
objects. So once again, congratulations, and I hope you found this course useful and I will see you in the next one!
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!
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.
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 ofreturn_values
and exceptions, you can only do one callableside_effect
at a time.Unix has a utility called
cal
and New Year’s Day 3030 is a Friday!