introduction
Earlier I shared an article on interface keyword encapsulation , which mainly encapsulates the constant acquisition method of interface test data in excel, which is the first line field of excel, called the keyword. Now that you have obtained the keywords for the interface test, how do you get the values corresponding to the keywords? For example, in the method we encapsulated before, the interface keyword url can be obtained, but Excel has multiple interface data, and each interface data corresponds to a different url value. We can't get the url of each interface data line by line, so all methods are encapsulated in a class, and what keyword data is needed, we can call methods from the class to get it.
Encapsulation
Knowing what to do, the following is the implementation, the code is as follows:
__author__ = 'Leo' from public.operate_excel import Operate_Excel from basic_method import testcases_keyword class getData(object): def __init__(self): self.op_excel = Operate_Excel() def get_case_nums(self): """获取测试用例条数""" return self.op_excel.get_sheet_nrows() def get_is_header(self,row): """是否携带请求头""" col = int(testcases_keyword.get_case_header()) header = self.op_excel.get_sheet_cell(row,col) if header is not None: return header else: print("没有header!") return None def get_is_run(self,row): """是否运行""" col = int(testcases_keyword.get_case_is_execute()) is_run = self.op_excel.get_sheet_cell(row,col) if is_run == 'yes': flag = True else: flag = False return flag def get_url(self,row): """获取url""" col = int(testcases_keyword.get_case_interface_url()) url = self.op_excel.get_sheet_cell(row,col) return url def get_method(self,row): """获取请求方法""" col = int(testcases_keyword.get_case_method()) method = self.op_excel.get_sheet_cell(row,col) return method def get_data(self,row): """获取请求数据""" col = int(testcases_keyword.get_case_payload()) data = self.op_excel.get_sheet_cell(row,col) return data def get_excepted_result(self,row): """获取预期结果""" col = int(testcases_keyword.get_case_expected_result()) excepted_result = self.op_excel.get_sheet_cell(row,col) if excepted_result == '': return None else: return excepted_result def get_actual_result(self,row,value): """获取实际结果""" col = int(testcases_keyword.get_case_actual_result()) actual_result = self.op_excel.get_sheet_cell(row,col) self.op_excel.write_to_excel(row,col,value) if __name__ == '__main__': get_data = getData() print(get_data.get_is_run(1)) print(get_data.get_url(1))
operation result:
Comparative Results:
to sum up
The above encapsulated method basically meets the requirements of the project interface test, of course, there will be more special cases, which will be added according to the actual project. If you are interested in automated testing and test development, you can join the test open communication group QQ: 696400122. To be a thoughtful farmer, don't miss the past and fear the future!