diff options
Diffstat (limited to 'functest/core')
-rw-r--r-- | functest/core/feature_base.py | 58 | ||||
-rw-r--r-- | functest/core/pytest_suite_runner.py | 55 | ||||
-rw-r--r-- | functest/core/testcase_base.py | 10 |
3 files changed, 123 insertions, 0 deletions
diff --git a/functest/core/feature_base.py b/functest/core/feature_base.py new file mode 100644 index 00000000..01a27f30 --- /dev/null +++ b/functest/core/feature_base.py @@ -0,0 +1,58 @@ +import time + +import testcase_base as base +import functest.utils.functest_utils as ft_utils +import functest.utils.functest_logger as ft_logger + + +class FeatureBase(base.TestcaseBase): + def __init__(self, project='functest', case='', repo='', cmd=''): + super(FeatureBase, self).__init__() + self.project_name = project + self.case_name = case + self.cmd = cmd + self.repo = self.get_conf('general.directories.{}'.format(repo)) + self.result_file = self.get_result_file() + self.logger = ft_logger.Logger(project).getLogger() + + def run(self, **kwargs): + self.prepare() + self.start_time = time.time() + ret = ft_utils.execute_command(self.cmd, output_file=self.result_file) + self.stop_time = time.time() + self.post() + self.parse_results(ret) + self.log_results() + return base.TestcaseBase.EX_OK + + def prepare(self, **kwargs): + pass + + def post(self, **kwargs): + pass + + def parse_results(self, ret): + exit_code = base.TestcaseBase.EX_OK + if ret == 0: + self.logger.info("{} OK".format(self.project_name)) + self.criteria = 'PASS' + else: + self.logger.info("{} FAILED".format(self.project_name)) + exit_code = base.TestcaseBase.EX_RUN_ERROR + self.criteria = "FAIL" + + return exit_code + + def get_result_file(self): + dir = self.get_conf('general.directories.dir_results') + return "{}/{}.log".format(dir, self.project_name) + + def log_results(self): + ft_utils.logger_test_results(self.project_name, + self.case_name, + self.criteria, + self.details) + + @staticmethod + def get_conf(parameter): + return ft_utils.get_functest_config(parameter) diff --git a/functest/core/pytest_suite_runner.py b/functest/core/pytest_suite_runner.py new file mode 100644 index 00000000..2d5b2667 --- /dev/null +++ b/functest/core/pytest_suite_runner.py @@ -0,0 +1,55 @@ +#!/usr/bin/python +# +# Copyright (c) 2015 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 + +import testcase_base as base +import unittest +import time + + +class PyTestSuiteRunner(base.TestcaseBase): + """ + This superclass is designed to execute pre-configured unittest.TestSuite() + objects + """ + def __init__(self): + super(PyTestSuiteRunner, self).__init__() + self.suite = None + + def run(self, **kwargs): + """ + Starts test execution from the functest framework + """ + self.start_time = time.time() + result = unittest.TextTestRunner(verbosity=2).run(self.suite) + self.stop_time = time.time() + + if result.errors: + self.logger.error('Number of errors in test suite - ' + + str(len(result.errors))) + for test, message in result.errors: + self.logger.error(str(test) + " ERROR with " + message) + + if result.failures: + self.logger.error('Number of failures in test suite - ' + + str(len(result.failures))) + for test, message in result.failures: + self.logger.error(str(test) + " FAILED with " + message) + + if (result.errors and len(result.errors) > 0) \ + or (result.failures and len(result.failures) > 0): + self.logger.info("%s FAILED" % self.case_name) + self.criteria = 'FAIL' + exit_code = base.TestcaseBase.EX_RUN_ERROR + else: + self.logger.info("%s OK" % self.case_name) + exit_code = base.TestcaseBase.EX_OK + self.criteria = 'PASS' + + self.details = {} + return exit_code diff --git a/functest/core/testcase_base.py b/functest/core/testcase_base.py index e869803d..838b6398 100644 --- a/functest/core/testcase_base.py +++ b/functest/core/testcase_base.py @@ -18,6 +18,7 @@ class TestcaseBase(object): EX_OK = os.EX_OK EX_RUN_ERROR = os.EX_SOFTWARE EX_PUSH_TO_DB_ERROR = os.EX_SOFTWARE - 1 + EX_TESTCASE_FAILED = os.EX_SOFTWARE - 2 logger = ft_logger.Logger(__name__).getLogger() @@ -29,6 +30,15 @@ class TestcaseBase(object): self.start_time = "" self.stop_time = "" + def check_criteria(self): + try: + assert self.criteria + if self.criteria == 'PASS': + return TestcaseBase.EX_OK + except: + self.logger.error("Please run test before checking the results") + return TestcaseBase.EX_TESTCASE_FAILED + def run(self, **kwargs): self.logger.error("Run must be implemented") return TestcaseBase.EX_RUN_ERROR |