[ ![Python logo](../_static/py.svg) ](https://www.python.org/)

Theme  Auto  Light  Dark

###  [ Table of Contents ](../contents.html)

  * ` unittest.mock  ` â mock object library 
    * Quick Guide 
    * The Mock Class 
      * Calling 
      * Deleting Attributes 
      * Mock names and the name attribute 
      * Attaching Mocks as Attributes 
    * The patchers 
      * patch 
      * patch.object 
      * patch.dict 
      * patch.multiple 
      * patch methods: start and stop 
      * patch builtins 
      * TEST_PREFIX 
      * Nesting Patch Decorators 
      * Where to patch 
      * Patching Descriptors and Proxy Objects 
    * MagicMock and magic method support 
      * Mocking Magic Methods 
      * Magic Mock 
    * Helpers 
      * sentinel 
      * DEFAULT 
      * call 
      * create_autospec 
      * ANY 
      * FILTER_DIR 
      * mock_open 
      * Autospeccing 
      * Sealing mocks 
    * Order of precedence of ` side_effect  ` , ` return_value  ` and _wraps_

####  Previous topic

[ ` unittest  ` â Unit testing framework ](unittest.html "previous chapter")

####  Next topic

[ ` unittest.mock  ` â getting started ](unittest.mock-examples.html "next
chapter")

###  This Page

  * [ Report a Bug ](../bugs.html)
  * [ Show Source ](https://github.com/python/cpython/blob/main/Doc/library/unittest.mock.rst)

###  Navigation

  * [ index ](../genindex.html "General Index")
  * [ modules ](../py-modindex.html "Python Module Index") | 
  * [ next ](unittest.mock-examples.html "unittest.mock â getting started") | 
  * [ previous ](unittest.html "unittest â Unit testing framework") | 
  * ![Python logo](../_static/py.svg)
  * [ Python ](https://www.python.org/) » 
  *   *   * [ 3.12.3 Documentation ](../index.html) » 
  * [ The Python Standard Library ](index.html) » 
  * [ Development Tools ](development.html) » 
  * [ ` unittest.mock  ` â mock object library ]()
  * | 
  * Theme  Auto  Light  Dark  | 

#  ` unittest.mock  ` â mock object library  Â¶

New in version 3.3.

**Source code:** [ Lib/unittest/mock.py
](https://github.com/python/cpython/tree/3.12/Lib/unittest/mock.py)

* * *

` unittest.mock  ` is a library for testing in Python. It allows you to
replace parts of your system under test with mock objects and make assertions
about how they have been used.

` unittest.mock  ` provides a core  ` Mock  ` class removing the need to
create a host of stubs throughout your test suite. After performing an action,
you can make assertions about which methods / attributes were used and
arguments they were called with. You can also specify return values and set
needed attributes in the normal way.

Additionally, mock provides a  ` patch()  ` decorator that handles patching
module and class level attributes within the scope of a test, along with  `
sentinel  ` for creating unique objects. See the  quick guide  for some
examples of how to use  ` Mock  ` ,  ` MagicMock  ` and  ` patch()  ` .

Mock is designed for use with [ ` unittest  ` ](unittest.html#module-unittest
"unittest: Unit testing framework for Python.") and is based on the âaction
-> assertionâ pattern instead of ârecord -> replayâ used by many mocking
frameworks.

There is a backport of  ` unittest.mock  ` for earlier versions of Python,
available as [ mock ](https://pypi.org/project/mock/) on PyPI.