diff options
Diffstat (limited to 'xtesting/core')
-rw-r--r-- | xtesting/core/behaveframework.py | 120 | ||||
-rw-r--r-- | xtesting/core/robotframework.py | 12 |
2 files changed, 124 insertions, 8 deletions
diff --git a/xtesting/core/behaveframework.py b/xtesting/core/behaveframework.py new file mode 100644 index 00000000..d8a61ef3 --- /dev/null +++ b/xtesting/core/behaveframework.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python + +# Copyright (c) 2019 Orange and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +"""Define classes required to run any Behave test suites.""" + +from __future__ import division + +import logging +import os +import time +import json + +from behave.__main__ import main as behave_main + +from xtesting.core import testcase + +__author__ = "Deepak Chandella <deepak.chandella@orange.com>" + + +class BehaveFramework(testcase.TestCase): + """BehaveFramework runner.""" + # pylint: disable=too-many-instance-attributes + + __logger = logging.getLogger(__name__) + dir_results = "/var/lib/xtesting/results" + + def __init__(self, **kwargs): + super(BehaveFramework, self).__init__(**kwargs) + self.res_dir = os.path.join(self.dir_results, self.case_name) + self.json_file = os.path.join(self.res_dir, 'output.json') + self.total_tests = 0 + self.pass_tests = 0 + self.fail_tests = 0 + self.skip_tests = 0 + self.response = None + + def parse_results(self): + """Parse output.json and get the details in it.""" + + try: + with open(self.json_file) as stream_: + self.response = json.load(stream_) + except IOError: + self.__logger.error("Error reading the file %s", self.json_file) + + try: + if self.response: + self.total_tests = len(self.response) + for item in self.response: + if item['status'] == 'passed': + self.pass_tests += 1 + elif item['status'] == 'failed': + self.fail_tests += 1 + elif item['status'] == 'skipped': + self.skip_tests += 1 + except KeyError: + self.__logger.error("Error in json - %s", self.response) + + try: + self.result = 100 * ( + self.pass_tests / self.total_tests) + except ZeroDivisionError: + self.__logger.error("No test has been run") + + self.details = {} + self.details['total_tests'] = self.total_tests + self.details['pass_tests'] = self.pass_tests + self.details['fail_tests'] = self.fail_tests + self.details['skip_tests'] = self.skip_tests + self.details['tests'] = self.response + + def run(self, **kwargs): + """Run the BehaveFramework feature files + + Here are the steps: + * create the output directories if required, + * run behave features with parameters + * get the results in output.json, + + Args: + kwargs: Arbitrary keyword arguments. + + Returns: + EX_OK if all suites ran well. + EX_RUN_ERROR otherwise. + """ + try: + suites = kwargs["suites"] + tags = kwargs.get("tags", []) + except KeyError: + self.__logger.exception("Mandatory args were not passed") + return self.EX_RUN_ERROR + if not os.path.exists(self.res_dir): + try: + os.makedirs(self.res_dir) + except Exception: # pylint: disable=broad-except + self.__logger.exception("Cannot create %s", self.res_dir) + return self.EX_RUN_ERROR + config = ['--tags='+','.join(tags), + '--format=json', + '--outfile='+self.json_file] + for feature in suites: + config.append(feature) + self.start_time = time.time() + behave_main(config) + self.stop_time = time.time() + + try: + self.parse_results() + self.__logger.info("Results were successfully parsed") + except Exception: # pylint: disable=broad-except + self.__logger.exception("Cannot parse results") + return self.EX_RUN_ERROR + return self.EX_OK diff --git a/xtesting/core/robotframework.py b/xtesting/core/robotframework.py index 2791b559..3cb0ad31 100644 --- a/xtesting/core/robotframework.py +++ b/xtesting/core/robotframework.py @@ -11,7 +11,6 @@ from __future__ import division -import errno import logging import os @@ -110,15 +109,12 @@ class RobotFramework(testcase.TestCase): except KeyError: self.__logger.exception("Mandatory args were not passed") return self.EX_RUN_ERROR - try: - os.makedirs(self.res_dir) - except OSError as ex: - if ex.errno != errno.EEXIST: + if not os.path.exists(self.res_dir): + try: + os.makedirs(self.res_dir) + except Exception: # pylint: disable=broad-except self.__logger.exception("Cannot create %s", self.res_dir) return self.EX_RUN_ERROR - except Exception: # pylint: disable=broad-except - self.__logger.exception("Cannot create %s", self.res_dir) - return self.EX_RUN_ERROR stream = StringIO() robot.run(*suites, variable=variable, variablefile=variablefile, include=include, output=self.xml_file, log='NONE', |