mocking for testing assertions can then be used to verify the results returned by the unit under test while mock specific methods are typically used to verify (spy onthe methods defined on the mock mocking frameworks for python due to python' dynamic nature it is well suited to the construction of mock functionsmethods and objects in fact there are several widely used mocking frameworks available for python includingunittest mock the unittest mock (included in the python distribution from python onwardsthis is the default mocking library provided with python for creating mock objects in python tests pymox this is widely used making framework it is an open source framework and has more complete set of facilities for enforcing the interface of mocked class mocktest this is another popular mocking framework it has its own dsl (domain specific languageto support mocking and wide set of expectation matching behaviour for mock objects in the remainder of this we will focus on the unittest mock library as it is provided as part of the standard python distribution the unittest mock library the standard python mocking library is the unittest mock library it has been included in the standard python distribution since python and provides simple way to define mocks for unit tests the key to the unittest mock library is the mock class and its subclass magicmock mock and magicmock objects can be used to mock functionsmethods and even whole classes these mock objects can have canned responses defined so that when they are involved by the unit under test they will respond appropriately existing objects can also have attributes or individual methods mocked allowing an object to be tested with known state and specified behaviour to make it easy to work with mock objectsthe library provides the @unittest mock patch(decorator this decorator can be used to replace real functions and objects with mock instances the function behind the decorator can also be used as context manager allowing it to be used in with-as statements providing for fine grained control over the scope of the mock if required