I'm not completely sure if I understood your question, but I'll give it a try.
As described in the documentation, Mock objects (actually MagickMock instances) are created by using the patch decorator:
from unittest.mock import patch
@patch('some_module.some_object')
def test_something(mocked_object):
print(mocked_object)
This gives something like:
<MagicMock name='some_object' id='1870192381512'>
This is eqivalent to:
def test_something():
with patch('some_module.some_object') as mocked_object:
print(mocked_object)
This gives you the possibility to replace any object by a mock object to avoid calling the actual production code and/or to check how the original object is called (if the object is a function). The reason why using patch (or some similar methods) is preferred is that this ensures that the patch is reverted after the test (or after the context manager scope in the second case), so there are no side effects on other tests or other code.
To cite the documentation:
The patch decorators are used for patching objects only within the scope of the function they decorate. They automatically handle the unpatching for you, even if exceptions are raised. All of these functions can also be used in with statements or as class decorators.
You are also able to create a Mock object manually and assign it to an object - I assume this is what you mean in your question. If you do this instead of using patch, you are responsible to reset the previous state yourself. As this is more error-prone, I would advice to the dedicated methods for patching if possible.
Where this does not matter is in local objects and other mocks. Mocking local objects is seldom needed, but Mock instances are often created in conjunction with patching an object to preserve the instance of a mock object for later check:
@mock.patch('my_functions.MyClass')
def test_object(mock_class):
arg1 = Mock()
arg2 = Mock()
do_something(arg1, arg2)
# check that do_something creates MyClass with the given arguments
mock_class.assert_called_with(arg1, arg2)
In this case the case will only be used as an argument to a mock object, so no reset is needed.
To summarize:
patchis a convenience decorator/context manager function to replace objects with mock objects (or other objects) and reset the previous state after finishing or in case of an exceptionMockor derived objects are created bymock.patch, and can also be created manually. Manually created mocks are usually only used to patch local functions or other mocks where no reset is needed.
python - What is the difference between using mock.Mock() vs mock.patch(), and when to use one over the other? - Stack Overflow
What is mock and patch() for writing python unit tests ? Explain me like I'm 5 please?
Can someone ELI5 unittest.mock.patch()?
Is there a way to mock a script which does not have functions or classes
I am writing a unit test for the first time and I am supposed to figure this out by my self. I am utterly confused cuz I dont know the first thing about writing a unit test. Especially mock and patch().
-
What is it?
-
Why do we use it ?
-
What is the difference between mock and patch?
I've tried reading documentations on python website(https://docs.python.org/3/library/unittest.mock.html) , tried watching youtube videos( after which I feel a little familiarized. But it is still useless for me ). I really feel stuck right now. :(
Also, any resource that could help this noob write better unit tests would be greatly appreciated.
I'm a QA at a startup learning python for some backend test automation. Super newb to python and coding in general beyond UI testing with Android
I'm taking Udemy's "Automated Testing in Python" course to dive deep into things and the teacher just introduced "patching" methods and I'm having some trouble wrapping my head around what this actually does (also, why I might want to use them. He seemed to stress wrapping my head around it since it will be used a lot throughout the course.
Can someone give in ELI5? The docs are somewhat obscure is all. Thanks so much!