def test_submit_website(self): '''演示POST请求,请求头内容类型为:Content-Type: application/x-www-form-urlencoded''' header = { 'Content-Type': 'application/x-www-form-urlencoded', 'charset': 'utf-8', 'Cookie': 'pharmconverge=7vql1rrh4pgslp9pjbpvm304b7' } self.http.set_header(header) #params = (self.params)[0] #params = str(params) #params = params.replace('|', ':') #params = params.encode('utf-8') #logger.info(params) # self.parmas = json.dumps(eval(self.params)) # 字符 # logger.info(self.params) # self.params = self.params.encode('utf-8') logger.info('正在发起POST请求...') response = self.http.post(self.url) #response = self.http.post(self.url, params) #response = response.decode('utf-8') print(response) logger.info('正在解析返回结果:%s' % response) # 解析HTML文档 #parser = MyHTMLParser(strict = False) #parser.feed(response) #self.assertNotEqual(str(i), self.expected_result['result'], msg='访问WebTours失败') logger.debug(response)
def test_submit_website(self): '''演示POST请求,请求头内容类型为:Content-Type: application/x-www-form-urlencoded''' header = { 'Content-Type': 'application/x-www-form-urlencoded', 'charset': 'utf-8' } self.http.set_header(header) params = (self.params)[0] params = str(params) params = params.replace('|', ':') params = params.encode('utf-8') logger.info(params) # self.parmas = json.dumps(eval(self.params)) # 字符 # logger.info(self.params) # self.params = self.params.encode('utf-8') logger.info('正在发起POST请求...') response = self.http.post(self.url, params) response = response.decode('utf-8') logger.info('正在解析返回结果:%s' % response) logger.debug(response)
def save_result(self, response_to_check): if '输出' in self.expected_result.keys(): # 需要提取服务器返回内容 output = self.expected_result['输出'] if type(output) == type({}): for key in output.keys(): if key.lower() == 'dic': # 如果为字典,则用键值提取 for var_name, extrator in output[key].items(): logger.info('使用键值提取') value_get = self.extrator( 'dic', extrator, response_to_check) # 获取键对应值 logger.info('获取到的变量的值为:%s' % value_get) self.outputs_dict[var_name] = value_get logger.info( '使用“键值提取”提取的自定义变量-值(key-value对)为:%s-%s' % (var_name, self.outputs_dict[var_name])) elif key.lower() == 're': for var_name, extrator in output[key].items(): logger.info('使用正则表达式提取') value_get = self.extrator('re', extrator, response_to_check) index = 1 tmp_var_name = var_name for item in value_get: logger.info('获取到的变量的值为:%s' % value_get) var_name = var_name + '_' + str(index) if var_name in self.outputs_dict.keys( ): # 已有存在值,替换已经存在的值 self.outputs_dict[var_name] = item continue else: self.outputs_dict[var_name] = item index = index + 1 logger.info( '使用“正则表达式提取”提取的自定义变量-值(key-value对)为:%s-%s' % (var_name, self.outputs_dict[var_name])) var_name = tmp_var_name elif key.lower() == 'xpath': for var_name, extrator in output[key].items(): logger.info('使用xpath提取') value_get = self.extrator('xpath', extrator, response_to_check) logger.info('获取到的变量的值为:%s' % value_get) self.outputs_dict[var_name] = value_get logger.info( '使用“xpath提取”提取的自定义变量-值(key-value对)为:%s-%s' % (var_name, self.outputs_dict[var_name])) logger.debug('提取的变量-值(key-value对)为:%s' % self.outputs_dict) else: logger.warn('未检测到从服务器返回中提取内容的要求,请检查是否正确填写预期结果')
def run_step(self, http=None): if '步骤类型' in self.action and (self.action['步骤类型']).lower() == '执行sql': # 执行sql脚本 step_run_result = self.run_sql_in_action() logger.debug('step_run_result:error, %s' % step_run_result[1]) return (step_run_result[0], [('CaseStep', step_run_result[1])]) else: try: if '类名' in self.action.keys(): class_name = self.action['类名'] else: class_name = 'InterfaceUnittestTestCase' if '函数' in self.action.keys(): function = self.action['函数'] else: function = 'test_interface_of_urlencode' logger.info('调用的方法为:%s.%s' % (class_name, function)) except Exception as e: logger.error('步骤[%s]信息填写不正确: %e,执行失败' % (self.step_number, e)) return ('Error', [('CaseStep', '%s' % e)]) # 替换动态参数 self.action['参数'] = self.__repalce_value_of_parmas_in_quest( self.action['参数']) self.action['url'] = self.__repalce_value_of_parmas_in_quest( self.action['url']) if self.expected_result != '' and '条件' in self.expected_result.keys( ): self.expected_result[ '条件'] = self.__repalce_value_of_parmas_in_quest( self.expected_result['条件']) if '请求头' in self.action.keys(): self.action['请求头'] = self.__repalce_value_of_parmas_in_quest( self.action['请求头']) self.action['请求头'] = json.dumps(self.action['请求头']) self.action['请求头'] = json.loads(self.action['请求头']) runner = unittest.TextTestRunner() test_step_action = unittest.TestSuite() test_step_action.addTest((globals()[class_name])(function, http, self)) step_run_result = runner.run(test_step_action) logger.debug('step_run_result:%s, errors:%s,failures:%s' % (step_run_result, step_run_result.errors, step_run_result.failures)) if 0 != len(step_run_result.errors): return ('Error', step_run_result.errors) elif 0 != len(step_run_result.failures): return ('Fail', step_run_result.failures) else: return ('Pass', '')
def save_onesql_query_result(self, sql_record): if self.expected_result and '输出' in self.expected_result.keys( ): # 需要提取服务器返回内容 output = self.expected_result['输出'] counter = 0 while counter < len(sql_record): for var_name, var_number in output.items(): var_number = int(var_number) #以防错误输入了字符串编号 temp_var_number = var_number - 1 if temp_var_number == counter: CaseStep.outputs_dict[var_name] = sql_record[ counter] # 已有存在值,替换已经存在的值 logger.debug('提取的输出结果(key-value对)为:%s-%s' % (var_name, sql_record[counter])) counter = counter + 1 logger.debug('提取的输出结果(key-value对)为:%s' % CaseStep.outputs_dict) else: logger.warn('未检测到从数据库服务器返回中提取内容的要求,请检查是否正确填写预期结果')
def run_step(self, http): try: class_name = self.action['class'] function = self.action['function'] logger.info('调用的方法为:%s.%s' % (class_name, function)) except Exception as e: logger.error('步骤[%s]信息填写不正确: %e,执行失败' % (self.step_number, e)) return ('Error', ('%s' % e)) runner = unittest.TextTestRunner() test_step_action = unittest.TestSuite() test_step_action.addTest((globals()[class_name])(function, http, self)) step_run_result = runner.run(test_step_action) logger.debug('step_run_result:%s, errors:%s,failures:%s' % (step_run_result, step_run_result.errors, step_run_result.failures)) if 0 != len(step_run_result.errors): return ('Error', step_run_result.errors) elif 0 != len(step_run_result.failures): return ('Fail', step_run_result.failures) else: return ('Pass', '')
def run_testcase(self, http, testplan): if 0 == self.active_status: logger.warning('用例[name=%s]处于禁用状态[active=0],不执行' % self.testcase_name) return ('Block', '用例处于禁用状态,不执行') protocol_all = http.get_protocol() host_all = http.get_host() port_all = http.get_port() for step in self.steps: # 构造测试步骤对象 step_id = int(step['id']) step_number = int(step['step_number']) step_action = other_tools.conver_date_from_testlink( step['actions']) logger.info('转换来自test_link 的step_action值为:%s' % step_action) expected_results = other_tools.conver_date_from_testlink( step['expected_results']) logger.info('转换来自test_link 的expected_results值为:%s' % expected_results) logger.debug('step_action: %s' % step_action) sql_insert = 'INSERT INTO '+ case_step_report_tb +'(executed_history_id, testcase_id, testcase_name, testplan, project, step_id, step_num, protocol_method, protocol, host, port, ' \ 'step_action, expected_results, runresult, reason, runtime)' \ ' VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)' if step_action.find('步骤类型') != -1 and step_action.find( '执行用例') != -1: # 待执行步骤为用例 data = (executed_history_id, self.testcase_id, self.testcase_name, testplan, self.testproject, step_id, step_number, '无', '无', '无', '无', step_action, expected_results, 'Block', '', '0000-00-00 00:00:00') logger.info('记录测试步骤到测试步骤报告表') testdb.execute_insert(sql_insert, data) elif step_action.find('步骤类型') != -1 and ( step_action.find('执行sql') != -1 or step_action.find('执行SQL') != -1): #: # 待执行步骤为sql data = (executed_history_id, self.testcase_id, self.testcase_name, testplan, self.testproject, step_id, step_number, '', '', testdb.get_host(), testdb.get_port(), step_action, expected_results, 'Block', '', '0000-00-00 00:00:00') logger.info('记录测试步骤到测试步骤报告表') testdb.execute_insert(sql_insert, data) elif step_action.find('步骤类型') == -1: # 待执行步骤为普通用例步骤 data = (executed_history_id, self.testcase_id, self.testcase_name, testplan, self.testproject, step_id, step_number, '', protocol_all, host_all, port_all, step_action, expected_results, 'Block', '', '0000-00-00 00:00:00') logger.info('记录测试步骤到测试步骤报告表') testdb.execute_insert(sql_insert, data) run_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # 记录运行时间 try: step_action = json.loads(step_action, object_pairs_hook=OrderedDict) if '步骤类型' not in step_action: protocol_method = step_action['方法'] else: protocol_method = '无' if expected_results != '': expected_results = json.loads(expected_results) step_obj = CaseStep(step_id, step_number, expected_results, step_action, self.testcase_id) except Exception as e: logger.error( '步骤[%s]信息填写错误: %s,停止执行用例[id=%s, name=%s]' % (step_number, e, self.testcase_id, self.testcase_name)) sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s' \ ' AND project=\'%s\' AND testplan=\'%s\'' data = ('Block', '%s' % e, protocol_method, run_time, str(executed_history_id), self.testcase_id, step_id, self.testproject, testplan) logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update, data) return ('Error', [ ('TestCase', '步骤[%s]信息填写错误: %s,停止执行用例[id=%s, name=%s]' % (step_number, e, self.testcase_id, self.testcase_name)) ]) logger.info('开始执行步骤操作[第%s步]' % step_number) run_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # 记录运行时间 if '步骤类型' in step_action: if step_action['步骤类型'] == '执行用例': step_run_result = globalpkg.global_function.run_testcase_by_id( step_action['用例id'], testplan) else: step_run_result = step_obj.run_step() # 运行sql,或者是普通用例步骤 else: step_run_result = step_obj.run_step(http) # 运行普通用例步骤 try: # 转换 “ 为 ‘ ,防止数据库存储出错 action_of_step = step_obj.get_action() if type(action_of_step) == type(b''): action_of_step = action_of_step.decode('utf-8') elif type(action_of_step) == type({}): action_of_step = str(action_of_step) action_of_step = json.dumps(action_of_step) action_of_step = action_of_step.replace('"', "'") result_of_step = step_obj.get_expected_result() if type(result_of_step) == type(b''): result_of_step = result_of_step.decode('utf-8') elif type(result_of_step) == type({}): result_of_step = str(result_of_step) result_of_step = result_of_step.replace('"', "'") # except Exception as e: # logger.error('获取step action、expected_result出错 %s' % e) # action_of_step = '' # result_of_step = '' finally: if step_run_result[0] == 'Error': fail_or_error_reason = step_run_result[1][0][1] fail_or_error_reason = fail_or_error_reason.replace( '\n', '') fail_or_error_reason = fail_or_error_reason.replace( '\"', '') fail_or_error_reason = fail_or_error_reason.replace( '\'', '') logger.error( '步骤[%s]执行出错,停止执行用例[id=%s, name=%s]' % (step_number, self.testcase_id, self.testcase_name)) sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\",' \ 'step_action=\"%s\", expected_results=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s' \ ' AND project=\'%s\' AND testplan=\'%s\'' data = (step_run_result[0], fail_or_error_reason, protocol_method, run_time, action_of_step, result_of_step, str(executed_history_id), self.testcase_id, step_id, self.testproject, testplan) logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update, data) return step_run_result elif step_run_result[0] == 'Fail': fail_or_error_reason = step_run_result[1][0][1] fail_or_error_reason = fail_or_error_reason.replace( '\n', '') fail_or_error_reason = fail_or_error_reason.replace( '\"', '') fail_or_error_reason = fail_or_error_reason.replace( '\'', '') logger.info( '步骤[%s]执行失败,停止执行用例[id=%s, name=%s]' % (step_number, self.testcase_id, self.testcase_name)) sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\",' \ 'step_action=\"%s\", expected_results=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s' \ ' AND project=\'%s\' AND testplan=\'%s\'' data = (step_run_result[0], fail_or_error_reason, protocol_method, run_time, action_of_step, result_of_step, str(executed_history_id), self.testcase_id, step_id, self.testproject, testplan) logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update, data) return step_run_result elif step_run_result[0] == 'Block': fail_or_error_reason = step_run_result[1] logger.info( '步骤[%s]执行失败,停止执行用例[id=%s, name=%s]' % (step_number, self.testcase_id, self.testcase_name)) sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s' \ ' AND project=\'%s\' AND testplan=\'%s\'' data = (step_run_result[0], fail_or_error_reason, '无', run_time, str(executed_history_id), self.testcase_id, step_id, self.testproject, testplan) logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update, data) return step_run_result else: fail_or_error_reason = '' sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\",' \ 'step_action=\"%s\", expected_results=\"%s\"'\ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s'\ ' AND project=\'%s\' AND testplan=\'%s\'' data = (step_run_result[0], fail_or_error_reason, protocol_method, run_time, action_of_step, result_of_step, str(executed_history_id), self.testcase_id, step_id, self.testproject, testplan) logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update, data) logger.info('测试用例[id=%s, name=%s]执行成功' % (self.testcase_id, self.testcase_name)) return ('Pass', '')
def run_testcase(self, http, testplan, case_executed_history_id): if 0 == self.active_status: logger.warning('用例[name=%s]处于禁用状态[active=0],不执行' % self.testcase_name) return ('Block', '用例处于禁用状态,不执行') if self.preconditions.find('使用上级配置') != -1: logger.info('用例[name=%s]使用上级套件的ip配置' % self.testcase_name) logger.info('正在读取用例[name=%s]所在套件的协议,host,端口配置...' % self.testcase_name) testsuite_info = mytestlink.getTestSuiteByID( self.testsuite_id) # 获取套件基本信息 testsuite_conf = other_tools.conver_date_from_testlink( testsuite_info['details']) testsuite_name = testsuite_info['name'] testsuite_id = testsuite_info['id'] testsuite_id_name = str(testsuite_id) + testsuite_name if testsuite_id_name in TestCase.testsuite_config.keys(): # 已存在配置 logger.info('检测到系统已保存用例[name=%s]所在套件[name=%s]配置,使用现有配置' % (testsuite_name, self.testcase_name)) http = TestCase.testsuite_config[testsuite_id_name] protocol = http.get_protocol() host = http.get_host() port = http.get_port() else: try: details = json.loads(testsuite_conf) protocol = details['protocol'] host = details['host'] port = details['port'] # 构造http对象 http = MyHttp(protocol, host, port) TestCase.testsuite_config[testsuite_id_name] = http except Exception as e: logger.error( '用例[name=%s]所在套件[name=%s]的协议,host,端口配置错误,未执行:%s' % (testsuite_name, self.testcase_name, e)) return ('Block', [ ('TestCase', '用例[name=%s]所在套件[name=%s]的协议,host,端口配置错误,未执行:%s' % (testsuite_name, self.testcase_name, e)) ]) else: # 使用统一的项目配置 protocol = http.get_protocol() host = http.get_host() port = http.get_port() for step in self.steps: # 构造测试步骤对象 step_id = int(step['id']) step_number = int(step['step_number']) step_action = other_tools.conver_date_from_testlink( step['actions']) logger.info('转换来自test_link 的step_action值为:%s' % step_action) expected_results = other_tools.conver_date_from_testlink( step['expected_results']) logger.info('转换来自test_link 的expected_results值为:%s' % expected_results) logger.debug('step_action: %s' % step_action) sql_insert = 'INSERT INTO '+ case_step_report_tb +'(executed_history_id, testcase_id, testcase_name, testplan, project, step_id, step_num, protocol_method, protocol, host, port, ' \ 'step_action, expected_results, runresult, reason, runtime)' \ ' VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)' if step_action.find('步骤类型') != -1 and step_action.find( '执行用例') != -1: # 待执行步骤为用例 data = (case_executed_history_id, self.testcase_id, self.testcase_name, testplan, self.testproject, step_id, step_number, '无', '无', '无', '无', step_action, expected_results, 'Block', '', '0000-00-00 00:00:00') logger.info('记录测试步骤到测试步骤报告表') testdb.execute_insert(sql_insert, data) elif step_action.find('步骤类型') != -1 and ( step_action.find('执行sql') != -1 or step_action.find('执行SQL') != -1): #: # 待执行步骤为sql data = (case_executed_history_id, self.testcase_id, self.testcase_name, testplan, self.testproject, step_id, step_number, '', '', testdb.get_host(), testdb.get_port(), step_action, expected_results, 'Block', '', '0000-00-00 00:00:00') logger.info('记录测试步骤到测试步骤报告表') testdb.execute_insert(sql_insert, data) elif step_action.find('步骤类型') == -1: # 待执行步骤为普通用例步骤 data = (case_executed_history_id, self.testcase_id, self.testcase_name, testplan, self.testproject, step_id, step_number, '', protocol, host, port, step_action, expected_results, 'Block', '', '0000-00-00 00:00:00') logger.info('记录测试步骤到测试步骤报告表') testdb.execute_insert(sql_insert, data) run_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # 记录运行时间 try: ifgot_protocol_method = 0 step_action = json.loads(step_action, object_pairs_hook=OrderedDict) if '步骤类型' not in step_action: protocol_method = step_action['方法'] else: protocol_method = '无' ifgot_protocol_method = 1 if expected_results != '': expected_results = json.loads(expected_results) step_obj = CaseStep(step_id, step_number, expected_results, step_action, self.testcase_id) except Exception as e: logger.error( '步骤[%s]信息填写错误: %s,停止执行用例[id=%s, name=%s]' % (step_number, e, self.testcase_id, self.testcase_name)) sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s' \ ' AND project=\'%s\' AND testplan=\'%s\'' if ifgot_protocol_method == 0: protocol_method = '未获取到' data = ('Block', '%s' % e, protocol_method, run_time, str(case_executed_history_id), self.testcase_id, step_id, self.testproject, testplan) logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update, data) return ('Error', [ ('TestCase', '步骤[%s]信息填写错误: %s,停止执行用例[id=%s, name=%s]' % (step_number, e, self.testcase_id, self.testcase_name)) ]) logger.info('开始执行步骤操作[第%s步]' % step_number) run_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # 记录运行时间 if '步骤类型' in step_action: if step_action['步骤类型'] == '执行用例': step_run_result = globalpkg.global_function.run_testcase_by_id( step_action['用例id'], None, testplan) else: step_run_result = step_obj.run_step() # 运行sql,或者是普通用例步骤 else: step_run_result = step_obj.run_step(http) # 运行普通用例步骤 try: # 转换 “ 为 ‘ ,防止数据库存储出错 action_of_step = step_obj.get_action() if type(action_of_step) == type(b''): action_of_step = action_of_step.decode('utf-8') elif type(action_of_step) == type({}): action_of_step = str(action_of_step) action_of_step = json.dumps(action_of_step, ensure_ascii=False) action_of_step = action_of_step.replace('"', "'") result_of_step = step_obj.get_expected_result() if type(result_of_step) == type(b''): result_of_step = result_of_step.decode('utf-8') elif type(result_of_step) == type({}): result_of_step = str(result_of_step) result_of_step = result_of_step.replace('"', "'") # except Exception as e: # logger.error('获取step action、expected_result出错 %s' % e) # action_of_step = '' # result_of_step = '' finally: if step_run_result[0] == 'Error': fail_or_error_reason = step_run_result[1][0][1] fail_or_error_reason = fail_or_error_reason.replace( '\n', '') fail_or_error_reason = fail_or_error_reason.replace( '\"', '') fail_or_error_reason = fail_or_error_reason.replace( '\'', '') logger.error( '步骤[%s]执行出错,停止执行用例[id=%s, name=%s]' % (step_number, self.testcase_id, self.testcase_name)) sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\",' \ 'step_action=\"%s\", expected_results=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s' \ ' AND project=\'%s\' AND testplan=\'%s\' AND runtime=\'%s\' ' data = (step_run_result[0], fail_or_error_reason, protocol_method, run_time, action_of_step, result_of_step, str(case_executed_history_id), self.testcase_id, step_id, self.testproject, testplan, '0000-00-00 00:00:00') logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update, data) return step_run_result elif step_run_result[0] == 'Fail': fail_or_error_reason = step_run_result[1][0][1] fail_or_error_reason = fail_or_error_reason.replace( '\n', '') fail_or_error_reason = fail_or_error_reason.replace( '\"', '') fail_or_error_reason = fail_or_error_reason.replace( '\'', '') logger.info( '步骤[%s]执行失败,停止执行用例[id=%s, name=%s]' % (step_number, self.testcase_id, self.testcase_name)) sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\",' \ 'step_action=\"%s\", expected_results=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s' \ ' AND project=\'%s\' AND testplan=\'%s\' AND runtime=\'%s\'' data = (step_run_result[0], fail_or_error_reason, protocol_method, run_time, action_of_step, result_of_step, str(case_executed_history_id), self.testcase_id, step_id, self.testproject, testplan, '0000-00-00 00:00:00') logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update, data) return step_run_result elif step_run_result[0] == 'Block': fail_or_error_reason = step_run_result[1] logger.info( '步骤[%s]执行失败,停止执行用例[id=%s, name=%s]' % (step_number, self.testcase_id, self.testcase_name)) sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s' \ ' AND project=\'%s\' AND testplan=\'%s\' AND runtime=\'%s\'' data = (step_run_result[0], fail_or_error_reason, '无', run_time, str(case_executed_history_id), self.testcase_id, step_id, self.testproject, testplan, '0000-00-00 00:00:00') logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update, data) return step_run_result else: fail_or_error_reason = '' sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\",' \ 'step_action=\"%s\", expected_results=\"%s\"'\ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s'\ ' AND project=\'%s\' AND testplan=\'%s\' AND runtime=\'%s\'' data = (step_run_result[0], fail_or_error_reason, protocol_method, run_time, action_of_step, result_of_step, str(case_executed_history_id), self.testcase_id, step_id, self.testproject, testplan, '0000-00-00 00:00:00') logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update, data) logger.info('测试用例[id=%s, name=%s]执行成功' % (self.testcase_id, self.testcase_name)) return ('Pass', '')
def run_testcase(self, http, testplan): if 0 == self.active_status: logger.warning('用例[name=%s]处于禁用状态[active=0],不执行' % self.testcase_name) return 'Block' protocol_all = http.get_protocol() host_all = http.get_host() port_all = http.get_port() for step in self.steps: # 构造测试步骤对象 step_id = int(step['id']) step_number = int(step['step_number']) step_action = other_tools.conver_date_from_testlink(step['actions']) expected_results = other_tools.conver_date_from_testlink(step['expected_results']) logger.debug('step_action: %s' % step_action) sql_insert = 'INSERT INTO '+ case_step_report_tb +'(executed_history_id, testcase_id, testcase_name, testplan, project, step_id, step_num, protocol_method, protocol, host, port, ' \ 'step_action, expected_results, runresult, reason, runtime)' \ ' VALUES' data = "('{}','{}','{}','{}','{}','{}','{}', '','{}','{}','{}','{}','{}', 'Block', '', '{}')".format(executed_history_id,self.testcase_id,self.testcase_name,testplan,self.testproject,step_id,step_number,protocol_all,host_all,port_all,step_action,expected_results,time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) logger.info('记录测试步骤到测试步骤报告表') sql = str(sql_insert) + str(data) testdb.execute_insert(sql) try: expected_results = json.loads(expected_results) step_action = json.loads(step_action) step_obj = CaseStep(step_id, step_number, expected_results, step_action, self.testcase_id) protocol_method = step_action['method'] except Exception as e: logger.error('步骤[%s]信息填写错误: %s,停止执行用例[id=%s, name=%s]' % (step_number, e, self.testcase_id, self.testcase_name)) return 'Error' logger.info('开始执行步骤操作[第%s步]'% step_number) run_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # 记录运行时间 step_run_result = step_obj.run_step(http) if step_run_result[0] == 'Error': fail_or_error_reason = step_run_result[1][0][1] fail_or_error_reason = fail_or_error_reason.replace('\n', '') fail_or_error_reason = fail_or_error_reason.replace('\"', '') fail_or_error_reason = fail_or_error_reason.replace('\'', '') logger.error('步骤[%s]执行出错,停止执行用例[id=%s, name=%s]' % (step_number, self.testcase_id, self.testcase_name)) sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s'\ ' AND project=\'%s\' AND testplan=\'%s\'' % \ (step_run_result[0], fail_or_error_reason, protocol_method, run_time, str(executed_history_id), self.testcase_id, step_id, self.testproject, testplan) logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update) return 'Error' elif step_run_result[0] == 'Fail': fail_or_error_reason = step_run_result[1][0][1] fail_or_error_reason = fail_or_error_reason.replace('\n', '') fail_or_error_reason = fail_or_error_reason.replace('\"', '') fail_or_error_reason = fail_or_error_reason.replace('\'', '') logger.info('步骤[%s]执行失败,停止执行用例[id=%s, name=%s]' % (step_number, self.testcase_id, self.testcase_name)) sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s'\ ' AND project=\'%s\' AND testplan=\'%s\'' % \ (step_run_result[0], fail_or_error_reason, protocol_method, run_time, str(executed_history_id), self.testcase_id, step_id, self.testproject, testplan) testdb.execute_update(sql_update) return 'Fail' else: fail_or_error_reason = '' sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s'\ ' AND project=\'%s\' AND testplan=\'%s\'' % \ (step_run_result[0], fail_or_error_reason, protocol_method, run_time, str(executed_history_id), self.testcase_id, step_id, self.testproject, testplan) logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update) logger.info('测试用例[id=%s, name=%s]执行成功' % (self.testcase_id, self.testcase_name)) return 'Pass'
def run_testcase(self, http, testplan): if 0 == self.active_status: logger.warning('用例[name=%s]处于禁用状态[active=0],不执行' % self.testcase_name) return 'Block' protocol_all = http.get_protocol() host_all = http.get_host() port_all = http.get_port() for step in self.steps: # 构造测试步骤对象 step_id = int(step['id']) step_number = int(step['step_number']) step_action = other_tools.conver_date_from_testlink(step['actions']) expected_results = other_tools.conver_date_from_testlink(step['expected_results']) logger.debug('step_action: %s' % step_action) sql_insert = 'INSERT INTO '+ case_step_report_tb +'(executed_history_id, testcase_id, testcase_name, testplan, project, step_id, step_num, protocol_method, protocol, host, port, ' \ 'step_action, expected_results, runresult, reason, runtime)' \ ' VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)' if step_action.find('style') != -1: data = (executed_history_id, self.testcase_id, self.testcase_name, testplan, self.testproject, step_id, step_number, '无', '无', '无', '无', step_action, expected_results, 'Block', '', '') else: data = (executed_history_id, self.testcase_id, self.testcase_name, testplan, self.testproject, step_id, step_number, '', protocol_all, host_all, port_all, step_action, expected_results, 'Block', '', '') logger.info('记录测试步骤到测试步骤报告表') testdb.execute_insert(sql_insert, data) try: step_action = json.loads(step_action) if 'style' not in step_action: protocol_method = step_action['method'] else: protocol_method = '无' if expected_results != '': expected_results = json.loads(expected_results) step_obj = CaseStep(step_id, step_number, expected_results, step_action, self.testcase_id) except Exception as e: logger.error('步骤[%s]信息填写错误: %s,停止执行用例[id=%s, name=%s]' % (step_number, e, self.testcase_id, self.testcase_name)) return 'Error' logger.info('开始执行步骤操作[第%s步]'% step_number) run_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # 记录运行时间 if 'style' in step_action: step_run_result = globalpkg.global_function.run_testcase_by_id(step_action['testcase_id'], testplan) else: step_run_result = step_obj.run_step(http) try:# 转换 “ 为 ‘ ,防止数据库存储出错 action_of_step = step_obj.get_action() if type(action_of_step) == type(b''): action_of_step = action_of_step.decode('utf-8') elif type(action_of_step) == type({}): action_of_step = str(action_of_step) action_of_step = action_of_step.replace('"', "'") result_of_step = step_obj.get_expected_result() if type(result_of_step) == type(b''): result_of_step = result_of_step.decode('utf-8') elif type(result_of_step) == type({}): result_of_step = str(result_of_step) result_of_step = result_of_step.replace('"', "'") # except Exception as e: # logger.error('获取step action、expected_result出错 %s' % e) # action_of_step = '' # result_of_step = '' finally: if step_run_result[0] == 'Error': fail_or_error_reason = step_run_result[1][0][1] fail_or_error_reason = fail_or_error_reason.replace('\n', '') fail_or_error_reason = fail_or_error_reason.replace('\"', '') fail_or_error_reason = fail_or_error_reason.replace('\'', '') logger.error('步骤[%s]执行出错,停止执行用例[id=%s, name=%s]' % (step_number, self.testcase_id, self.testcase_name)) sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\",' \ 'step_action=\"%s\", expected_results=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s' \ ' AND project=\'%s\' AND testplan=\'%s\'' % \ (step_run_result[0], fail_or_error_reason, protocol_method, run_time, action_of_step, result_of_step, str(executed_history_id),self.testcase_id, step_id, self.testproject, testplan) logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update) return 'Error' elif step_run_result[0] == 'Fail': fail_or_error_reason = step_run_result[1][0][1] fail_or_error_reason = fail_or_error_reason.replace('\n', '') fail_or_error_reason = fail_or_error_reason.replace('\"', '') fail_or_error_reason = fail_or_error_reason.replace('\'', '') logger.info('步骤[%s]执行失败,停止执行用例[id=%s, name=%s]' % (step_number, self.testcase_id, self.testcase_name)) sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\",' \ 'step_action=\"%s\", expected_results=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s' \ ' AND project=\'%s\' AND testplan=\'%s\'' % \ (step_run_result[0], fail_or_error_reason, protocol_method, run_time, action_of_step, result_of_step, str(executed_history_id),self.testcase_id, step_id, self.testproject, testplan) logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update) return 'Fail' elif step_run_result[0] == 'Block': fail_or_error_reason = step_run_result[1] logger.info('步骤[%s]执行失败,停止执行用例[id=%s, name=%s]' % (step_number, self.testcase_id, self.testcase_name)) sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s' \ ' AND project=\'%s\' AND testplan=\'%s\'' % \ (step_run_result[0], fail_or_error_reason, '无', run_time, str(executed_history_id), self.testcase_id, step_id, self.testproject, testplan) logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update) return 'Fail' else: fail_or_error_reason = '' # sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\"' \ # ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s'\ # ' AND project=\'%s\' AND testplan=\'%s\'' % \ # (step_run_result[0], fail_or_error_reason, protocol_method, run_time, str(executed_history_id), self.testcase_id, step_id, # self.testproject, testplan) sql_update = 'UPDATE '+ case_step_report_tb +' SET runresult=\"%s\",reason=\"%s\", protocol_method=\"%s\", runtime=\"%s\",' \ 'step_action=\"%s\", expected_results=\"%s\"'\ ' WHERE executed_history_id = %s AND testcase_id = %s AND step_id = %s'\ ' AND project=\'%s\' AND testplan=\'%s\'' % \ (step_run_result[0], fail_or_error_reason, protocol_method, run_time, action_of_step, result_of_step, str(executed_history_id),self.testcase_id, step_id, self.testproject, testplan) logger.info('正在更新步骤执行结果') testdb.execute_update(sql_update) logger.info('测试用例[id=%s, name=%s]执行成功' % (self.testcase_id, self.testcase_name)) return 'Pass'
def handle_charref(self, name): if name.startswitch('x'): c = chr(int(name[1:], 16)) else: c = chr(int(name)) logger.debug('Num ent:%s' % c)
def handle_entityref(self, name): c = chr(name2codepoint[name]) logger.debug('Named ent:%s' % c)
def handle_comment(self, data): pass logger.debug('Comment:%s' % data)
def handle_data(self, data): logger.debug('Data %s' % data) tmp_list = [self.start_tag, data] self.starttag_data.append(tmp_list)
def handle_endtag(self, tag): pass logger.debug('End tag: %s' % tag)
def handle_starttag(self, tag, attrs): logger.debug('Start tag: %s' % tag) tmp_list = [tag, attrs] self.start_tag = tag self.starttag_arrts.append(tmp_list)