Magic mock assert_called_once vs assert_called_once_with weird behaviour - python

I am noticing a weird behavior with assert_called_once and assert_called_once_with in python. This is my real simple test:
File module/a.py
from .b import B
class A(object):
def __init__(self):
self.b = B("hi")
def call_b_hello(self):
print(self.b.hello())
File module/b.py
class B(object):
def __init__(self, string):
print("created B")
self.string = string;
def hello(self):
return self.string
These are my tests:
import unittest
from mock import patch
from module.a import A
class MCVETests(unittest.TestCase):
#patch('module.a.B')
def testAcallBwithMockPassCorrect(self, b1):
a = A()
b1.assert_called_once_with("hi")
a.call_b_hello()
a.b.hello.assert_called_once()
#patch('module.a.B')
def testAcallBwithMockPassCorrectWith(self, b1):
a = A()
b1.assert_called_once_with("hi")
a.call_b_hello()
a.b.hello.assert_called_once_with()
#patch('module.a.B')
def testAcallBwithMockFailCorrectWith(self, b1):
a = A()
b1.assert_called_once_with("hi")
a.b.hello.assert_called_once_with()
#patch('module.a.B')
def testAcallBwithMockPassWrong(self, b1):
a = A()
b1.assert_called_once_with("hi")
a.b.hello.assert_called_once()
if __name__ == '__main__':
unittest.main()
My problem as stated in the name of the function is:
Test 1 passes correctly
Test 2 passes correctly
Test 3 fails correctly (I've removed the call to b)
Test 4 passes I am not sure why.
Am I doing something wrong? I am not sure but reading the documentation docs python:
assert_called_once(*args, **kwargs)
Assert that the mock was called exactly once.

This is old, but for others landing here...
For python < 3.6, assert_called_once isn't a thing and so you're actually making a mocked function call which doesn't error
Please see: http://engineroom.trackmaven.com/blog/mocking-mistakes/
You can check the call count instead.

Related

How to mock init of helper class

I am having trouble with checking if an object is being constructed with the proper params from another instance of an object. In the below example, I am trying to create an instance of B within an instance of A. I want to check the parameter being used in the constructor of B inside of the A instance. When I run the test below, I get:
AssertionError: assert None
[CPython36:setup:stdout] E + where None = <bound method NonCallableMock.assert_called_with of <MagicMock name='B' id='139968329210736'>>(4)
[CPython36:setup:stdout] E + where <bound method NonCallableMock.assert_called_with of <MagicMock name='B' id='139968329210736'>> = <MagicMock name='B' id='139968329210736'>.assert_called_with
I am not quite sure what I am doing wrong here and have looked at other stack overflow posts, but have not been able to solve my issue.
b.py:
class B(object):
def __init__(self, x):
self.x = x
def square(self):
return x * x
a.py:
from b import B
class A(object):
def foo(self):
b = B(4)
b.square()
test_a.py:
import unittest
from unittest.mock import patch
from a import A
class TestA(unittest.TestCase):
#patch('a.B')
def test_foo(self, mock_b):
self.a = A()
self.a.foo()
assert mock_b.assert_called_with(4)
The method assert_called_with returns None, so what your are doing is like doing
assert None
And that's basically the error message you are getting.
You can just use
mock_b.assert_called_with(4)
Which has an assert internally and pytest will display it correctly in case of failure. Try to check it by changing the argument value.
Alternatively, if you prefer to write the assert yourself, you can do something like this:
from unittest.mock import call
assert mock_b.call_args_list == [call(4)]
Or just the last call:
from unittest.mock import call
assert mock_b.call_args == call(4)

Patch a method outside python class

I am interested in patching a method which is called by another method in one file. Example - original.py file contains -
def A():
a = 10
b = 5
return a*b;
def B():
c = A()
return c* 10
I want to write unit test for this file , say call it test.py
import mock
import unittest
class TestOriginal(unitest.TestCase):
def test_Original_method(self):
with patch(''):
How can I use patch and mock modules to test original.py. I want A() to always return MagicMock() object instead of an integer.
You simply patch out the A global in the module under test. I'd use the #patch decorator syntax here:
import mock
import unittest
import module_under_test
class TestOriginal(unitest.TestCase):
#patch('module_under_test.A')
def test_Original_method(self, mocked_A):
mocked_A.return_value = 42
result = module_under_test.B()
mocked_A.assert_called_with()
self.assertEqual(result, 420)
This passes in the MagicMock mock object for A() as an extra argument to the test method.
Note that we explicitly named the module here. You could also use patch.object(), just naming the attribute on the module (which are your module globals):
class TestOriginal(unitest.TestCase):
#patch.object(module_under_test, 'A')
def test_Original_method(self, mocked_A):
mocked_A.return_value = 42
result = module_under_test.B()
mocked_A.assert_called_with()
self.assertEqual(result, 420)
You can still use a with statement too, of course:
class TestOriginal(unitest.TestCase):
def test_Original_method(self):
with patch('module_under_test.A') as mocked_A:
mocked_A.return_value = 42
result = module_under_test.B()
mocked_A.assert_called_with()
self.assertEqual(result, 420)

making a function as an else inside an __init__

How to get a function inside if/else inside an __init__ :
class Foo(object):
def __init__(self, q, **keywords):
if a == "":
print "No empty strings"
else:
def on_g(self, response):
if response.error:
print "Check your internet settings"
else:
self.Bar()
http_client.fetch("http://www.google.com/", self.on_g)
because the program dont read the on_g() if i put an empty string!
If i use the on_g() outside in parallel with __init__() i need a declared variable, for example:
class Foo(object):
def __init__(self, q, **keywords):
if a == "":
print "No empty strings"
else:
self.on_g()
def on_g(self):
print 'hello there'
will return hello there
Your bug is in
http_client.fetch("http://www.google.com/", self.on_g)
which should be
http_client.fetch("http://www.google.com/", on_g)
since you defined a function, not a method.
self (the instance you're creating through __init__ ) doesn't have a on_g method.
Functions for the class-es need to be defined at the class level (as shown on your second chunk of code). They are evaluated when the class is first... erm... "looked-up"? "evaluated"?
That's why your second piece of code works. How come you can call self.on_g within the __init__ when the actual definition of the on_g method seems to come later in the code? It's an odd behavior (at a first glance) for an interpreter, right? Well... That's because when you run self.on_g(), the whole Foo class has already been evaluated and on_g has been added to the class (not to the instance!: To the class)
class Foo(object):
def __init__(self, q, **keywords):
[ . . . ]
else:
self.on_g() # I can use self.on_g() eventhough is defined... _
# |
# |
def on_g(self): # <------------ LATER ---------------------------|
print 'hello there'
Whereas if you define your method within the __init__, the interpreter will yell at you:
class Test(object):
def __init__(self):
def test(self):
print "Hello"
self.test()
a = Test()
Throws:
Traceback (most recent call last):
File "./test.py", line 10, in <module>
a = Test()
File "./test.py", line 8, in __init__
self.test()
AttributeError: 'Test' object has no attribute 'test'
Even if you think Oh, maybe the class doesn't have the test method because it's still within the __init__, and it will have it once the initialization is completed... Meeeck... Wrong:
class Test(object):
def __init__(self):
def test(self):
print "Hello"
a = Test()
a.test()
Same AttributeError.
If you still want to add on_g to the class at runtime (very bad idea, IMHO) you can do the interpreter's job by doing this:
class Test(object):
def __init__(self):
def test(self):
print "Hello"
self.__class__.test = test
self.test()
a = Test()
a.test()
... which correctly prints:
Hello
Hello
Now, the two most straightforward things to do I can think of are:
You move the def on_g(self) to the class level (as you showed in your second code snippet)
You call your http_client.fetch with on_g as a function local to the __init__'s scope (being picky with the language: on_g now is a function, not a method, since is not bound to an object anymore).
def __init__(self, q, **keywords):
if a == "":
print "No empty strings"
else:
def on_g(response):
if response.error:
print "Check your internet settings"
else:
self.Bar()
http_client.fetch("http://www.google.com/", on_g)

What is the right way to test callback invocation using Python unittest?

I have an application code that looks like the following.
# Filename: app.py
class Foo:
def __init__(self):
self.callback = None
def set_handler(self, callback):
self.callback = callback
def run(self, details):
name, age = details.split('/')
if age.isdigit():
age = int(age)
else:
age = -1
return self.callback(name, age)
As you can see, it offers a set_handler method to set a callback. The callback must later be invoked with two arguments: a string and an integer. I am trying to ensure this in a unittest.
# Filename: test_app.py
import unittest
from app import Foo
class AppTest(unittest.TestCase):
def f(self, a, b):
# This callback should get called with the first argument as
# string and the second argument as integer
return repr(a) + ',' + repr(b)
def test_callback(self):
foo = Foo()
foo.set_handler(self.f)
self.assertEqual(foo.run('John/20'), "'John',20")
self.assertEqual(foo.run('John/xyz'), "'John',-1")
if __name__ == '__main__':
unittest.main()
This unit test succeeds. But I don't think my way of testing is robust. This unit test is basically a hack because I don't know how to correctly test if a callback has been invoked with the right type of arguments. What I find weird about it is that AppTest's f method is sharing the responsibility of type checking by attempting to return a value which are composed of repr() of the arguments, and this is not at all robust to say the least.
Could you please help me? Is it possible to relieve the f method of the responsibility of testing the types?
EDIT:
Try using unittest.mock (standard library on Python 3.3). It allows you to assert how methods were called. For example:
import unittest
from unittest.mock import Mock
from app import Foo
class AppTest(unittest.TestCase):
def test_callback(self):
foo = Foo()
f = Mock()
foo.set_handler(f)
foo.run('John/20')
f.assert_called_with('John', 20)
foo.run('John/xyz')
f.assert_called_with('John', -1)
if __name__ == '__main__':
unittest.main()

Why this mocking script doesn't work (unittest, Mocker, 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.

Categories