1 # Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http:#www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 """ Interface for test cases. """
31 XUNIT_FILE_NAME = "XUNIT_RESULT.xml"
32 XUNIT_RECEIVER = xunitgen.EventReceiver()
33 XUNIT_DEFAULT_TEST_SUITE = "test-suite"
36 class DefaultEnvConfig(object):
38 default test configs. There're 3 places to set configs, priority is (high -> low):
40 1. overwrite set by caller of test method
41 2. values set by test_method decorator
42 3. default env config get from this class
48 "env_config_file": None,
49 "test_suite_name": None,
53 def set_default_config(cls, **kwargs):
55 :param kwargs: configs need to be updated
58 cls.DEFAULT_CONFIG.update(kwargs)
61 def get_default_config(cls):
63 :return: current default config
65 return cls.DEFAULT_CONFIG.copy()
68 set_default_config = DefaultEnvConfig.set_default_config
69 get_default_config = DefaultEnvConfig.get_default_config
72 class TestResult(object):
79 def get_failed_cases(cls):
81 :return: failed test cases
83 return cls.TEST_RESULT["fail"]
86 def get_passed_cases(cls):
88 :return: passed test cases
90 return cls.TEST_RESULT["pass"]
93 def set_result(cls, result, case_name):
95 :param result: True or False
96 :param case_name: test case name
99 cls.TEST_RESULT["pass" if result else "fail"].append(case_name)
102 get_failed_cases = TestResult.get_failed_cases
103 get_passed_cases = TestResult.get_passed_cases
108 "env_tag": "default",
109 "category": "function",
114 def test_method(**kwargs):
116 decorator for test case function.
117 The following keyword arguments are pre-defined.
118 Any other keyword arguments will be regarded as filter for the test case,
119 able to access them by ``case_info`` attribute of test method.
121 :keyword app: class for test app. see :doc:`App <App>` for details
122 :keyword dut: class for current dut. see :doc:`DUT <DUT>` for details
123 :keyword env_tag: name for test environment, used to select configs from config file
124 :keyword env_config_file: test env config file. usually will not set this keyword when define case
125 :keyword test_suite_name: test suite name, used for generating log folder name and adding xunit format test result.
126 usually will not set this keyword when define case
129 # get test function file name
130 frame = inspect.stack()
131 test_func_file_name = frame[1][1]
133 case_info = MANDATORY_INFO.copy()
134 case_info["name"] = case_info["ID"] = test_func.__name__
135 case_info.update(kwargs)
137 @functools.wraps(test_func)
138 def handle_test(extra_data=None, **overwrite):
140 create env, run test and record test results
142 :param extra_data: extra data that runner or main passed to test case
143 :param overwrite: args that runner or main want to overwrite
146 # create env instance
147 env_config = DefaultEnvConfig.get_default_config()
149 if key in env_config:
150 env_config[key] = kwargs[key]
152 env_config.update(overwrite)
153 env_inst = Env.Env(**env_config)
154 # prepare for xunit test results
155 xunit_file = os.path.join(env_inst.app_cls.get_log_folder(env_config["test_suite_name"]),
157 XUNIT_RECEIVER.begin_case(test_func.__name__, time.time(), test_func_file_name)
160 Utility.console_log("starting running test: " + test_func.__name__, color="green")
161 # execute test function
162 test_func(env_inst, extra_data)
163 # if finish without exception, test result is True
165 except Exception as e:
166 # handle all the exceptions here
167 traceback.print_exc()
169 XUNIT_RECEIVER.failure(str(e), test_func_file_name)
171 # do close all DUTs, if result is False then print DUT debug info
172 env_inst.close(dut_debug=(not result))
173 # end case and output result
174 XUNIT_RECEIVER.end_case(test_func.__name__, time.time())
175 with open(xunit_file, "ab+") as f:
176 f.write(xunitgen.toxml(XUNIT_RECEIVER.results(),
177 XUNIT_DEFAULT_TEST_SUITE))
180 Utility.console_log("Test Succeed: " + test_func.__name__, color="green")
182 Utility.console_log(("Test Fail: " + test_func.__name__), color="red")
183 TestResult.set_result(result, test_func.__name__)
186 handle_test.case_info = case_info
187 handle_test.test_method = True