def test_books_003(self): book_id = read_id_from_file() # print(self.excel.get_url(row=3).replace('{bookID}', book_id)) res = self.rqst.get(url=self.excel.get_url( row=3).replace('{bookID}', book_id)) # print(res.json()) self.assert_result(res, row=3)
def test_book_004(self): # print(self.excel.get_url(row=4).replace('{bookID}', read_id_from_file())) # print(self.excel.get_param(row=4)) res = self.rqst.put(url=self.excel.get_url(row=4).replace( '{bookID}', read_id_from_file()), json=self.excel.get_param(row=4)) # print(res.json()) self.assert_result(res, row=4)
def test_books(self): for i in range(1, self.excel.get_rows): if self.excel.get_method(i) == 'get': if '{bookID}' in self.excel.get_url(i): res = self.rqst.get(url=self.excel.get_url(i).replace( '{bookID}', read_id_from_file())) else: res = self.rqst.get(self.excel.get_url(i)) elif self.excel.get_method(i) == 'post': res = self.rqst.post(url=self.excel.get_url(i), json=self.excel.get_param(i)) write_id_to_file(res.json()[0]['datas']['id']) elif self.excel.get_method(i) == 'put': res = self.rqst.put(url=self.excel.get_url(i).replace( '{bookID}', read_id_from_file()), json=self.excel.get_param(i)) else: res = self.rqst.delete(url=self.excel.get_url(i).replace( '{bookID}', read_id_from_file())) print() print(res.json()) print() self.assert_result(res, i)
def test_login_book(data): # 1. 判断case存不存在前置条件,如果存在,则先执行前置条件 pre_case = excel.exist_pre( data[ExcelColumNameAll.pre_condition].strip()) # pre_case是一个字典类型 if pre_case: # 在此处,我们知道所有的case最多只有一个前置条件,且只有一个是login, # 所以这里直接使用post,如果不确定就要再封装一层 res = rqst.post(url=pre_case[ExcelColumNameAll.rqst_url], json=get_dict(pre_case, ExcelColumNameAll.rqst_param)) access_token = res.json()['access_token'] # print(access_token) if len(data[ExcelColumNameAll.rqst_header].strip()) == 0: headers = None else: headers = json.loads(data[ExcelColumNameAll.rqst_header].replace( '{token}', access_token)) # 判断是获取全部书籍信息,还是获取某一本书籍的信息 book_id = None if '{bookID}' in data[ExcelColumNameAll.rqst_url]: book_id = read_id_from_file() # 2. 根据method不同,使用不同的请求方法发送请求 if data[ExcelColumNameAll.rqst_method] == 'get': res = rqst.get(url=get_url(data, book_id), headers=headers) elif data[ExcelColumNameAll.rqst_method] == 'post': res = rqst.post(url=data[ExcelColumNameAll.rqst_url], json=get_dict(data, ExcelColumNameAll.rqst_param), headers=headers) # 添加书籍成功后,需要存储书籍的bookID,后面的case可能会用到 if 'id' in res.text: write_id_to_file(res.json()[0]['datas']['id']) elif data[ExcelColumNameAll.rqst_method] == 'put': res = rqst.put(url=get_url(data, book_id), json=get_dict(data, ExcelColumNameAll.rqst_param), headers=headers) elif data[ExcelColumNameAll.rqst_method] == 'delete': res = rqst.delete(url=get_url(data, book_id), headers=headers) # print(res.json()) # 3. 对返回信息进行断言 assert_result(res, data)
def test_book_005(self): res = self.rqst.delete(url=self.excel.get_url( row=5).replace('{bookID}', read_id_from_file())) # print(res.json()) self.assert_result(res, row=5)