Why this mocking script doesn't work (unittest, Mocker, python) - python

I want to mock MyClass.toBeMockedMethod which is called in MyClass.mymethod using the following script. It runs without actually mocking it. Not sure why....
class Test_mytest(MockerTestCase):
def mock_it_up(self, function, result=None, mmin=0, mmax=None):
function = self.m.mock()
function(ARGS)
self.m.result(result)
self.m.count(mmin, mmax)
def setUp(self):
self.m = Mocker()
self.mock_it_up(MyClass.toBeMockedMethod)
self.o=Myclass(0)
def test_one_atom(self):
self.o.mymethod()
def tearDown(self):
pass
if __name__ == '__main__':
main()

As with this question what you're really trying to do is patch your instance of MyClass. If MyClass is a new-style class then you can do this:
class Test_mytest(MockerTestCase):
def mock_it_up(self, function, result = None, mmin = 0, mmax = None):
methodToMock = getattr(self.p, function)
methodToMock()
self.m.result(result)
self.m.count(mmin, mmax)
def setUp(self):
self.m = Mocker()
self.o = MyClass(0)
self.p = self.m.patch(self.o)
self.mock_it_up('toBeMockedMethod')
# Put more calls to mock_it_up here.
self.m.replay()
def test_one_atom(self):
self.o.mymethod()
This will modify self.o so that calls to toBeMockedMethod are mocked.
However, if MyClass is not a new-style class then patching won't work. In this case, you can use type simulation to trick MyClass into doing what you want. For example:
class Test_mytest(MockerTestCase):
def mock_it_up(self, function, result = None, mmin = 0, mmax = None):
methodToMock = getattr(self.mockObj, function)
methodToMock()
self.m.result(result)
self.m.count(mmin, mmax)
def setUp(self):
self.m = Mocker()
self.o = MyClass(0)
self.mockObj = self.m.mock(MyClass)
self.mock_it_up('toBeMockedMethod')
# Put more calls to mock_it_up here.
self.m.replay()
def test_one_atom(self):
MyClass.mymethod(self.mockObj)
Note that the mocker's mock method is called with the class to be type-simulated. Later, instead of calling self.o.mymethod() we call MyClass.mymethod(...). Now MyClass.mymethod() expects an instance of MyClass as its first argument, but fortunately the mock object is masquerading as an instance of MyClass so the call goes through. When mymethod() calls toBeMockedMethod() it will actually call the mocked method, not the real method.
I quickly hacked up an test MyClass like this:
class MyClass():
def __init__(self, x):
self.x = x
def toBeMockedMethod(self):
print "Not Mocked!"
def mymethod(self):
self.toBeMockedMethod()
and when I ran this code as a unit test I got:
.
----------------------------------------------------------------------
Ran 1 test in 0.002s
OK
which is the desired result.

Related

override a method of a class with an method from another class

I have following code:
class SomeClass:
def __init__(self) -> None:
pass
def some_class_function(self, par):
print(par)
class SomeOtherClass:
def __init__(self) -> None:
pass
def some_other_class_function(self, par):
print(par+1)
if __name__ == "__main__":
sc = SomeClass()
sc.some_class_function = SomeOtherClass.some_other_class_function
sc.some_class_function(1)
When I execute the code I get
TypeError: some_other_class_function() missing 1 required positional argument: 'par'
How can I override the method of the first class with the method of the second class properly?
As you have noted in the comments, you are interested in adding method that will use sc as the "self" instance.
To that end, see this post. To summarize, you can either add a function to the class definition (affecting future instances of the same class), or bind the function to the particular instance.
As an example, consider the following class and function.
class Test():
def __init__(self):
self.phrase = "hello world"
def func(self):
print("this is the old method")
def test_func(self):
print(self.phrase)
For the first approach, we could do the following
test = Test()
Test.func = test_func
test.func()
Note that future instances of Test will have this function as an attribute. For example, running Test().func() will still result in the same output, even though the method is being used on a new class instance.
For the second, we could do the following.
import types
test = Test()
test.func = types.MethodType(test_func, test)
test.func()
In this case, running the line Test().func() will result in the output "this is the old method" because func has not been overwritten for new instances.
You need to initialize the class to call its method.
sc = SomeClass()
sco = SomeOtherClass() # initialize the second call to call it's method
sc.some_class_function = sco.some_other_class_function
sc.some_class_function(1)

How to unit test a method called within __init__() function?

