behave_tests: add secondary logging infrastructure 84/72784/2
authorGwenael Lambrouin <gwenael.lambrouin@orange.com>
Fri, 16 Jul 2021 13:40:16 +0000 (15:40 +0200)
committerGwenael Lambrouin <gwenael.lambrouin@orange.com>
Thu, 22 Jul 2021 15:08:18 +0000 (17:08 +0200)
In addition to the main logger managed by behave framework to show the test
progress and the results summary, add a second logger to provide insight on
what's going on internally.

That second logger logs to a file in the results dir.

Change-Id: Ie2e8012ea54b153a2e661a06a2c521f48e7040b2
Signed-off-by: Gwenael Lambrouin <gwenael.lambrouin@orange.com>
behave_tests/features/environment.py

index 7789ca8..61096f6 100644 (file)
@@ -17,6 +17,7 @@
 
 import json
 import os
+import logging
 import pathlib
 import time
 
@@ -45,6 +46,22 @@ def before_feature(context, feature):
     context.start_time = time.time()
     context.CASE_NAME = feature.name
 
+    # Create results dir if needed
+    results_dir = pathlib.Path('/var/lib/xtesting/results/' + context.CASE_NAME)
+    if not results_dir.exists():
+        results_dir.mkdir()
+
+    # Setup a second logger to be able to understand why a test passed or failed
+    # (The main logger is used by behave itself)
+    context.logger = logging.getLogger('behave_tests')
+    context.logger.setLevel(logging.INFO)
+    fh = logging.FileHandler(filename=results_dir / pathlib.Path('behave_tests.log'),
+                             mode='w')  # Re-create the file at the beginning of the feature
+    fh.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
+    context.logger.addHandler(fh)
+
+    context.logger.info('before_feature: ' + feature.name)
+
 
 def before_scenario(context, scenario):
     context.tag = scenario.tags[0]
@@ -57,6 +74,8 @@ def before_scenario(context, scenario):
         context.json['flavor_type'] = loopvm_flavor
     context.synthesis = {}
 
+    context.logger.info('before_scenario: ' + scenario.name)
+
 
 def after_feature(context, feature):
     if len(context.results) == 0:
@@ -64,8 +83,5 @@ def after_feature(context, feature):
         return
 
     results_dir = pathlib.Path('/var/lib/xtesting/results/' + context.CASE_NAME)
-    if not results_dir.exists():
-        results_dir.mkdir()
-
     results_file = results_dir / pathlib.Path('campaign_result.json')
     results_file.write_text(json.dumps(context.results, indent=4))