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."""
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 """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 = ft_logger.Logger(__name__).getLogger()
37 def __init__(self, **kwargs):
39 self.project_name = kwargs.get('project_name', 'functest')
40 self.case_name = kwargs.get('case_name', '')
45 def check_result(self):
46 """Interpret the result of the test case.
48 It allows getting the result of TestCase. It completes run()
49 which only returns the execution status.
51 It can be overriden if checking result is not suitable.
54 TestCase.EX_OK if result is 'PASS'.
55 TestCase.EX_TESTCASE_FAILED otherwise.
59 if self.result == 'PASS':
61 except AssertionError:
62 self.logger.error("Please run test before checking the results")
63 return TestCase.EX_TESTCASE_FAILED
65 def run(self, **kwargs):
68 It allows running TestCase and getting its execution
71 The subclasses must override the default implementation which
74 The new implementation must set the following attributes to
75 push the results to DB:
82 kwargs: Arbitrary keyword arguments.
85 TestCase.EX_RUN_ERROR.
87 # pylint: disable=unused-argument
88 self.logger.error("Run must be implemented")
89 return TestCase.EX_RUN_ERROR
92 """Push the results of the test case to the DB.
94 It allows publishing the results and to check the status.
96 It could be overriden if the common implementation is not
97 suitable. The following attributes must be set before pushing
107 TestCase.EX_OK if results were pushed to DB.
108 TestCase.EX_PUSH_TO_DB_ERROR otherwise.
111 assert self.project_name
112 assert self.case_name
114 assert self.start_time
115 assert self.stop_time
116 if ft_utils.push_results_to_db(
117 self.project_name, self.case_name, self.start_time,
118 self.stop_time, self.result, self.details):
119 self.logger.info("The results were successfully pushed to DB")
120 return TestCase.EX_OK
122 self.logger.error("The results cannot be pushed to DB")
123 return TestCase.EX_PUSH_TO_DB_ERROR
124 except Exception: # pylint: disable=broad-except
125 self.logger.exception("The results cannot be pushed to DB")
126 return TestCase.EX_PUSH_TO_DB_ERROR