Skip to content

Instantly share code, notes, and snippets.

@davesque
Created August 30, 2013 20:09
Show Gist options
  • Save davesque/6393809 to your computer and use it in GitHub Desktop.
Save davesque/6393809 to your computer and use it in GitHub Desktop.
setUp/tearDown patcher pattern for python unittest and python mock
import unittest
from mock import call, patch
class PatcherTestCase(unittest.TestCase):
def setUp(self):
self.patcher = patch('some_module.some_object')
self.mock_object = self.patcher.start()
def tearDown(self):
self.patcher.stop()
def assertMockCallsEqual(self, *args):
self.assertEqual(self.mock_object.call_args_list, list(args))
def test_something(self):
# Do something that impacts mock object
pass
# Make assertions about mock object state
self.assertMockCallsEqual(
call('arg_1_value', 'arg_2_value', 'arg_3_value'), # call 1
call('arg_1_value', 'arg_2_value', 'arg_3_value'), # call 2
)
@davesque
Copy link
Author

I know you guys aren't using the standard lib unittest module, but you might be able to use some aspects of this pattern that I had used in one of my projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment