Created
September 2, 2019 12:18
-
-
Save romuald/01e2d60fcb736acf8a48c24c963cba2b to your computer and use it in GitHub Desktop.
A simple pass-though mock template
This file contains 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.mock | |
def mock_passthrough(*args, **kwargs): | |
""" | |
A simple mock template to check that a method was called, | |
not modifying to original patched method. | |
Behave the same as mock.patch.object(), default autospec is True, | |
and side_effect is always overrided | |
Usage: | |
>>> patch = mock_passthrough(object, 'attribute') | |
... with patch as mocked: | |
... do_stuff() | |
... mocked.assert_called() | |
""" | |
if 'autospec' not in kwargs: | |
kwargs['autospec'] = True | |
def side_effect(*sargs, **kwsargs): | |
return patch.temp_original(*sargs, **kwsargs) | |
kwargs['side_effect'] = side_effect | |
patch = unittest.mock.patch.object(*args, **kwargs) | |
return patch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment