-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtestcase.py
More file actions
35 lines (29 loc) · 1.38 KB
/
testcase.py
File metadata and controls
35 lines (29 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from unittest import TestCase, mock
class HypernodeTestCase(TestCase):
def __init__(self, *args, **kwargs):
super(HypernodeTestCase, self).__init__(*args, **kwargs)
def set_up_patch(self, topatch, themock=None, **kwargs):
"""
Patch a function or class without having to use
class or method decorators. This is helpful when
you want to compose a mock in the setup without
without having to do the boilerplate of patching,
adding cleanup and starting the patch. See
https://docs.python.org/3/library/unittest.mock-examples.html#applying-the-same-patch-to-every-test-method
This helper is used in most Hypernode projects.
:param topatch: string The class to patch
:param themock: optional object to use as mock
:return: mocked object
"""
if themock is None:
themock = mock.Mock()
if "return_value" in kwargs:
themock.return_value = kwargs["return_value"]
patcher = mock.patch(topatch, themock)
self.addCleanup(patcher.stop)
return patcher.start()
def set_up_context_manager_patch(self, topatch, themock=None, **kwargs):
patcher = self.set_up_patch(topatch, themock=themock, **kwargs)
patcher.return_value.__exit__ = lambda a, b, c, d: None
patcher.return_value.__enter__ = lambda x: None
return patcher