def test_get_address_info(self): '''演示接口使用host,port,协议和统一配置不一致的时的处理''' #global step_output myhttp = MyHttp('http', 'ip.ws.126.net', '80') logger.info('正在发起GET请求...') response = myhttp.get(self.url) sql_update = 'UPDATE '+ case_step_report_tb +' SET protocol=\"%s\",host=\"%s\", port=\"%s\"' \ 'WHERE executed_history_id = %s and testcase_id = %s and step_id = %s' % \ ('http', 'ditu.amap.com', '80', executed_history_id, self.testcase_id, self.step_id) logger.info('正在更新步骤端口,主机,协议等配置信息: %s' % sql_update) testdb.execute_update(sql_update) logger.info('正在解析返回结果') response = response.decode('gbk') # decode函数对获取的字节数据进行解码 print(response) IPQuery.step_output = response # 保存返回结果供其它接口使用 response = response.split('localAddress=')[1] response = response.split(',')[0] response = response.split(':')[1] response = response.replace('\"', '') self.assertEqual(response, self.expected_result['city_name'], msg='city不为深圳市')
def run_testsuite(self, http): logger.info('正在获取套件[id=%s,name=%s]的测试用例...' % (self.testsuite_id, self.testsuite_name)) testcases_id_list_for_testsuit = self.get_testcases_id_for_testsuite() for testcase_id in testcases_id_list_for_testsuit[:]: testcase_info = mytestlink.getTestCase(testcase_id) # 获取测试用例基本信息 # 构造测试用例对象 testcase_name = testcase_info[0]['name'] testcase_steps = testcase_info[0]['steps'] testcase_isactive = int(testcase_info[0]['active']) testcase_obj = TestCase(testcase_id, testcase_name, testcase_steps, testcase_isactive, self.project_name) sql_insert = 'INSERT INTO '+testcase_report_tb +'(executed_history_id, testcase_id, testcase_name, testsuit, testplan, project, runresult, runtime)' \ ' VALUES' testplan = '无计划' data = "('{}','{}','{}','{}','{}','{}', 'Block','{}')".format(executed_history_id,testcase_id,testcase_name,self.testsuite_name,testplan,self.project_name,time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) logger.info('记录测试用例到到测试用例报表') sql = str(sql_insert) + str(data) testdb.execute_insert(sql) logger.info('开始执行测试用例[id=%s,name=%s]' % (testcase_id, testcase_name)) run_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # 记录运行时间 testcase_run_result = testcase_obj.run_testcase(http, testplan) logger.info('正在更新用例执行结果') sql_update = 'UPDATE '+testcase_report_tb +' SET runresult=\"%s\", runtime=\"%s\"' \ ' WHERE executed_history_id = %s and testcase_id = %s' \ ' AND project=\'%s\' AND testplan=\'%s\'' % \ (testcase_run_result, run_time, executed_history_id, testcase_id, self.project_name, testplan) testdb.execute_update(sql_update) logger.info('测试套件[id=%s ,name=%s]已执行完' % (self.testsuite_id, self.testsuite_name))
def run_testplan(self, http): logger.info('正在获取测试计划[project=%s,name=%s]的测试用例' % (self.testproject, self.testplan_name)) testcases_for_testplan = mytestlink.getTestCasesForTestPlan(self.testplan_id) if [] == testcases_for_testplan: logger.warning('未获取到测试用例') return testcases_id_for_testplan = testcases_for_testplan.keys() logger.info('成功获取测试计划[project=%s,name=%s]的测试用例id:%s' % (self.testproject, self.testplan_name, testcases_id_for_testplan)) if 0 == self.active_status: logger.warning('测试计划[project=%s,name=%s]处于不活动状态[active=0],不执行' % (self.testproject, self.testplan_name)) return for testcase_id_str in testcases_id_for_testplan: testcase_id = int(testcase_id_str) testcase_info = mytestlink.getTestCase(testcase_id) # 获取测试用例基本信息 # 构造测试用例对象 testcase_name = testcase_info[0]['name'] testcase_steps = testcase_info[0]['steps'] testcase_isactive = int(testcase_info[0]['active']) testsuite_id = int(testcase_info[0]['testsuite_id']) testsuite_info = mytestlink.getTestSuiteByID(testsuite_id) testsuite_name = testsuite_info['name'] testcase_obj = TestCase(testcase_id, testcase_name, testcase_steps, testcase_isactive, self.testproject) sql_insert = 'INSERT INTO '+testcase_report_tb +'(executed_history_id, testcase_id, testcase_name, testsuit, testplan, project, runresult, runtime)' \ ' VALUES(%s, %s, %s, %s, %s, %s, %s, %s)' data = (executed_history_id, testcase_id, str(testcase_name), testsuite_name, self.testplan_name, self.testproject, 'Block', '') logger.info('记录测试用例到测试用例报表') testdb.execute_insert(sql_insert, data) logger.info('开始执行测试用例[id=%s,name=%s]' % (testcase_id, testcase_name)) run_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # 记录运行时间 testcase_run_result = testcase_obj.run_testcase(http, self.testplan_name) logger.info('正在更新用例执行结果') sql_update = 'UPDATE '+testcase_report_tb +' SET runresult=\"%s\", runtime=\"%s\"' \ ' WHERE executed_history_id = %s AND testcase_id = %s' \ ' AND project=\'%s\' AND testplan=\'%s\'' % \ (testcase_run_result, run_time, executed_history_id, testcase_id, self.testproject, self.testplan_name) testdb.execute_update(sql_update) bulid_info = mytestlink.getLatestBuildForTestPlan(self.testplan_id) # 固定取最新的版本 bulid_version = bulid_info['name'] logger.info('正在更新testlink上测试计划[testplan_id=%s, bulid_version=%s],对应用例[testcase_id=%s]的执行结果' % (self.testplan_id, bulid_version, testcase_id)) notes = '用例[id:%s]在测试计划[testplan_id:%s,testplan_name:%s,bulidversion:%s]中的执行结果' \ % (testcase_id, self.testplan_id,self.testplan_name, bulid_version) if 'Fail' == testcase_run_result or 'Error' == testcase_run_result: self.__execute_case_in_testlink(testcase_id, bulid_version, 'f', notes) # f - 失败 elif 'Pass' == testcase_run_result: self.__execute_case_in_testlink(testcase_id, bulid_version, 'p', notes) # f - 成功 else: pass # 如果未执行,啥都不做,str(self.testplan_id) logger.info('测试计划[project=%s ,testplan=%s]已执行完' % (self.testproject, self.testplan_name))
def run_testsuite(self, http): logger.info('正在获取套件[id=%s,name=%s]的测试用例...' % (self.testsuite_id, self.testsuite_name)) testcases_id_list_for_testsuit = self.get_testcases_id_for_testsuite() for testcase_id in testcases_id_list_for_testsuit[:]: testcase_info = mytestlink.getTestCase(testcase_id) # 获取测试用例基本信息 #debug by david logger.info("测试用例信息%s" % testcase_info) # 构造测试用例对象 testcase_name = testcase_info[0]['name'] testcase_steps = testcase_info[0]['steps'] #testcase_steps = testcase_steps[0]["actions"] testcase_isactive = int(testcase_info[0]['active']) #调试用by david #logger.info('输出用例信息 =====step===david========%s'%testcase_info) #处理测试用例的步骤 #other_tools = OtherTools() #testcase_steps = other_tools.conver_date_from_testlink(testcase_steps) #示例testsuite_details = other_tools.conver_date_from_testlink(testsuite_info['details']) #logger.info('输出用例信息 ===处理后=====david========%s' % testcase_steps) testcase_obj = TestCase(testcase_id, testcase_name, testcase_steps, testcase_isactive, self.project_name) sql_insert = 'INSERT INTO '+testcase_report_tb +'(executed_history_id, testcase_id, testcase_name, testsuit, testplan, project, runresult, runtime)' \ ' VALUES(%s, %s, %s, %s, %s, %s, %s, %s)' testplan = '无计划' #MODIFY BY DAVID BLOCK TO PASS data = (executed_history_id, testcase_id, testcase_name, self.testsuite_name, testplan, self.project_name, 'PASS', '') logger.info('记录测试用例到到测试用例报表') testdb.execute_insert(sql_insert, data) logger.info('开始执行测试用例[id=%s,name=%s]' % (testcase_id, testcase_name)) run_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) # 记录运行时间 testcase_run_result = testcase_obj.run_testcase(http, testplan) logger.info('正在更新用例执行结果') sql_update = 'UPDATE '+testcase_report_tb +' SET runresult=\"%s\", runtime=\"%s\"' \ ' WHERE executed_history_id = %s and testcase_id = %s' \ ' AND project=\'%s\' AND testplan=\'%s\'' % \ (testcase_run_result, run_time, executed_history_id, testcase_id, self.project_name, testplan) testdb.execute_update(sql_update) logger.info('测试套件[id=%s ,name=%s]已执行完' % (self.testsuite_id, self.testsuite_name))
def test_get_lng_and_lat(self): '''演示当某个测试步骤使用的接口所在host,port,和协议和当前套件、项目等统一配置信息不一致时的处理操作''' self.params = (self.params)[0] self.params = urllib.urlencode(self.params) myhttp = MyHttp('http', 'ditu.amap.com', '80') logger.info('正在发起GET请求...') response = myhttp.get(self.url, self.params) sql_update = 'UPDATE '+ case_step_report_tb +' SET protocol=\"%s\",host=\"%s\", port=\"%s\"' \ 'WHERE executed_history_id = %s and testcase_id = %s and step_id = %s' % \ ('http', 'ditu.amap.com', '80', executed_history_id, self.testcase_id, self.step_id) logger.info('正在更新步骤端口,主机,协议等配置信息: %s' % sql_update) testdb.execute_update(sql_update) logger.info('正在解析返回结果') json_response = json.loads(response.decode('utf-8')) self.assertEqual(json_response['code'], self.expected_result['code'], msg='code != 1, 获取用户经纬度失败')
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'