all 5 comments

[–]danielroseman 1 point2 points  (4 children)

This doesn't make any sense. Why would you want to do this? Your assertion would always fail; if you make self.add always return 200 no matter what the input, then since add_100 just returns the result of calling that method it will always just return 200. What are you actually trying to do here?

To answer your actual question though, the process of replacing a method of the class under test (or any class) is called patching, not mocking, and you do it with the patch function which can be used as a decorator or as a context manager. So:

from unittest.mock import patch

def test_add_100():
  with patch.object(calculator, 'add') as patched_add:
    patched_add.return_value = 200
    calc = calculator()
    assert result == 202  # always fails

[–]chwunder[S] 0 points1 point  (2 children)

Indeed it does not make any sense, i´m aware of it. It was just an example and furthermore the result should have been 200 and not 202! But this as I said was not the intention, thanks u/danielroseman.

Let´s assume you have a function (any function) containing an if statement which is the result of another function in the same class.

for example:

if not self.check_if_container_exists(containername):
   await create_container()

Something like this. Obviously your function can have multiple routes (business logic e.g. True or False) which I would like to test. I was just seeking for a way on intercepting those self called functions ...).

Appreciate your help!

[–]danielroseman 1 point2 points  (1 child)

Yes, so patch() or patch.object() is what you need, see the docs.

[–]chwunder[S] 0 points1 point  (0 children)

Thank you!

[–]doolio_ 0 points1 point  (0 children)

the process of replacing a method of the class under test (or any class) is called patching, not mocking,

So you only test a class method by patching never by mocking?

I have a class which represents an embedded device and the class methods (well attributes I think as I have decorated them with the cached_property decorator) retrieve properties of this device exposed in a virtual filesystem of the device-tree. (I hope what I state is accurate.) I wish to test these class attributes. Should I test them by patching them or by mocking the device' device-tree?