forked from facebook/TestSlide
/
__init__.py
76 lines (60 loc) · 2.27 KB
/
__init__.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import os
if "COVERAGE_PROCESS_START" in os.environ:
import coverage
coverage.process_startup()
import os
import sys
import unittest
from typing import Any
from . import mock_callable as _mock_callable, mock_constructor as _mock_constructor, patch_attribute as _patch_attribute
from .strict_mock import StrictMock # noqa
if sys.version_info < (3, 7):
raise RuntimeError("Python >=3.7 required.")
def _importer(target: str) -> Any:
components = target.split(".")
import_path = components.pop(0)
thing = __import__(import_path)
def dot_lookup(thing: object, comp: str, import_path: str) -> Any:
try:
return getattr(thing, comp)
except AttributeError:
__import__(import_path)
return getattr(thing, comp)
for comp in components:
import_path += ".%s" % comp
thing = dot_lookup(thing, comp, import_path)
return thing
class TestCase(unittest.TestCase):
"""
A subclass of unittest.TestCase that adds TestSlide's features.
"""
def setUp(self) -> None:
_mock_callable.register_assertion = lambda assertion: self.addCleanup(
assertion
)
self.addCleanup(_mock_callable.unpatch_all_callable_mocks)
self.addCleanup(_mock_constructor.unpatch_all_constructor_mocks)
self.addCleanup(_patch_attribute.unpatch_all_mocked_attributes)
super(TestCase, self).setUp()
@staticmethod
def mock_callable(
*args: Any, **kwargs: Any
) -> _mock_callable._MockCallableDSL:
return _mock_callable.mock_callable(*args, **kwargs)
@staticmethod
def mock_async_callable(
*args: Any, **kwargs: Any
) -> _mock_callable._MockCallableDSL:
return _mock_callable.mock_async_callable(*args, **kwargs)
@staticmethod
def mock_constructor(
*args: Any, **kwargs: Any
) -> _mock_constructor._MockConstructorDSL:
return _mock_constructor.mock_constructor(*args, **kwargs)
@staticmethod
def patch_attribute(*args: Any, **kwargs: Any) -> None:
return _patch_attribute.patch_attribute(*args, **kwargs)