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 all Functest TestCases."""
15 import functest.utils.functest_utils as ft_utils
17 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
20 class TestCase(object):
21 """Base model for single test case."""
24 """everything is OK"""
26 EX_RUN_ERROR = os.EX_SOFTWARE
29 EX_PUSH_TO_DB_ERROR = os.EX_SOFTWARE - 1
30 """push_to_db() failed"""
32 EX_TESTCASE_FAILED = os.EX_SOFTWARE - 2
33 """results are false"""
35 __logger = logging.getLogger(__name__)
37 def __init__(self, **kwargs):
39 self.project_name = kwargs.get('project_name', 'functest')
40 self.case_name = kwargs.get('case_name', '')
41 self.criteria = kwargs.get('criteria', 100)
46 def get_duration(self):
47 """Return the duration of the test case.
50 duration if start_time and stop_time are set
54 assert self.start_time
56 if self.stop_time < self.start_time:
58 return "{0[0]:02.0f}:{0[1]:02.0f}".format(divmod(
59 self.stop_time - self.start_time, 60))
61 self.__logger.error("Please run test before getting the duration")
64 def is_successful(self):
65 """Interpret the result of the test case.
67 It allows getting the result of TestCase. It completes run()
68 which only returns the execution status.
70 It can be overriden if checking result is not suitable.
73 TestCase.EX_OK if result is 'PASS'.
74 TestCase.EX_TESTCASE_FAILED otherwise.
78 if isinstance(self.result, int) and isinstance(self.criteria, int):
79 if self.result >= self.criteria:
82 # Backward compatibility
83 # It must be removed as soon as TestCase subclasses
84 # stop setting result = 'PASS' or 'FAIL'.
85 # In this case criteria is unread.
86 self.__logger.warning(
87 "Please update result which must be an int!")
88 if self.result == 'PASS':
90 except AssertionError:
91 self.__logger.error("Please run test before checking the results")
92 return TestCase.EX_TESTCASE_FAILED
94 def run(self, **kwargs):
97 It allows running TestCase and getting its execution
100 The subclasses must override the default implementation which
103 The new implementation must set the following attributes to
104 push the results to DB:
111 kwargs: Arbitrary keyword arguments.
114 TestCase.EX_RUN_ERROR.
116 # pylint: disable=unused-argument
117 self.__logger.error("Run must be implemented")
118 return TestCase.EX_RUN_ERROR
120 def push_to_db(self):
121 """Push the results of the test case to the DB.
123 It allows publishing the results and to check the status.
125 It could be overriden if the common implementation is not
126 suitable. The following attributes must be set before pushing
136 TestCase.EX_OK if results were pushed to DB.
137 TestCase.EX_PUSH_TO_DB_ERROR otherwise.
140 assert self.project_name
141 assert self.case_name
142 assert self.start_time
143 assert self.stop_time
144 pub_result = 'PASS' if self.is_successful(
145 ) == TestCase.EX_OK else 'FAIL'
146 if ft_utils.push_results_to_db(
147 self.project_name, self.case_name, self.start_time,
148 self.stop_time, pub_result, self.details):
150 "The results were successfully pushed to DB")
151 return TestCase.EX_OK
153 self.__logger.error("The results cannot be pushed to DB")
154 return TestCase.EX_PUSH_TO_DB_ERROR
155 except Exception: # pylint: disable=broad-except
156 self.__logger.exception("The results cannot be pushed to DB")
157 return TestCase.EX_PUSH_TO_DB_ERROR