I\'m trying to set up some automatic unit testing for a project. I have some functions which, as a side effect occasionally call another function. I want to write a unit test wh
You can mock the function b
using mock module and check if it was called. Here's an example:
import unittest
from mock import patch
def a(n):
if n > 10:
b()
def b():
print "test"
class MyTestCase(unittest.TestCase):
@patch('__main__.b')
def test_b_called(self, mock):
a(11)
self.assertTrue(mock.called)
@patch('__main__.b')
def test_b_not_called(self, mock):
a(10)
self.assertFalse(mock.called)
if __name__ == "__main__":
unittest.main()
Also see:
Hope that helps.