From 95092b511dbacac412dbe5b8ff3a28abb3d3f664 Mon Sep 17 00:00:00 2001 From: Cédric Ollivier Date: Fri, 10 Aug 2018 12:55:10 +0200 Subject: Leverage on abc and stevedore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I7b3c4c0c5dd0c9e6fb3e52c3fff5221d4b831b02 Signed-off-by: Cédric Ollivier --- xtesting/tests/unit/ci/test_run_tests.py | 21 ++++++++++------- xtesting/tests/unit/core/test_feature.py | 15 +++++++++++- xtesting/tests/unit/core/test_testcase.py | 35 ++++++++++++++++++---------- xtesting/tests/unit/utils/test_decorators.py | 13 ++++++++++- 4 files changed, 61 insertions(+), 23 deletions(-) (limited to 'xtesting/tests/unit') diff --git a/xtesting/tests/unit/ci/test_run_tests.py b/xtesting/tests/unit/ci/test_run_tests.py index 392612bd..423bf486 100644 --- a/xtesting/tests/unit/ci/test_run_tests.py +++ b/xtesting/tests/unit/ci/test_run_tests.py @@ -20,6 +20,10 @@ from xtesting.core.testcase import TestCase class FakeModule(TestCase): def run(self, **kwargs): + self.start_time = 0 + self.stop_time = 1 + self.criteria = 100 + self.result = 100 return TestCase.EX_OK @@ -140,9 +144,8 @@ class RunTestsTesting(unittest.TestCase): msg = "Cannot import the class for the test case." self.assertTrue(msg in str(context.exception)) - @mock.patch('importlib.import_module', name="module", - return_value=mock.Mock(test_class=mock.Mock( - side_effect=FakeModule))) + @mock.patch('stevedore.driver.DriverManager', + return_value=mock.Mock(driver=FakeModule())) @mock.patch('xtesting.ci.run_tests.Runner.get_dict_by_test') def test_run_tests_default(self, *args): mock_test = mock.Mock() @@ -151,14 +154,15 @@ class RunTestsTesting(unittest.TestCase): 'is_enabled.return_value': True, 'needs_clean.return_value': True} mock_test.configure_mock(**kwargs) - test_run_dict = {'module': 'test_module', - 'class': 'test_class'} + test_run_dict = {'name': 'test_module'} with mock.patch('xtesting.ci.run_tests.Runner.get_run_dict', return_value=test_run_dict): self.runner.clean_flag = True - self.runner.run_test(mock_test) + self.assertEqual(self.runner.run_test(mock_test), TestCase.EX_OK) args[0].assert_called_with('test_name') - args[1].assert_called_with('test_module') + args[1].assert_called_with( + invoke_kwds=mock.ANY, invoke_on_load=True, name='test_module', + namespace='xtesting.testcase') self.assertEqual(self.runner.overall_result, run_tests.Result.EX_OK) @@ -170,8 +174,7 @@ class RunTestsTesting(unittest.TestCase): 'is_enabled.return_value': False, 'needs_clean.return_value': True} mock_test.configure_mock(**kwargs) - test_run_dict = {'module': 'test_module', - 'class': 'test_class'} + test_run_dict = {'name': 'test_name'} with mock.patch('xtesting.ci.run_tests.Runner.get_run_dict', return_value=test_run_dict): self.runner.clean_flag = True diff --git a/xtesting/tests/unit/core/test_feature.py b/xtesting/tests/unit/core/test_feature.py index 72bc488f..a4ac5af7 100644 --- a/xtesting/tests/unit/core/test_feature.py +++ b/xtesting/tests/unit/core/test_feature.py @@ -18,6 +18,19 @@ from xtesting.core import feature from xtesting.core import testcase +class FakeTestCase(feature.Feature): + + def execute(self, **kwargs): + pass + + +class AbstractFeatureTesting(unittest.TestCase): + + def test_run_unimplemented(self): + with self.assertRaises(TypeError): + feature.Feature(case_name="feature", project_name="xtesting") + + class FeatureTestingBase(unittest.TestCase): _case_name = "foo" @@ -46,7 +59,7 @@ class FeatureTesting(FeatureTestingBase): # what will break these unit tests. logging.disable(logging.CRITICAL) with mock.patch('six.moves.builtins.open'): - self.feature = feature.Feature( + self.feature = FakeTestCase( project_name=self._project_name, case_name=self._case_name) def test_run_exc(self): diff --git a/xtesting/tests/unit/core/test_testcase.py b/xtesting/tests/unit/core/test_testcase.py index 6b83b97c..51ea6f35 100644 --- a/xtesting/tests/unit/core/test_testcase.py +++ b/xtesting/tests/unit/core/test_testcase.py @@ -7,6 +7,8 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 +# pylint: disable=missing-docstring + """Define the class required to fully cover testcase.""" from datetime import datetime @@ -23,10 +25,22 @@ from xtesting.core import testcase __author__ = "Cedric Ollivier " -class TestCaseTesting(unittest.TestCase): - """The class testing TestCase.""" +class FakeTestCase(testcase.TestCase): + # pylint: disable=too-many-instance-attributes + + def run(self, **kwargs): + return testcase.TestCase.EX_OK + + +class AbstractTestCaseTesting(unittest.TestCase): + + def test_run_unimplemented(self): + with self.assertRaises(TypeError): + testcase.TestCase(case_name="base", project_name="xtesting") - # pylint: disable=missing-docstring,too-many-public-methods + +class TestCaseTesting(unittest.TestCase): + # pylint: disable=too-many-instance-attributes,too-many-public-methods _case_name = "base" _project_name = "xtesting" @@ -35,8 +49,8 @@ class TestCaseTesting(unittest.TestCase): _headers = {'Content-Type': 'application/json'} def setUp(self): - self.test = testcase.TestCase(case_name=self._case_name, - project_name=self._project_name) + self.test = FakeTestCase( + case_name=self._case_name, project_name=self._project_name) self.test.start_time = 1 self.test.stop_time = 2 self.test.result = 100 @@ -47,9 +61,8 @@ class TestCaseTesting(unittest.TestCase): os.environ['NODE_NAME'] = "node_name" os.environ['BUILD_TAG'] = "foo-daily-master-bar" - def test_run_unimplemented(self): - self.assertEqual(self.test.run(), - testcase.TestCase.EX_RUN_ERROR) + def test_run_fake(self): + self.assertEqual(self.test.run(), testcase.TestCase.EX_OK) def _test_pushdb_missing_attribute(self): self.assertEqual(self.test.push_to_db(), @@ -255,13 +268,11 @@ class TestCaseTesting(unittest.TestCase): def test_str_project_name_ko(self): self.test.project_name = None - self.assertIn("