Consider the following three files.
# my_class.py
class MyClass:
def __init__(self):
pass
def do_thing(self):
return 5
# main.py
from my_class import MyClass
def my_func():
instance = MyClass()
instance.do_thing()
# test_main.py
from main import my_func
from unittest.mock import patch
#patch('main.MyClass')
def test_my_func(MockMyClass):
my_func()
MockMyClass.do_thing.assert_called_once()
AssertionError: Expected 'do_thing' to have been called once. Called 0 times.
I'm instantiating a class MyClass inside a driver function my_func and calling one of the class's methods do_thing. What I'd like to do is test that when the driver function is invoked, the method of the class is called exactly once. I'm encountering an assertion error that's giving me problems.
I've read a million and one SO posts and other resources online about Python mocks, but I'm not able to figure this out. I thought the trick was that the #patch decorator patches the namespace the module is imported into, not from [Python Mocking a function from an imported module. What am I doing wrong here?
The do_thing method is an instance method of MyClass, NOT class method. You assert MockMyClass.do_thing.assert_called_once() is not correct. Here is the unit test solution:
my_class.py:
class MyClass:
def __init__(self):
pass
def do_thing(self):
return 5
main.py:
from my_class import MyClass
def my_func():
instance = MyClass()
instance.do_thing()
test_main.py:
from main import my_func
import unittest
from unittest.mock import patch
class TestMain(unittest.TestCase):
#patch('main.MyClass')
def test_my_func(self, MockMyClass):
mock_my_class_instance = MockMyClass.return_value
my_func()
mock_my_class_instance.do_thing.assert_called_once()
if __name__ == '__main__':
unittest.main()
unit test results with coverage report:
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Name Stmts Miss Cover Missing
-----------------------------------------------------------------------
src/stackoverflow/60539392/main.py 4 0 100%
src/stackoverflow/60539392/my_class.py 5 2 60% 3, 6
src/stackoverflow/60539392/test_main.py 10 0 100%
-----------------------------------------------------------------------
TOTAL
Related
I have a class similarly structured to this:
class TestClass:
def a(self):
pass
def b(self):
self.a()
I want to test if running TestClass().b() successfully calls TestClass().a():
class TestTestClass(unittest.TestCase):
def test_b(self):
with patch('test_file.TestClass') as mock:
instance = mock.return_value
TestClass().b()
instance.a.assert_called()
However, it fails because a isn't called. What am I doing wrong? I went through the mock documentation and various other guides to no avail.
Thanks!
You can use patch.object() to patch the a() method of TestClass.
E.g.
test_file.py:
class TestClass:
def a(self):
pass
def b(self):
self.a()
test_test_file.py:
import unittest
from unittest.mock import patch
from test_file import TestClass
class TestTestClass(unittest.TestCase):
def test_b(self):
with patch.object(TestClass, 'a') as mock_a:
instance = TestClass()
instance.b()
instance.a.assert_called()
mock_a.assert_called()
if __name__ == '__main__':
unittest.main()
unit test result:
⚡ coverage run /Users/dulin/workspace/github.com/mrdulin/python-codelab/src/stackoverflow/66707071/test_test_file.py && coverage report -m --include='./src/**'
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Name Stmts Miss Cover Missing
----------------------------------------------------------------------------
src/stackoverflow/66707071/test_file.py 5 1 80% 3
src/stackoverflow/66707071/test_test_file.py 12 0 100%
----------------------------------------------------------------------------
TOTAL 17 1 94%
So I've this code that mocks two times, the first time by mocking imports with:
sys.modules['random'] = MagicMock()
The second time happens inside the unittest of a function that used that import, for example a function that used random
The tests. py is:
import sys
import unittest
from unittest import mock
from unittest.mock import MagicMock
import foo
sys.modules['random'] = MagicMock()
class test_foo(unittest.TestCase):
def test_method(self):
with mock.patch('random.choice', return_value = 2):
object = foo.FooClass(3)
self.assertEqual(2, object.method(), 'Should be 2')
def test_staticmethod(self):
with mock.patch('random.choice', return_value = 2):
object = foo.FooClass(3)
self.assertEqual(2, object.method(), 'should be 2')
The original file Foo.py is:
import random
class FooClass:
def __init__(self,arg):
self.arg = arg
def method(self):
print(random.choice)
return random.choice([1,2,3])
#staticmethod
def staticmethod():
print(random.choice)
random.choice([1,2,3])
The two mocks contrarrest each other, and the mocking of random doesn't happen.
When it prints random it actually prints:
<<bound method Random.choice of <random.Random object at 0x7fe688028018>>
I want that to print a MagicMock.
Can someone help me understand what's happening? Why are they contrarresting each other?
You don't need to update the module source with sys.modules['random'] = MagicMock() without this line it works fine <MagicMock name='choice' id='...'>. patch already does all the work for the isolated temporary updating the method. See more explanation in the docs - Where to patch
I'm encountering a problem with unit testing in Python. Specifically, when I try to mock a function my code imports, variables assigned to the output of that function get assigned to a MagicMock object instead of the mock-function's return_value. I've been digging through the docs for python's unittest library, but am not having any luck.
The following is the code I want to test:
from production_class import function_A, function_B, function_M
class MyClass:
def do_something(self):
variable = functionB()
if variable:
do_other_stuff()
else:
do_something_else
this is what I've tried:
#mock.patch(path.to.MyClass.functionB)
#mock.patch(<other dependencies in MyClass>)
def test_do_something(self, functionB_mock):
functionB_mock.return_value = None # or False, or 'foo' or whatever.
myClass = MyClass()
myClass.do_something()
self.assertTrue(else_block_was_executed)
The issue I have is that when the test gets to variable = functionB in MyClass, the variable doesn't get set to my return value; it gets set to a MagicMock object (and so the if-statement always evaluates to True). How do I mock an imported function such that when executed, variables actually get set to the return value and not the MagicMock object itself?
We'd have to see what import path you're actually using with path.to.MyClass.functionB. When mocking objects, you don't necessarily use the path directly to where the object is located, but the one that the intepreter sees when recursively importing modules.
For example, if your test imports MyClass from myclass.py, and that file imports functionB from production_class.py, the mock path would be myclass.functionB, instead of production_class.functionB.
Then there's the issue that you need additional mocks of MyClass.do_other_stuff and MyClass.do_something_else in to check whether MyClass called the correct downstream method, based on the return value of functionB.
Here's a working example that tests both possible return values of functionB, and whether they call the correct downstream method:
myclass.py
from production_class import functionA, functionB, functionM
class MyClass:
def do_something(self):
variable = functionB()
if variable:
self.do_other_stuff()
else:
self.do_something_else()
def do_other_stuff(self):
pass
def do_something_else(self):
pass
production_class.py
import random
def functionA():
pass
def functionB():
return random.choice([True, False])
def functionM():
pass
test_myclass.py
import unittest
from unittest.mock import patch
from myclass import MyClass
class MyTest(unittest.TestCase):
#patch('myclass.functionB')
#patch('myclass.MyClass.do_something_else')
def test_do_something_calls_do_something_else(self, do_something_else_mock, functionB_mock):
functionB_mock.return_value = False
instance = MyClass()
instance.do_something()
do_something_else_mock.assert_called()
#patch('myclass.functionB')
#patch('myclass.MyClass.do_other_stuff')
def test_do_something_calls_do_other_stuff(self, do_other_stuff_mock, functionB_mock):
functionB_mock.return_value = True
instance = MyClass()
instance.do_something()
do_other_stuff_mock.assert_called()
if __name__ == '__main__':
unittest.main()
calling python test_myclass.py results in:
..
----------------------------------------------------------------------
Ran 2 tests in 0.002s
OK
What I wound up doing was changing the import statements in MyClass to import the object instead of the individual methods. I was then able to mock the object without any trouble.
More explicitly I changed MyClass to look like this:
import production_class as production_class
class MyClass:
def do_something(self):
variable = production_class.functionB()
if variable:
do_other_stuff()
else:
do_something_else
and changed my test to
#mock.patch(path.to.MyClass.production_class)
def test_do_something(self, prod_class_mock):
prod_class_mock.functionB.return_value = None
myClass = MyClass()
myClass.do_something()
self.assertTrue(else_block_was_executed)
Using Python 2.7, and mock library
How can I test that certain patched object has been initialized with some specific arguments using mock?
Here some sample code and pseudo-code:
unittest.py :
import mock
#mock.patch('mylib.SomeObject')
def test_mytest(self, mock_someobject):
test1 = mock_someobject.return_value
test1 = method_inside_someobject.side_effect = ['something']
mylib.method_to_test()
# How can I assert that method_to_test instanced SomeObject with certain arguments?
# I further test things with that method_inside_someobject call, no problems there...
mylib.py :
from someobjectmodule import SomeObject
def method_to_test():
obj = SomeObject(arg1=val1, arg2=val2, arg3=val3)
obj.method_inside_someobject()
So, how can I test SomeObject was instanced with arg1=val1, arg2=val2, arg3=val3?
If you replaced a class with a mock, creating an instance is just another call. Assert that the right parameters have been passed to that call, for example, with mock.assert_called_with():
mock_someobject.assert_called_with(arg1=val1, arg2=val2, arg3=val3)
To illustrate, I've updated your MCVE to a working example:
test.py:
import mock
import unittest
import mylib
class TestMyLib(unittest.TestCase):
#mock.patch('mylib.SomeObject')
def test_mytest(self, mock_someobject):
mock_instance = mock_someobject.return_value
mock_instance.method_inside_someobject.side_effect = ['something']
retval = mylib.method_to_test()
mock_someobject.assert_called_with(arg1='foo', arg2='bar', arg3='baz')
self.assertEqual(retval, 'something')
if __name__ == '__main__':
unittest.main()
mylib.py:
from someobjectmodule import SomeObject
def method_to_test():
obj = SomeObject(arg1='foo', arg2='bar', arg3='baz')
return obj.method_inside_someobject()
someobjectmodule.py:
class SomeObject(object):
def method_inside_someobject(self):
return 'The real thing'
and running the test:
$ python test.py
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
I am attempting to test multiple functions with the same name and signature. (Student assignment submissions.) My setup currently looks like this:
TestCases/
__init__
testcase1.py
submission1/
func.py
submission2/
func.py
submission3/
func.py
Each func.py contains a function named foo. I would like to test each occurrence of foo with the same test suite. The standard unittest setup of
import unittest
from func import foo
class TestFoo(unittest.TestCase):
def test_empty(self):
self.assertTrue(foo(''))
...
if __name__ == '__main__':
unittest.main()
has (unsurprisingly) problems with the from func import foo line since func is not in the same folder, and I cannot seem to get it to pick up the proper import dynamically.
It struck me that I could take advantage of having already extended the base class and eliminate the need for the import by doing something like:
import unittest
class TestFoo(unittest.TestCase):
def __init__(self, foo):
self.foo = foo
def test_empty(self):
self.assertTrue(self.foo(''))
and in my test program instantiate a TestFoo object and pass it the function to test:
tf = TestFoo(foo)
but then I haven't been able to properly define and call the test suite.
Or is there an entirely different approach that would do what I need?