# Python Unit Testing with Mocks: Patching and Namespaces

DevFeed: [Python Unit Testing with Mocks: Patching and Namespaces](<https://devfeed.tech/articles/i-want-to-mock-with-you-20243.md>)

Original publisher: [Read original article](<http://anjana.dev/blog/i-want-to-mock-with-you/>)

Author: Anjana Sofia Vakil (contact@anjana.dev)

Published: 2016-06-16T00:00:00Z

Content type: tutorial

Language: en

Sources: [Mozilla Automation](<https://devfeed.tech/sources/mozilla-automation.md>)

Topics: [Mocking](<https://devfeed.tech/topics/mocking.md>), [Python](<https://devfeed.tech/topics/python.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [Library](<https://devfeed.tech/topics/library.md>), [PyPI](<https://devfeed.tech/topics/pypi.md>), [pip](<https://devfeed.tech/topics/pip.md>)

Tags: [install](<https://devfeed.tech/tags/install.md>), [library](<https://devfeed.tech/tags/library.md>), [mocking](<https://devfeed.tech/tags/mocking.md>), [patches](<https://devfeed.tech/tags/patches.md>), [pypi](<https://devfeed.tech/tags/pypi.md>), [python](<https://devfeed.tech/tags/python.md>), [testing](<https://devfeed.tech/tags/testing.md>), [unit-tests](<https://devfeed.tech/tags/unit-tests.md>)

## AI overview

A tutorial on using Python mocks in unit tests to isolate specific behavior, inspect method calls, simulate return values, and patch objects in the correct module namespace.

## Source excerpt

This post brought to you from Mozilla's London All Hands meeting - cheers! When writing Python unit tests, sometimes you want to just test one specific aspect of a piece of code that does multiple things. For example, maybe you're wondering: Does object X get created here? Does method X get called here? Assuming method X returns Y, does the right thing happen after that? Finding the answers to such questions is super simple if you use mock: a library which "allows you to replace parts of your system under test with mock objects and make assertions about how they have been used." Since Python 3.3 it's available simply as unittest.mock, but if you're using an earlier Python you can get it from PyPI with pip install mock. So, what are mocks? How do you use them? Well, in short I could tell you that a Mock is a sort of magical object that's intended to be a doppelgänger for some object in your code that you want to test. Mocks have special attributes and methods you can use to find out how your test is using the object you're mocking. For example, you can use Mock.called and .call_count to find out if and how many times a method has been called. You can also manipulate Mocks to simulate functionality that you're not directly testing, but is necessary for the code you're testing. For example, you can set Mock.return_value to pretend that an function gave you some particular output, and make sure that the right thing happens in your program. But honestly, I don't think I could give a better or more succinct overview of mocks than the Quick Guide, so for a real intro you should go read that. While you're doing that, I'm going to watch this fantastic Michael Jackson video: Oh you're back? Hi! So, now that you have a basic idea of what makes Mocks super cool, let me share with you some of the tips/tips/trials/tribulations I discovered when starting to use them. Patches and namespaces tl;dr: Learn where to patch if you don't want to be sad! When you import a helper module int