diff options
author | Cédric Ollivier <cedric.ollivier@orange.com> | 2018-08-10 12:55:10 +0200 |
---|---|---|
committer | Cédric Ollivier <cedric.ollivier@orange.com> | 2018-08-12 18:32:59 +0200 |
commit | 95092b511dbacac412dbe5b8ff3a28abb3d3f664 (patch) | |
tree | 24a547cf1a687af0d4db184375abcc03aef998ba /xtesting/tests | |
parent | a9491ef0948f8fe3eb3772b6b6ae44d86bde7e9b (diff) |
Leverage on abc and stevedore
Change-Id: I7b3c4c0c5dd0c9e6fb3e52c3fff5221d4b831b02
Signed-off-by: Cédric Ollivier <cedric.ollivier@orange.com>
Diffstat (limited to 'xtesting/tests')
-rw-r--r-- | xtesting/tests/unit/ci/test_run_tests.py | 21 | ||||
-rw-r--r-- | xtesting/tests/unit/core/test_feature.py | 15 | ||||
-rw-r--r-- | xtesting/tests/unit/core/test_testcase.py | 35 | ||||
-rw-r--r-- | xtesting/tests/unit/utils/test_decorators.py | 13 |
4 files changed, 61 insertions, 23 deletions
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 <cedric.ollivier@orange.com>" -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("<xtesting.core.testcase.TestCase object at", - str(self.test)) + self.assertIn("FakeTestCase object at", str(self.test)) def test_str_case_name_ko(self): self.test.case_name = None - self.assertIn("<xtesting.core.testcase.TestCase object at", - str(self.test)) + self.assertIn("FakeTestCase object at", str(self.test)) def test_str_pass(self): duration = '01:01' diff --git a/xtesting/tests/unit/utils/test_decorators.py b/xtesting/tests/unit/utils/test_decorators.py index 83b182a8..c08a7ea3 100644 --- a/xtesting/tests/unit/utils/test_decorators.py +++ b/xtesting/tests/unit/utils/test_decorators.py @@ -28,6 +28,13 @@ FILE = '{}/null'.format(DIR) URL = 'file://{}'.format(FILE) +class FakeTestCase(testcase.TestCase): + # pylint: disable=missing-docstring + + def run(self, **kwargs): + return testcase.TestCase.EX_OK + + class DecoratorsTesting(unittest.TestCase): # pylint: disable=missing-docstring @@ -66,7 +73,7 @@ class DecoratorsTesting(unittest.TestCase): return json.dumps(data, sort_keys=True) def _get_testcase(self): - test = testcase.TestCase( + test = FakeTestCase( project_name=self._project_name, case_name=self._case_name) test.start_time = self._start_time test.stop_time = self._stop_time @@ -74,6 +81,10 @@ class DecoratorsTesting(unittest.TestCase): test.details = {} return test + def test_run_fake(self): + test = self._get_testcase() + self.assertEqual(test.run(), testcase.TestCase.EX_OK) + @mock.patch('requests.post') def test_http_shema(self, *args): os.environ['TEST_DB_URL'] = 'http://127.0.0.1' |