After spending some time, of which ~ one hour for a stupid mistake:
- The fact that the generated module was incomplete (the linker did not complain about undefined characters when it did not deliver the MockTest object to it)
- It was eventually created because I forgot to add
public: in front of class members in MockTest.h ... X: ((((
and for some time, exploring the real problem, I finally came to the conclusion: the problem is not that the C ++ language (at least not directly), but lies in the intermediate layer (s) created by Swig .
More details
The MockTest class (the return method of the mockInput method that we are trying to patch ) is defined in the MockTest module ( !!!, which is MockTest.py !!! ), which is automatically generated by Swig (the command in my case was swig -o MockTest_wrap.cpp -python -c++ MockTest.i ). Here is its definition:
class MockTest(_object): __swig_setmethods__ = {} __setattr__ = lambda self, name, value: _swig_setattr(self, MockTest, name, value) __swig_getmethods__ = {} __getattr__ = lambda self, name: _swig_getattr(self, MockTest, name) __repr__ = _swig_repr def __init__(self): this = _MockTest.new_MockTest() try: self.this.append(this) except __builtin__.Exception: self.this = this __swig_destroy__ = _MockTest.delete_MockTest __del__ = lambda self: None def getInput(self): return _MockTest.MockTest_getInput(self) def mockInput(self, input): return _MockTest.MockTest_mockInput(self, input)
As you probably guessed, this mockInput is patch ed, not the one from C ++ (whose name is MockTest_mockInput and exported by its own module: _MockTest - the proper name _wrap_MockTest_mockInput defined in MocTest_wrap.cpp). Of course, since getInput calls MockTest_getInput , patch ing mockInput (the Python shell) has no effect (just as if you changed its body to say: return "123" ).
Here is an example of the code I prepared to better illustrate the behavior (as I mentioned in my comment, I am using Python 3.5.4). Please ignore the last 3 lines of code and output them until you read the following paragraph:
from unittest.mock import patch import MockTest if __name__ == "__main__": return_value = "value overridden by `patch`" mock_input_arg = "argument passed to `MockTest.mockInput`" mock_test = MockTest.MockTest() with patch("MockTest.MockTest.mockInput", return_value=return_value): print("`mock_test.getInput()` returned: \"{}\"".format(mock_test.getInput())) print("`mock_test.mockInput(\"{}\")` returned: \"{}\"".format(mock_input_arg, mock_test.mockInput(mock_input_arg))) print("\nDon't mind the following output for now...\n")
And the conclusion:
c:\Work\Dev\StackOverflow\q45934545>"c:\Install\x64\Python\Python\3.5\python.exe" dummy.py Enter you name `mock_test.getInput()` returned: "Hello hi person" `mock_test.mockInput("argument passed to `MockTest.mockInput`")` returned: "value overridden by `patch`" Don't mind the following output for now... Enter you name: `mock_test.getInput()` returned: "Hello hi person" `mock_test.mockInput("argument passed to `MockTest.mockInput`")` returned: "value overridden by `patch`"
Then I went on trying patch MockTest._MockTest.MockTest_mockInput , but I got the same result because I did not patch MockTest::mockInput (from MockTest.cpp), but _wrap_MockTest_mockInput (from MocTest_wrap.cpp).
Below is a table with all the layers between C ++ and Python code for mockInput (for getInput it is exactly the same):

As getInput calls mockInput ( layer 0 ), there should be a patch , but, unfortunately, is not available for Python.
The 1 st solution that comes to my mind is to patch ing getInput (layer 1) directly (if used in many places).