Quantcast
Channel: Python Mock object with method called multiple times - Stack Overflow
Browsing all 6 articles
Browse latest View live

Answer by Tomasz Bartkowiak for Python Mock object with method called...

As in here, apart from using side_effect in unittest.mock.Mock you can also use @mock.patch.object with new_callable, which allows you to patch an attribute of an object with a mock object. Let's say a...

View Article


Answer by abourget for Python Mock object with method called multiple times

A little sweeter: mockobj.method.side_effect = lambda x: {123: 100, 234: 10000}[x] or for multiple arguments: mockobj.method.side_effect = lambda *x: {(123, 234): 100, (234, 345): 10000}[x] or with a...

View Article


Answer by Addison for Python Mock object with method called multiple times

I've ran into this when I was doing my own testing. If you don't care about capturing calls to your methodfromdepclass() but just need it to return something, then the following may suffice: def...

View Article

Answer by k.parnell for Python Mock object with method called multiple times

Try side_effect def my_side_effect(*args, **kwargs): if args[0] == 42: return "Called with 42" elif args[0] == 43: return "Called with 43" elif kwarg['foo'] == 7: return "Foo is seven"...

View Article

Python Mock object with method called multiple times

I have a class that I'm testing which has as a dependency another class (an instance of which gets passed to the CUT's init method). I want to mock out this class using the Python Mock library. What I...

View Article


Answer by Douglas Morais for Python Mock object with method called multiple...

You can use Side Effect + Dict Dispatch Pattern for a more clean code:For example (using async, ignore future objects for non async cases)@pytest.fixture()def mock_fixture(mocker): # dict to switch...

View Article
Browsing all 6 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>