3 # Copyright (c) 2016 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 the parent class of Functest TestCase."""
14 import functest.utils.functest_logger as ft_logger
15 import functest.utils.functest_utils as ft_utils
17 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
20 class TestCase(object):
21 """Parent class of Functest TestCase."""
24 EX_RUN_ERROR = os.EX_SOFTWARE
25 EX_PUSH_TO_DB_ERROR = os.EX_SOFTWARE - 1
26 EX_TESTCASE_FAILED = os.EX_SOFTWARE - 2
28 logger = ft_logger.Logger(__name__).getLogger()
30 def __init__(self, case_name=""):
32 self.project_name = "functest"
33 self.case_name = case_name
38 def check_criteria(self):
39 """Interpret the results of TestCase.
41 It allows getting the results of TestCase. It completes run()
42 which only returns the execution status.
44 It can be overriden if checking criteria is not suitable.
47 TestCase.EX_OK if criteria is 'PASS'.
48 TestCase.EX_TESTCASE_FAILED otherwise.
52 if self.criteria == 'PASS':
54 except AssertionError:
55 self.logger.error("Please run test before checking the results")
56 return TestCase.EX_TESTCASE_FAILED
58 def run(self, **kwargs):
61 It allows running TestCase and getting its execution
64 The subclasses must override the default implementation which
65 is false on purpose. The only prerequisite is to set the
66 following attributes to push the results to DB:
73 **kwargs: Arbitrary keyword arguments.
76 TestCase.EX_RUN_ERROR.
78 # pylint: disable=unused-argument
79 self.logger.error("Run must be implemented")
80 return TestCase.EX_RUN_ERROR
83 """Push the results of TestCase to the DB.
85 It allows publishing the results and to check the status.
87 It could be overriden if the common implementation is not
88 suitable. The following attributes must be set before pushing
97 TestCase.EX_OK if results were pushed to DB.
98 TestCase.EX_PUSH_TO_DB_ERROR otherwise.
101 assert self.project_name
102 assert self.case_name
104 assert self.start_time
105 assert self.stop_time
106 if ft_utils.push_results_to_db(
107 self.project_name, self.case_name, self.start_time,
108 self.stop_time, self.criteria, self.details):
109 self.logger.info("The results were successfully pushed to DB")
110 return TestCase.EX_OK
112 self.logger.error("The results cannot be pushed to DB")
113 return TestCase.EX_PUSH_TO_DB_ERROR
114 except Exception: # pylint: disable=broad-except
115 self.logger.exception("The results cannot be pushed to DB")
116 return TestCase.EX_PUSH_TO_DB_ERROR