Created
August 30, 2013 20:09
-
-
Save davesque/6393809 to your computer and use it in GitHub Desktop.
setUp/tearDown patcher pattern for python unittest and python mock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.