diff options
-rw-r--r-- | functest/core/unit.py | 27 | ||||
-rw-r--r-- | functest/tests/unit/core/test_unit.py | 6 |
2 files changed, 21 insertions, 12 deletions
diff --git a/functest/core/unit.py b/functest/core/unit.py index 6799420c7..515a20806 100644 --- a/functest/core/unit.py +++ b/functest/core/unit.py @@ -26,10 +26,11 @@ __author__ = ("Steven Pisarski <s.pisarski@cablelabs.com>, " class Suite(testcase.TestCase): """Base model for running unittest.TestSuite.""" + __logger = logging.getLogger(__name__) + def __init__(self, **kwargs): super(Suite, self).__init__(**kwargs) self.suite = None - self.logger = logging.getLogger(__name__) def run(self, **kwargs): """Run the test suite. @@ -61,24 +62,28 @@ class Suite(testcase.TestCase): try: self.suite = unittest.TestLoader().loadTestsFromName(name) except ImportError: - self.logger.error("Can not import %s", name) + self.__logger.error("Can not import %s", name) return testcase.TestCase.EX_RUN_ERROR except KeyError: pass - self.start_time = time.time() - stream = six.StringIO() - result = unittest.TextTestRunner( - stream=stream, verbosity=2).run(self.suite) - self.logger.debug("\n\n%s", stream.getvalue()) - self.stop_time = time.time() - self.details = {"failures": result.failures, - "errors": result.errors} try: + assert self.suite + self.start_time = time.time() + stream = six.StringIO() + result = unittest.TextTestRunner( + stream=stream, verbosity=2).run(self.suite) + self.__logger.debug("\n\n%s", stream.getvalue()) + self.stop_time = time.time() + self.details = {"failures": result.failures, + "errors": result.errors} self.result = 100 * ( (result.testsRun - (len(result.failures) + len(result.errors))) / result.testsRun) return testcase.TestCase.EX_OK + except AssertionError: + self.__logger.error("No suite is defined") + return testcase.TestCase.EX_RUN_ERROR except ZeroDivisionError: - self.logger.error("No test has been run") + self.__logger.error("No test has been run") return testcase.TestCase.EX_RUN_ERROR diff --git a/functest/tests/unit/core/test_unit.py b/functest/tests/unit/core/test_unit.py index f86ea8d31..79c4e7d7a 100644 --- a/functest/tests/unit/core/test_unit.py +++ b/functest/tests/unit/core/test_unit.py @@ -20,6 +20,7 @@ class PyTestSuiteRunnerTesting(unittest.TestCase): def setUp(self): self.psrunner = unit.Suite() + self.psrunner.suite = "foo" @mock.patch('unittest.TestLoader') def _test_run(self, mock_class=None, result=mock.Mock(), @@ -30,7 +31,10 @@ class PyTestSuiteRunnerTesting(unittest.TestCase): mock_class.assert_not_called() def test_check_suite_null(self): - self.assertEqual(self.psrunner.suite, None) + self.assertEqual(unit.Suite().suite, None) + self.psrunner.suite = None + self._test_run(result=mock.Mock(), + status=testcase.TestCase.EX_RUN_ERROR) def test_run_no_ut(self): mock_result = mock.Mock(testsRun=0, errors=[], failures=[]) |