def _analysis_variable_keywords(self, step): step_split = step.split('=', 1) if len(step_split) != 2: raise SyntaxError(f'"{step}"') elif not step_split[-1].strip(): raise SyntaxError(f'"{step}"') name = step_split[0].strip()[2:-1] var_value = step_split[-1].strip() if re.match(r'\$\.(\w)+\(.*\)', var_value): key = var_value.split('(', 1)[0].strip() if key == '$.id': parms = [self._get_replace_string(var_value.split(key, 1)[-1][1:-1])] else: parms = self._get_parms(var_value.split(key, 1)[-1]) elif re.match(r'(\w)+\(.*\)', var_value): key = var_value.split('(', 1)[0].strip() parms = self._get_parms(var_value.lstrip(key)) else: key = None parms = [self._get_params_type(var_value)] action_data = Dict({ 'key': 'variable', 'parms': parms, 'name': name, 'func': key, 'step': step }) return action_data
def _analysis_other_keywords(self, step): key = step.split(' ', 1)[0].strip() parms = self._get_replace_string(step.lstrip(key).strip()) action_data = Dict({ 'key': key, 'parms': [parms], 'step': f'{key} {parms}' }) return action_data
def _analysis_not_exist_parms_keywords(self, step): key = step parms = None action_data = Dict({ 'key': key, 'parms': parms, 'step': step }) return action_data
def _analysis_exist_parms_keywords(self, step): key = step.split('(', 1)[0].strip() parms = self._get_parms(step.lstrip(key)) action_data = Dict({ 'key': key, 'parms': parms, 'step': step }) return action_data
def _analysis_common_keywords(self, step, style): key = step.split('call', 1)[-1].strip().split('(', 1)[0].strip() parms = step.split('call', 1)[-1].strip().split(key, 1)[-1] parms = self._get_parms(parms) action_data = Dict({ 'key': 'call', 'parms': parms, 'func': key, 'style': style, 'step': step }) return action_data
def analytical_file(path): ''' analytical file :param path: :return: ''' with open(path, "r", encoding='utf-8') as f: yaml_data = yaml.load(f, Loader=yaml.FullLoader) yaml_dict = Dict() if yaml_data: for key, value in yaml_data.items(): yaml_dict[key] = value return yaml_dict
def __analysis_setVar_keywords(self, step): key = '$.setVar' parms = self.__get_parms(step.lstrip('$.setVar')) if len(parms) != 2: raise SyntaxError( f'"{step}" Missing required parameter key or value!') if not isinstance(parms[0], str): raise TypeError(f'"{step}" Key must be str, not {type(parms[0])}') action_data = Dict({ 'key': key, 'parms': parms, 'tag': 'setVar', 'step': step }) return action_data
def _analysis_for_keywords(self, step): f_p = re.search(r'for\s+(\$\{\w+\})\s+in\s+(\S+)', step) f_t = f_p.groups() if len(f_t) != 2: raise SyntaxError(f'"{step}"') # 迭代值 iterating = f_t[0][2:-1] # 迭代对象 parms = self._get_params_type(f_t[1]) action_data = Dict({ 'key': 'for', 'parms': [parms], 'value': iterating, 'step': f'for {f_t[0]} in {self._get_params_type(f_t[1])}' }) return action_data
def run(self, test): ''' :param test: :return: ''' result = self._makeResult() result.failfast = self.failfast result.buffer = self.buffer starTime = time.time() test(result) stopTime = time.time() test_result = Dict() for modulek, modulev in result.result.items(): test_list = [] for info in modulev: case_info = Dict({ 'caseName': info.case_name, 'casePath': info.case_path, 'dataId': info.data_id, 'description': info.description, 'moduleName': info.module_name, 'report': info.report, 'snapshotDir': info.snapshot_dir, 'startTime': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(info.start_time)), 'duration': str(int(info.stop_time - info.start_time)) + 's', 'status': info.status, 'err': info.err, 'steps': info.test_case_steps }) test_list.append(case_info) test_result[modulek] = test_list failures_list = [] for failure in result.failures: cast_info = failure[0] failures_list.append(cast_info.test_case_path) errors_list = [] for errors in result.errors: cast_info = errors[0] errors_list.append(cast_info.test_case_path) result = Dict({ 'report': result.report, 'total': result.testsRun, 'successes': len(result.successes), 'failures': len(result.failures), 'errors': len(result.errors), 'skipped': len(result.skipped), 'startTime': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(starTime)), 'duration': str(int(stopTime - starTime)) + 's', 'result': test_result, 'errorsList': errors_list, 'failuresList': failures_list }) properties_path = os.path.join(Var.root, 'result.properties') with open(properties_path, "w") as f: f.write(f'report={result.report}\n') f.write(f'total={result.total}\n') f.write(f'successes={result.successes}\n') f.write(f'failures={result.failures}\n') f.write(f'errors={result.errors}\n') f.write(f'skipped={result.skipped}\n') json_path = os.path.join(result.report, 'result.json') with open(json_path, 'w') as f: json.dump(result, fp=f, cls=DictEncoder, indent=4) html_file = os.path.join(Var.report, 'report.html') fp = open(html_file, 'wb') html_runner = HTMLTestRunner(stream=fp, title='Test Results', description='Test') html_runner.generate_report(result) Var.all_result = result fp.close() return result