From 6e48528b2272130d17389f130189744911389beb Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 3 Feb 2025 07:03:39 +0100 Subject: [PATCH] [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 4a31c753e72b665882a7b4e803d729f946446201) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- tests/unit/plugins/modules/helper.py | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/tests/unit/plugins/modules/helper.py b/tests/unit/plugins/modules/helper.py index 5ebb5747d3..026da9644d 100644 --- a/tests/unit/plugins/modules/helper.py +++ b/tests/unit/plugins/modules/helper.py @@ -152,26 +152,19 @@ class ModuleTestCase: mocks=test_case_spec.get("mocks", {}), flags=test_case_spec.get("flags", {}) ) - tc.build_mocks(test_module, mocks_map) + tc.build_mocks(mocks_map) 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(): - 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._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 def fixtures(self): return dict(self._fixtures) @@ -207,10 +200,6 @@ class ModuleTestCase: class TestCaseMock: - @property - def name(self): - raise NotImplementedError() - @classmethod def build_mock(cls, mock_specs): return cls(mock_specs) @@ -229,9 +218,7 @@ class TestCaseMock: class RunCommandMock(TestCaseMock): - @property - def name(self): - return "run_command" + name = "run_command" def __str__(self): return "".format(specs=self.mock_specs)