Patching target object with new argument results in error
See original GitHub issueHello!
I’m trying to patch these three objects like that:
@asynctest.patch('foo.bar.method_1', new=asynctest.CoroutineMock)
@asynctest.patch('foo.bar.method_2', new=asynctest.CoroutineMock)
@asynctest.patch('foo.bar.method_3')
async def test_foo_bar(self, mock_method_3, method_2, method_1):
So I’m using the new argument because the library doesn’t properly identify the patched object (async def statement) as a coroutine. When running the tests I get the following error:
args = (<tests.unit_tests.test_app.test_foo_bar.TestRunFooBar testMethod=test_foo_bar>, <CoroutineMock name='method_3' id='140075174886144'>)
keywargs = {}
extra_args = [<CoroutineMock name='method_3' id='140075174886144'>]
patchers_to_exit = [<asynctest.mock._patch object at 0x7f65cbb5a080>]
patch_dict_with_limited_scope = []
exc_info = (<class 'TypeError'>, TypeError("test_foo_bar() missing 2 required positional arguments: 'method_2' and 'method_1'",), <traceback object at 0x7f65cb0a58c8>)
patching = <asynctest.mock._patch object at 0x7f65cbb5a080>
arg = <class 'asynctest.mock.CoroutineMock'>
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Python mock patch argument `new` vs `new_callable`
When new is mock.DEFAULT , the mock object is a MagicMock instance precisely because the default value of new_callable is MagicMock .
Read more >unittest.mock — mock object library — Python 3.11.1 ...
. Create a new Mock object. Mock takes several optional arguments that specify the behaviour of the Mock object:
Read more >Right Way to Test, Mock, and Patch in Python - Medium
The target should be a string of the form 'package.module.ClassName' . The target is imported and the object it specifies is replaced with...
Read more >Edge Cases - Patching - Harmony
So a constructor is just an initialiser method that gets the newly empty obj as an argument to set the values of fields....
Read more >Understanding the Python Mock Object Library - Real Python
The target path was 'my_calendar.requests' which consists of the module name and the object. You also defined a new parameter for the test...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Great, thank you very much for explanation. I’m closing this issue now as I’ve resolved it.
newallows to specify which object will replace the original patched object. if this parameter is not provided, the default behavior is to use a new instance of CoroutineMock or MagicMock (depending of whether the target of the patch is a coroutine function or not).new_callableallows to specify a callable which returns the object that will replace the target of the patch. This one can be used to specify the type of the object that will replace the target, but it can be any kind of callable an can return something else than a mock.I try to avoid repeating the documentation of unittest in asynctest’ documentation, but that’s true that this is far from obvious. I’ll rework the doc a bit.