def __init__(self): self.net = NetworkRequest() self.check = Check() self.write = WriteReport() self.interface = Tool.read_json('interface') self.api = Tool.read_json('api') self.token = self.net.request_token()
def write_report(self, url, params, expect, actual, flag): open_file = Tool.read_excel("report") table = open_file.sheets()[0] n = table.nrows wb = copy(open_file) write_sheet = wb.get_sheet(0) params = json.dumps(params, ensure_ascii=False) # 默认不将中文编码 # print("写报告测试参数", params) expect = json.dumps(expect, ensure_ascii=False) expect = Tool.json_converted_str(expect) # print("写报告期望结果", expect) actual = json.dumps(actual, ensure_ascii=False) actual = Tool.json_converted_str(actual) # print("写报告实际结果", actual) style = xlwt.easyxf('align: wrap on') # 数据写入excel自动换行 write_sheet.col(2).width = (30 * 367) write_sheet.col(3).width = (30 * 367) write_sheet.write(n, 0, url) write_sheet.write(n, 1, params) write_sheet.write(n, 2, expect, style) write_sheet.write(n, 3, actual, style) # result_sheet.write(0, 1, txt1.decode('utf-8')) if flag == 1: write_sheet.write(n, 4, u"测试通过") print("测试通过") else: write_sheet.write(n, 4, u"测试失败") print("测试失败") wb.save(r"../test_report/report.xls")
def __init__(self): Tool.__init__(self) # * Initialize ZMQ context and open subscriber, publisher sockets self.logger.debug("ZMQ version: {}, PyZMQ version: {}".format(zmq.zmq_version(), zmq.pyzmq_version())) # ** Context self.zmqContext = zmq.Context() # ** Subscriber self.subSocket = self.zmqContext.socket(zmq.SUB) self.subSocket.connect(self.sub_address) time.sleep(0.005) # mandatory sleep for ZMQ backend self.logger.debug("[sub] Connected to {}".format(self.sub_address)) # ** Subscriber topics for input messages self.subSocket.setsockopt(zmq.SUBSCRIBE, self.sub_topic) self.subSocket.setsockopt(zmq.LINGER, self.sub_socket_linger) self.logger.debug("[sub]Subscribed to topic \"{}\"".format(self.sub_topic)) time.sleep(0.005) # mandatory sleep for ZMQ backend # ** Publisher self.pubSocket = self.zmqContext.socket(zmq.PUB) self.pubSocket.bind(self.pub_address) time.sleep(0.005) # mandatory sleep for ZMQ backend self.logger.debug("[pub] Bound to {}".format(self.pub_address)) # * Initialize other members self.valid = False self.buttons = [0, 0] # primary, secondary self.transform = hm.translation(hm.identity(), self.position_offset) #self.position = self.position_offset self.loop = True # TODO ensure this is properly shared across threads # * Start sensing loop self.senseThread = Thread(target=self.senseLoop) self.senseThread.daemon = True # to prevent indefinite wait on recv() self.senseThread.start() time.sleep(0.005) # sleep to allow child thread to run
def __init__(self): self.net = "" self.check = Check() self.write = WriteReport() self.interface = Tool.read_json('interface') self.api = Tool.read_json('api') self.token = ""
def request_token(self): try: result = self.req.get(self.baseUrl + '/login', headers=self.headers) page_info = result.text token = re.findall(r'name="csrf-token" content="(.*?)">', str(page_info))[0] # 正则表达式只能匹配str self.headers["X-CSRF-TOKEN"] = token return token except Exception as e: print(e) Tool.log(e, 'debug.log')
def run(self): for interface in self.interface['interfaces']: action_file = Tool.read_json(interface['addr']) actions = action_file['actions'] datas = action_file['data'] for action in actions: print(action)
def run(self): for interface in self.interface['interfaces']: action_file = Tool.read_json(interface['addr']) interface_name = interface['name'] actions = action_file['actions'] # 获取所有动作 alldata = action_file['data'] # 获取所有测试数据 for action_list in actions: self.net = NetworkRequest() self.token = self.net.request_token() for action in action_list["action"]: interface = action["api"] # 需要的接口 dataname = action["data"] # 需要的数据名字 url, method, param_type_datas = self.get_one_api( self.api, interface) # check, param, expect = self.get_one_data(dataname, alldata) check, test_datas = self.get_test_datas(dataname, alldata) for test_data in test_datas: print(test_data) interface_description = test_data[ "Interface_description"] print(interface_description) param = test_data["input"] expect = test_data["output"] param = self.data_processing(self.token, param_type_datas, param) self.request_method(url, interface_name, interface_description, method, check, param, expect)
def check_options(self): """Call tool's check_options(). Each tool knows its own way to check if options are right. They Verify: Expected Options, Option's type, Path existence """ # Particular treatment for general options. Tool.check_options(self.__all_options['General']) # The loop will check options for each tool's name in the # Constant list. for tool in Constant.TOOLS_LIST: Logger().debug(tool) object_name = tool.rstrip('/ ').split('/')[-1] # The checking is done with static methods and variables. available_options = eval(object_name).OPTIONS_PROPERTIES # 'tool' is the path to the OptionList for the tool. eval(object_name).check_options(self.__all_options[tool], available_options)
def data_processing(self, token, types, param): for item in types: name = item['name'] type = item['type'] must = item['must'] if type == 1: pass elif type == 2: pass elif type == 3: param[name] = token elif type == 4: param[name] = Tool.hash_password(param[name], token) return param
def __init__(self): Tool.__init__(self) # * Initialize ZMQ context and open subscriber, publisher sockets self.logger.debug("ZMQ version: {}, PyZMQ version: {}".format( zmq.zmq_version(), zmq.pyzmq_version())) # ** Context self.zmqContext = zmq.Context() # ** Subscriber self.subSocket = self.zmqContext.socket(zmq.SUB) self.subSocket.connect(self.sub_address) time.sleep(0.005) # mandatory sleep for ZMQ backend self.logger.debug("[sub] Connected to {}".format(self.sub_address)) # ** Subscriber topics for input messages self.subSocket.setsockopt(zmq.SUBSCRIBE, self.sub_topic) self.subSocket.setsockopt(zmq.LINGER, self.sub_socket_linger) self.logger.debug("[sub]Subscribed to topic \"{}\"".format( self.sub_topic)) time.sleep(0.005) # mandatory sleep for ZMQ backend # ** Publisher self.pubSocket = self.zmqContext.socket(zmq.PUB) self.pubSocket.bind(self.pub_address) time.sleep(0.005) # mandatory sleep for ZMQ backend self.logger.debug("[pub] Bound to {}".format(self.pub_address)) # * Initialize other members self.valid = False self.buttons = [0, 0] # primary, secondary self.transform = hm.translation(hm.identity(), self.position_offset) #self.position = self.position_offset self.loop = True # TODO ensure this is properly shared across threads # * Start sensing loop self.senseThread = Thread(target=self.senseLoop) self.senseThread.daemon = True # to prevent indefinite wait on recv() self.senseThread.start() time.sleep(0.005) # sleep to allow child thread to run
def run(self): for interface in self.interface['interfaces']: action_file = Tool.read_json(interface['addr']) actions = action_file['actions'] # 获取所有动作 alldata = action_file['data'] # 获取所有测试数据 for action in actions: interface = action["api"] # 需要的接口 dataname = action["data"] # 需要的数据名字 url, method, datas = self.get_one_api(self.api, interface) check, param, expect = self.get_one_data(dataname, alldata) param = self.data_processing(self.token, datas, param) self.request_method(url, method, check, param, expect)
def __init__(self): self.r = NetworkRequest() self.interface = Tool.read_json('interface') self.api = Tool.read_json('api') self.token = self.r.request_token()
def run(self): # 读json文件,取测试数据 api_file = Tool.read_json("interface") test_data_file = Tool.read_json("test_data") expect_data_file = Tool.read_json("expect_data") # 获取接口的具体信息,循环读取某一接口 api_list = api_file["api_list"] for api in api_list: print(api) url = api["url"] method = api["method"] need_login = api["need_login"]["type"] login_username = api["need_login"]["username"] login_password = api["need_login"]["password"] test_datas = api["test_data"] expect_data_addr = api["expect_data"] expect_datas = expect_data_file[expect_data_addr] params = test_data_file[test_datas] for index in range(len(params)): # print("测试和期望",params[index],expect_datas[index]) param = params[index] expect_data = expect_datas[index] print("期望结果", expect_data) if '_token' in param.keys(): param["_token"] = self.token print("测试数据", param) if need_login == 0: print("执行不需要登录的") if method == "get": result = self.net.get(url, param) result_dict = json.loads(result.content) print("实际结果", result_dict) # print("开始比较实际结果和期望值") flag = self.check.comparison_result( expect_data, result_dict) self.write.write_report(url, param, expect_data, result_dict, flag) elif method == "post": result = self.net.post(url, param) result_dict = json.loads(result.content) print("实际结果", result_dict) # print("开始比较实际结果和期望值") flag = self.check.comparison_result( expect_data, result_dict) self.write.write_report(url, param, expect_data, result_dict, flag) elif method == "delete": # print("请求的params", param) # print("执行需要登录的post") result = self.net.post(url, param) result_dict = json.loads(result.content) print("实际结果", result_dict) # print("开始比较实际结果和期望值") flag = self.check.comparison_result( expect_data, result_dict) self.write.write_report(url, param, expect_data, result_dict, flag) elif method == "put": # print("请求的params", param) # print("执行需要登录的post") result = self.net.post(url, param) result_dict = json.loads(result.content) print("实际结果", result_dict) # print("开始比较实际结果和期望值") flag = self.check.comparison_result( expect_data, result_dict) self.write.write_report(url, param, expect_data, result_dict, flag) else: print("执行登录") parms = {} hash_password = Tool.hash_password(login_password, self.token) parms["user_name"] = login_username parms["_token"] = self.token parms["password"] = hash_password r_login = self.net.post("/auth/login", parms) print("登录结果:", r_login.text) if method == "get": result = self.net.get(url, param) result_dict = json.loads(result.content) print("实际结果", result_dict) # print("开始比较实际结果和期望值") flag = self.check.comparison_result( expect_data, result_dict) self.write.write_report(url, param, expect_data, result_dict, flag) elif method == "post": # print("请求的params", param) # print("执行需要登录的post") result = self.net.post(url, param) result_dict = json.loads(result.content) print("实际结果", result_dict) # print("开始比较实际结果和期望值") flag = self.check.comparison_result( expect_data, result_dict) self.write.write_report(url, param, expect_data, result_dict, flag) elif method == "delete": # print("请求的params", param) print("----执行需要登录的删除请求----") result = self.net.post(url, param) result_dict = json.loads(result.content) print("实际结果:", result_dict) # print("开始比较实际结果和期望值") flag = self.check.comparison_result( expect_data, result_dict) self.write.write_report(url, param, expect_data, result_dict, flag) elif method == "put": # print("请求的params", param) # print("执行需要登录的post") result = self.net.post(url, param) result_dict = json.loads(result.content) print("实际结果", result_dict) # print("开始比较实际结果和期望值") flag = self.check.comparison_result( expect_data, result_dict) self.write.write_report(url, param, expect_data, result_dict, flag) result = self.net.post("/auth/logout", {}) print('---------------')
def write_report(self,url, interface_name,interface_description,params, expect, actual, flag): open_file = Tool.read_excel("report") table = open_file.sheets()[0] n = table.nrows wb = copy(open_file) write_sheet = wb.get_sheet(0) params = json.dumps(params, ensure_ascii=False) # 默认不将中文编码 expect = json.dumps(expect, ensure_ascii=False) expect = Tool.json_converted_str(expect) actual = json.dumps(actual, ensure_ascii=False) actual = Tool.json_converted_str(actual) style = xlwt.easyxf('align: wrap on') # 数据写入excel自动换行 write_sheet.col(2).width = (30 * 367) write_sheet.col(3).width = (30 * 367) write_sheet.write(n, 0, url) write_sheet.write(n, 1, interface_name) write_sheet.write(n, 2, interface_description) write_sheet.write(n, 3, params) write_sheet.write(n, 4, expect, style) write_sheet.write(n, 5, actual, style) Tool.log('URL:' + str(url), 'test') Tool.log('接口名字:' + str(interface_name), 'test') Tool.log('期望结果:'+str(expect),'test') Tool.log('实际结果:' + str(actual), 'test') if flag == 1: write_sheet.write(n, 6, u"测试通过") Tool.log("测试通过",'test') print("测试通过") else: write_sheet.write(n, 6, u"测试失败") Tool.log("测试失败", 'test') print("测试失败") wb.save(r"../test_report/report.xls")