Define logger as Feature instance attribute
authorJose Lausuch <jose.lausuch@ericsson.com>
Mon, 12 Jun 2017 15:10:51 +0000 (23:10 +0800)
committerjose.lausuch <jose.lausuch@ericsson.com>
Wed, 21 Jun 2017 11:30:59 +0000 (13:30 +0200)
It allows any Feature subclass to print warning messages in console
and debug messages in a dedicated file.

Co-Authored-By: Cédric Ollivier <cedric.ollivier@orange.com>
Change-Id: Ic5b1b1184c16cf50f0baadc3904075d0acdf3c6d
Signed-off-by: Jose Lausuch <jose.lausuch@ericsson.com>
Signed-off-by: Cédric Ollivier <cedric.ollivier@orange.com>
functest/core/feature.py
functest/tests/unit/core/test_feature.py

index d53eb7d..010ff4b 100644 (file)
@@ -33,6 +33,24 @@ class Feature(base.TestCase):
         super(Feature, self).__init__(**kwargs)
         self.result_file = "{}/{}.log".format(
             CONST.__getattribute__('dir_results'), self.case_name)
+        try:
+            module = kwargs['run']['module']
+            self.logger = logging.getLogger(module)
+        except KeyError:
+            self.__logger.warning(
+                "Cannot get module name %s. Using %s as fallback",
+                kwargs, self.case_name)
+            self.logger = logging.getLogger(self.case_name)
+        handler = logging.StreamHandler()
+        handler.setLevel(logging.WARN)
+        self.logger.addHandler(handler)
+        handler = logging.FileHandler(self.result_file)
+        handler.setLevel(logging.DEBUG)
+        self.logger.addHandler(handler)
+        formatter = logging.Formatter(
+            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
+        handler.setFormatter(formatter)
+        self.logger.addHandler(handler)
 
     def execute(self, **kwargs):
         """Execute the Python method.
index 0160c8e..988981e 100644 (file)
@@ -38,12 +38,26 @@ class FeatureTestingBase(unittest.TestCase):
         self.assertEqual(self.feature.start_time, 1)
         self.assertEqual(self.feature.stop_time, 2)
 
+    def test_logger_module_ko(self):
+        with mock.patch('six.moves.builtins.open'):
+            self.feature = feature.Feature(
+                project_name=self._project_name, case_name=self._case_name)
+            self.assertEqual(self.feature.logger.name, self._case_name)
+
+    def test_logger_module(self):
+        with mock.patch('six.moves.builtins.open'):
+            self.feature = feature.Feature(
+                project_name=self._project_name, case_name=self._case_name,
+                run={'module': 'bar'})
+            self.assertEqual(self.feature.logger.name, 'bar')
+
 
 class FeatureTesting(FeatureTestingBase):
 
     def setUp(self):
-        self.feature = feature.Feature(
-            project_name=self._project_name, case_name=self._case_name)
+        with mock.patch('six.moves.builtins.open'):
+            self.feature = feature.Feature(
+                project_name=self._project_name, case_name=self._case_name)
 
     def test_run_exc(self):
         # pylint: disable=bad-continuation
@@ -60,8 +74,9 @@ class FeatureTesting(FeatureTestingBase):
 class BashFeatureTesting(FeatureTestingBase):
 
     def setUp(self):
-        self.feature = feature.BashFeature(
-            project_name=self._project_name, case_name=self._case_name)
+        with mock.patch('six.moves.builtins.open'):
+            self.feature = feature.BashFeature(
+                project_name=self._project_name, case_name=self._case_name)
 
     @mock.patch("functest.utils.functest_utils.execute_command")
     def test_run_no_cmd(self, mock_method=None):