When writing tests, this regularly occurs: you are testing not only your own code but also the interaction with external resources, such as hardware, databases, web hosts, servers, and others. Some of these can be run safely, but certain tests are too slow, too dangerous, or even impossible to run. In those cases, mock objects are your friends; they can be used to fake anything, so you can be certain that your code still returns the expected results without having any variation from external factors.
unittest.mock library provides two base objects,
MagicMock, to easily mock any external resources. The
Mock object is just a general generic mock object and
MagicMock is mostly the same, but it has all the ...