question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Patching target object with new argument results in error

See original GitHub issue

Hello!

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:closed
  • Created 6 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jeryinicommented, Jan 8, 2018

Great, thank you very much for explanation. I’m closing this issue now as I’ve resolved it.

0reactions
Martiuswebcommented, Jan 8, 2018

new allows 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_callable allows 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found