I am trying to test a class method which is called within an__init__ function.
class abc:
def __init__(path):
list = []
foo(path)
bar('hello') # test function bar
def foo(self, path):
# does a bunch of stuff and creates internal list
list =
def bar(self):
# does a bunch of stuff and uses list
I would like to write a test for method bar here which I guess must be called through an instance of class abc. I can mock list array for this test, but cannot understand how to avoid the call to foo().
Just mock foo method for the time of testing bar. You can use patch.object.
A full example below:
import unittest
from unittest.mock import patch
class MyClass:
def __init__(self, path):
self.list = []
self.foo(path)
self.bar('/init')
def foo(self, path):
self.list.append(path)
def bar(self, path):
self.list.insert(0, path)
class MyTestClass(unittest.TestCase):
#patch.object(MyClass, 'foo')
def test_bar_decorated(self, mock):
a = MyClass('/foo')
a.bar('/bar')
self.assertEqual(a.list, ['/bar', '/init']) # .foo() wasn't invoked
if __name__ == '__main__':
unittest.main()
Notice that, a mock is created for you and passed in as an extra argument to the decorated function (we don't use it in this test). To avoid that you can use context manager version of patch.object:
def test_bar_context_manager(self):
with patch.object(MyClass, 'foo'):
a = MyClass('/foo')
a.bar('/bar')
self.assertEqual(a.list, ['/bar', '/init']) # same behaviour

Mocking a class method and changing some object attributes in Python

I am new to mock in Python. I want to know how to replace (mock) a class method while testing with another one, knowing that the original just changes some attributes of self without returning any value. For example:
def some_method(self):
self.x = 4
self.y = 6
So here I can't just change the return_value of the mock. I tried to define a new function (that should replace the original) and give it as side_effect to the mock. But how can I make the mocking function change attributes of the object in the class.
Here is my code:
#patch('path.myClass.some_method')
def test_this(self,someMethod):
def replacer(self):
self.x = 5
self.y = 16
some_method.side_effect = replacer
So how does Python now understands the self argument of replacer? Is that the self of the test class, or the self as the object of the class under test?
Apologies in advance if I don't understand what you are trying to do, but I think this might work:
import unittest
from unittest.mock import patch
class MyClass:
def __init__(self):
self.x = 0
self.y = 0
def some_method(self):
self.x = 4
self.y = 6
class OtherClass:
def other_method(self):
self.x = 5
self.y = 16
class MyTestClass(unittest.TestCase):
#patch('__main__.MyClass.some_method', new=OtherClass.other_method)
def test_patched(self):
a = MyClass()
a.some_method()
self.assertEqual(a.x, 5)
self.assertEqual(a.y, 16)
def test_not_patched(self):
a = MyClass()
a.some_method()
self.assertEqual(a.x, 4)
self.assertEqual(a.y, 6)
if __name__ == "__main__":
unittest.main()
This replaces some_method() with other_method() when patched, which sets different values for attributes x, y, and when the test is run, it gives the results:
..
----------------------------------------------------------------------
Ran 2 tests in 0.020s
OK
EDIT: to answer question about how to do inside the test function without mocking a class...
def test_inside_patch(self):
def othermethod(self):
self.x = 5
self.y = 16
patcher = patch('__main__.MyClass.some_method', new=othermethod)
patcher.start()
a = MyClass()
a.some_method()
self.assertEqual(a.x, 5)
self.assertEqual(a.y, 16)
patcher.stop()
Make sure you call start() and stop() on the patcher otherwise you can get into a situation where the patch is active and you don't want it to be. Note that to define the mock function inside the test code function, I didn't use patch as a decorator, because the mock function has to be defined before using the 'new' keyword in patch. If you want to use patch as a decorator you have to define the mock function someplace before the patch, defining it inside of MyTestClass also works, but it seems you really want to have the mock function defined inside your test function code.
EDIT: added summary of 4 ways I see to do this...
# first way uses a class outside MyTest class
class OtherClass:
def other_method(self):
...
class MyTest(unittest.TestCase):
#patch('path_to_MyClass.some_method', new=OtherClass.other_method)
def test_1(self)
...
# 2nd way uses class defined inside test class
class MyOtherClass:
def other_method(self):
...
#patch('path_to_MyClass.some_method', new=MyOtherClass.other_method)
def test_2(self):
...
# 3rd way uses function defined inside test class but before patch decorator
def another_method(self):
...
#patch('path_to_MyClass.some_method', new=another_method)
def test_3(self):
...
# 4th way uses function defined inside test function but without a decorator
def test_4(self):
def yet_another_method(self):
...
patcher = patch('path_to_MyClass.some_method', new=yet_another_method)
patcher.start()
...
patcher.stop()
None of these uses a side_effect, but they all solve the problem of mocking a class method and changing some attributes. Which one you choose depends on the application.

How to make unit tests for a methods in complicated class in python?

I would like to create unit test for filename_to_txt method:
class some_panel(wx.Panel):
def __init__(self,parent,Some_class,some_handler,Some_Event):
wx.Panel.__init__(self,parent=parent)
self.parent = parent
self.some_handler = some_handler
self.some_Event = Some_Event
self.some_another_class = Some_class
def filename_to_txt(self,input_filename):
splitted = input_filename.split(".raw")
txt_filename = splitted[0] + splitted[1] + ".txt"
return txt_filename
How should I write unit test for that method?
It is static method in some class, but to test that firstly I have to make and instance of Some_panel class.
Do I really have to provide all arguments for __init__ method of Some_panel class?? Is there another better approach, for which I don't have to make an instance of that Some_panel class to test filename_to_txt method.
import unittest
from some_file import Some_panel
class TestSomething(unittest.TestCase):
def testname(self):
some_panel = Some_panel( ???? )
testfilename = "TestFilename.raw.001"
result = some_panel.filename_to_txt(input_filename = testfilename)
self.assertEqual(result, "TestFilename.001.txt", "Something is wrong")
If it's a static method, remove the self argument and decorate it with the #staticmethod decorator. You can then call it as a pure function using some_panel.filename_to_text(filename).
I think the best approach would be to stub out the SomePanel class.
class SomePanelStub(SomePanel):
def __init__(self):
self.parent = None
self.some_handler = None
self.some_Event = None
self.some_another_class = None
Now inside your unittest Class
def SetUp(self):
self.helper = SomePanelStub()
Now you have stubbed out SomePanel and can easily mock out any dependencies.

How do you mock patch a python class and get a new Mock object for each instantiation?

OK,
I know this is mentioned in the manual, and probably has to do with side_effect and/or return_value, but a simple, direct example will help me immensely.
I have:
class ClassToPatch():
def __init__(self, *args):
_do_some_init_stuff()
def some_func():
_do_stuff()
class UUT():
def __init__(self, *args)
resource_1 = ClassToPatch()
resource_2 = ClassToPatch()
Now, I want to unit test the UUT class, and mock the ClassToPatch. Knowing the UUT class will instantiate exactly two ClassToPatch objects, I want the Mock framework to return a new Mock object for each instantiation, so I can later assert calls on each separately.
How do I achieve this using the #patch decorator in a test case? Namely, how to fix the following code sample?
class TestCase1(unittest.TestCase):
#patch('classToPatch.ClassToPatch',autospec=True)
def test_1(self,mock1,mock2):
_assert_stuff()
Here's a quick'n'dirty example to get you going:
import mock
import unittest
class ClassToPatch():
def __init__(self, *args):
pass
def some_func(self):
return id(self)
class UUT():
def __init__(self, *args):
resource_1 = ClassToPatch()
resource_2 = ClassToPatch()
self.test_property = (resource_1.some_func(), resource_2.some_func())
class TestCase1(unittest.TestCase):
#mock.patch('__main__.ClassToPatch', autospec = True)
def test_1(self, mock1):
ctpMocks = [mock.Mock(), mock.Mock()]
ctpMocks[0].some_func.return_value = "funky"
ctpMocks[1].some_func.return_value = "monkey"
mock1.side_effect = ctpMocks
u = UUT()
self.assertEqual(u.test_property, ("funky", "monkey"))
if __name__ == '__main__':
unittest.main()
I've added test_property to UUT so that the unit test does something useful. Now, without the mock test_property should be a tuple containing the ids of the two ClassToPatch instances. But with the mock it should be the tuple: ("funky", "monkey").
I've used the side_effect property of the mock object so that a different instance of ClassToPatch is returned on each call in the UUT initialiser.
Hope this helps.
Edit: Oh, by the way, when I run the unit test I get:
.
----------------------------------------------------------------------
Ran 1 test in 0.004s
OK
Here is another version which is more generic to handle any number of instances created:
class TestUUT:
def test_init(self, mocker):
class MockedClassToPatchMeta(type):
static_instance = mocker.MagicMock(spec=ClassToPatch)
def __getattr__(cls, key):
return MockedClassToPatchMeta.static_instance.__getattr__(key)
class MockedClassToPatch(metaclass=MockedClassToPatchMeta):
original_cls = ClassToPatch
instances = []
def __new__(cls, *args, **kwargs):
MockedClassToPatch.instances.append(
mocker.MagicMock(spec=MockedClassToPatch.original_cls))
MockedClassToPatch.instances[-1].__class__ = MockedClassToPatch
return MockedClassToPatch.instances[-1]
mocker.patch(__name__ + '.ClassToPatch', new=MockedClassToPatch)
UUT()
# since your original code created two instances
assert 2 == len(MockedClassToPatch.instances)
If you need more thorough validation for each instance you can access MockedClassToPatch.instances[0] or MockedClassToPatch.instances[1].
I've also created a helper library to generate the meta class boilerplate for me. To generate the needed code for your example I wrote:
print(PytestMocker(mocked=ClassToPatch, name=__name__).mock_classes().mock_classes_static().generate())

Categories