[PR #9675/4a31c753 backport][stable-10] test helper: remove magically finding the testcasemock in the test module (#9677)
test helper: remove magically finding the testcasemock in the test module (#9675)
(cherry picked from commit 4a31c753e7
)
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
pull/9682/head
parent
f545c300d9
commit
6e48528b22
|
@ -152,26 +152,19 @@ class ModuleTestCase:
|
||||||
mocks=test_case_spec.get("mocks", {}),
|
mocks=test_case_spec.get("mocks", {}),
|
||||||
flags=test_case_spec.get("flags", {})
|
flags=test_case_spec.get("flags", {})
|
||||||
)
|
)
|
||||||
tc.build_mocks(test_module, mocks_map)
|
tc.build_mocks(mocks_map)
|
||||||
return tc
|
return tc
|
||||||
|
|
||||||
def build_mocks(self, test_module, mocks_map):
|
def build_mocks(self, mocks_map):
|
||||||
for mock_name, mock_spec in self.mock_specs.items():
|
for mock_name, mock_spec in self.mock_specs.items():
|
||||||
mock_class = mocks_map.get(mock_name, self.get_mock_class(test_module, mock_name))
|
try:
|
||||||
|
mock_class = mocks_map[mock_name]
|
||||||
|
except KeyError:
|
||||||
|
raise Exception("Cannot find TestCaseMock class for: {0}".format(mock_name))
|
||||||
self.mocks[mock_name] = mock_class.build_mock(mock_spec)
|
self.mocks[mock_name] = mock_class.build_mock(mock_spec)
|
||||||
|
|
||||||
self._fixtures.update(self.mocks[mock_name].fixtures())
|
self._fixtures.update(self.mocks[mock_name].fixtures())
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_mock_class(test_module, mock):
|
|
||||||
try:
|
|
||||||
class_name = "".join(x.capitalize() for x in mock.split("_")) + "Mock"
|
|
||||||
plugin_class = getattr(test_module, class_name)
|
|
||||||
assert issubclass(plugin_class, TestCaseMock), "Class {0} is not a subclass of TestCaseMock".format(class_name)
|
|
||||||
return plugin_class
|
|
||||||
except AttributeError:
|
|
||||||
raise ValueError("Cannot find class {0} for mock {1}".format(class_name, mock))
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def fixtures(self):
|
def fixtures(self):
|
||||||
return dict(self._fixtures)
|
return dict(self._fixtures)
|
||||||
|
@ -207,10 +200,6 @@ class ModuleTestCase:
|
||||||
|
|
||||||
|
|
||||||
class TestCaseMock:
|
class TestCaseMock:
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def build_mock(cls, mock_specs):
|
def build_mock(cls, mock_specs):
|
||||||
return cls(mock_specs)
|
return cls(mock_specs)
|
||||||
|
@ -229,9 +218,7 @@ class TestCaseMock:
|
||||||
|
|
||||||
|
|
||||||
class RunCommandMock(TestCaseMock):
|
class RunCommandMock(TestCaseMock):
|
||||||
@property
|
name = "run_command"
|
||||||
def name(self):
|
|
||||||
return "run_command"
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "<RunCommandMock specs={specs}>".format(specs=self.mock_specs)
|
return "<RunCommandMock specs={specs}>".format(specs=self.mock_specs)
|
||||||
|
|
Loading…
Reference in New Issue