Fully cover pytest_suite_runner.py 49/35049/2
authorCédric Ollivier <cedric.ollivier@orange.com>
Fri, 19 May 2017 09:29:26 +0000 (11:29 +0200)
committerCédric Ollivier <cedric.ollivier@orange.com>
Fri, 19 May 2017 09:44:28 +0000 (11:44 +0200)
Change-Id: I766afb1d5c6ef53a5b551539662ee363ae8eadee
Signed-off-by: Cédric Ollivier <cedric.ollivier@orange.com>
functest/tests/unit/core/test_pytest_suite_runner.py

index 4160df0..f317cde 100644 (file)
@@ -21,25 +21,32 @@ class PyTestSuiteRunnerTesting(unittest.TestCase):
     def setUp(self):
         self.psrunner = pytest_suite_runner.PyTestSuiteRunner()
 
-    def _test_run(self, result, status=testcase.TestCase.EX_OK):
+    @mock.patch('unittest.TestLoader')
+    def _test_run(self, mock_class=None, result=mock.Mock(),
+                  status=testcase.TestCase.EX_OK):
         with mock.patch('functest.core.pytest_suite_runner.'
                         'unittest.TextTestRunner.run',
                         return_value=result):
             self.assertEqual(self.psrunner.run(), status)
+            mock_class.assert_not_called()
+
+    def test_check_suite_null(self):
+        self.assertEqual(self.psrunner.suite, None)
 
     def test_run_no_ut(self):
         mock_result = mock.Mock(testsRun=0, errors=[], failures=[])
-        self._test_run(mock_result, testcase.TestCase.EX_RUN_ERROR)
+        self._test_run(result=mock_result,
+                       status=testcase.TestCase.EX_RUN_ERROR)
         self.assertEqual(self.psrunner.result, 0)
         self.assertEqual(self.psrunner.details, {'errors': [], 'failures': []})
         self.assertEqual(self.psrunner.is_successful(),
                          testcase.TestCase.EX_TESTCASE_FAILED)
 
-    def test_run_ko(self):
+    def test_run_result_ko(self):
         self.psrunner.criteria = 100
         mock_result = mock.Mock(testsRun=50, errors=[('test1', 'error_msg1')],
                                 failures=[('test2', 'failure_msg1')])
-        self._test_run(mock_result, testcase.TestCase.EX_OK)
+        self._test_run(result=mock_result)
         self.assertEqual(self.psrunner.result, 96)
         self.assertEqual(self.psrunner.details,
                          {'errors': [('test1', 'error_msg1')],
@@ -47,15 +54,38 @@ class PyTestSuiteRunnerTesting(unittest.TestCase):
         self.assertEqual(self.psrunner.is_successful(),
                          testcase.TestCase.EX_TESTCASE_FAILED)
 
-    def test_run_ok(self):
+    def test_run_result_ok(self):
         mock_result = mock.Mock(testsRun=50, errors=[],
                                 failures=[])
-        self._test_run(mock_result)
+        self._test_run(result=mock_result)
         self.assertEqual(self.psrunner.result, 100)
         self.assertEqual(self.psrunner.details, {'errors': [], 'failures': []})
         self.assertEqual(self.psrunner.is_successful(),
                          testcase.TestCase.EX_OK)
 
+    @mock.patch('unittest.TestLoader')
+    def test_run_name_exc(self, mock_class=None):
+        mock_obj = mock.Mock(side_effect=ImportError)
+        mock_class.side_effect = mock_obj
+        self.assertEqual(self.psrunner.run(name='foo'),
+                         testcase.TestCase.EX_RUN_ERROR)
+        mock_class.assert_called_once_with()
+        mock_obj.assert_called_once_with()
+
+    @mock.patch('unittest.TestLoader')
+    def test_run_name(self, mock_class=None):
+        mock_result = mock.Mock(testsRun=50, errors=[],
+                                failures=[])
+        mock_obj = mock.Mock()
+        mock_class.side_effect = mock_obj
+        with mock.patch('functest.core.pytest_suite_runner.'
+                        'unittest.TextTestRunner.run',
+                        return_value=mock_result):
+            self.assertEqual(self.psrunner.run(name='foo'),
+                             testcase.TestCase.EX_OK)
+        mock_class.assert_called_once_with()
+        mock_obj.assert_called_once_with()
+
 
 if __name__ == "__main__":
     logging.disable(logging.CRITICAL)