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

Improving 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.

Locked learning resources

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

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

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

Unlock This Lesson

Already a member? Sign-In

00:00 In the previous lesson, I showed you how to use a specification to be explicit about what a mock object fakes out and what it does not. This is the last lesson in the course, and I’m going to summarize what got covered and point you at some other resources.

00:14 Python’s standard library includes a unittest framework, and within that framework, the mock module is there to help you dynamically replace code, and just why would you do that?

00:24 Well, to make your tests deterministic. When dealing with code that uses random number generators, dates, or resource-hungry things like network connections, you can use mock to fake out that behavior and force an expected result to test against. The Mock and MagicMock objects dynamically create methods on themselves when you invoke them.

00:44 So any method you call exists. A MagicMock is a subclass of Mock that also implements magic (or dunder) methods like __len__().

00:54 By default, Mock() returns a new instance of the parent object, but you can control this by setting the .return_value attribute on a method to return something specific.

01:06 Or instead, you can set the .side_effect method to cause an exception to happen, to iterate over a set of returnables, or to trigger a side-effect function that determines the result.

01:18 The patch() mechanism, which can be used as a decorator or a context manager, helps you dynamically replace a module, object, or function in your code with a MagicMock object.

01:28 You specify what to mock by providing a string that says what to replace. The trick is to remember that you’re specifying the item relative to the spot it is being replaced, which is almost always the module where it’s being used, not in the test code itself.

01:46 To avoid typos invoking an unwanted method on a Mock object, you can provide a specification to restrict what methods get faked out. Anything not in the spec will throw an error.

01:59 Testing is a pretty broad subject, and there are lots of tools out there to help you along. Some programmers feel like the standard library’s unit test is too verbose.

02:08 It takes too much typing and a lot of boilerplate. A popular alternative is the pytest library, which is a third-party library that uses decorated functions to mean less testing code.

02:20 This next tool isn’t testing-specific, but if you’re mucking around with dates a lot, Delorean is one of my favorite libraries. It’s named after the car used as a time machine in the Back to the Future movies, and has intuitive methods for mucking around with dates, including things like moving between time zones and calls such as last Tuesday.

02:40 This last one is one of mine. Waelstow is a small library with functions to help you write tests. I mentioned it in one of the lessons because it includes a function for discovering tests that allows you to only type in part of a test’s name.

02:53 There are also tools in there for capturing output, annotating exceptions with more information, and a context manager that temporarily stores the contents of a directory in case your tests muck around with it.

03:06 There’s always more content for you at Real Python. For example, if you’d like to learn more about the popular alternative testing library that I mentioned before, here’s a course and tutorial on pytest.

03:18 Instead of specifying your tests in a separate script, you can include them in your pydoc blocks. This tutorial shows you how and why you might do that.

03:28 Of course, you can’t code nowadays without someone suggesting a machine can help you, so why not learn how to use ChatGPT to help you write Python tests.

03:37 Finally, to go along with your testing skills, you might want to improve your debugging skills. There are loads of debuggers out there, but pdb is the one that comes with Python.

03:46 It’s a little old school and takes some getting used to, but it’s a powerful tool.

03:52 That’s all I have for the course. I hope you learned something new. Thanks for your attention. Oh yeah, I almost forgot: there were 127 jelly beans called Mock.

04:01 Oops. Make that 128.

Become a Member to join the conversation.