3 # Copyright (c) 2019 Orange and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 """Define classes required to run any Behave test suites."""
12 from __future__ import division
20 from behave.__main__ import main as behave_main
22 from xtesting.core import testcase
24 __author__ = "Deepak Chandella <deepak.chandella@orange.com>"
27 class BehaveFramework(testcase.TestCase):
28 """BehaveFramework runner."""
29 # pylint: disable=too-many-instance-attributes
31 __logger = logging.getLogger(__name__)
33 def __init__(self, **kwargs):
34 super().__init__(**kwargs)
35 self.json_file = os.path.join(self.res_dir, 'output.json')
42 def parse_results(self):
43 """Parse output.json and get the details in it."""
44 with open(self.json_file) as stream_:
45 self.response = json.load(stream_)
47 self.total_tests = len(self.response)
48 for item in self.response:
49 if item['status'] == 'passed':
51 elif item['status'] == 'failed':
53 elif item['status'] == 'skipped':
56 self.pass_tests / self.total_tests)
58 self.details['total_tests'] = self.total_tests
59 self.details['pass_tests'] = self.pass_tests
60 self.details['fail_tests'] = self.fail_tests
61 self.details['skip_tests'] = self.skip_tests
62 self.details['tests'] = self.response
64 def run(self, **kwargs):
65 """Run the BehaveFramework feature files
68 * create the output directories if required,
69 * run behave features with parameters
70 * get the results in output.json,
73 kwargs: Arbitrary keyword arguments.
76 EX_OK if all suites ran well.
77 EX_RUN_ERROR otherwise.
80 suites = kwargs["suites"]
82 self.__logger.exception("Mandatory args were not passed")
83 return self.EX_RUN_ERROR
84 if not os.path.exists(self.res_dir):
86 os.makedirs(self.res_dir)
87 except Exception: # pylint: disable=broad-except
88 self.__logger.exception("Cannot create %s", self.res_dir)
89 return self.EX_RUN_ERROR
90 config = ['--junit', '--junit-directory={}'.format(self.res_dir),
91 '--format=json', '--outfile={}'.format(self.json_file)]
92 html_file = os.path.join(self.res_dir, 'output.html')
93 config += ['--format=behave_html_formatter:HTMLFormatter',
94 '--outfile={}'.format(html_file)]
95 if kwargs.get("tags", False):
96 config += ['--tags='+','.join(kwargs.get("tags", []))]
97 if kwargs.get("console", False):
98 config += ['--format=pretty', '--outfile=-']
99 for feature in suites:
100 config.append(feature)
101 self.start_time = time.time()
103 self.stop_time = time.time()
107 self.__logger.info("Results were successfully parsed")
108 except Exception: # pylint: disable=broad-except
109 self.__logger.exception("Cannot parse results")
110 return self.EX_RUN_ERROR