python 接口自动化测试 requests excel_Requests+Excel接口自动化测试(Python)

#coding:utf-8

importsysimportjson

sys.path.append('C:/Users/lxz/Desktop/InterFace_JIA')from tool.operation_excel importOperationExcelfrom base.runmethod importRunMethodfrom operation_data.get_data importGetDatafrom jsonpath_rw importjsonpath, parseclassDependdentData:def __init__(self, case_id):

self.case_id=case_id

self.opera_excel=OperationExcel()

self.data=GetData()#通过case_id去获取该case_id的整行数据

defget_case_line_data(self):

rows_data=self.opera_excel.get_rows_data(self.case_id)returnrows_data#执行依赖测试,获取结果

defrun_dependent(self):

run_method=RunMethod()

row_num=self.opera_excel.get_row_num(self.case_id)

request_data=self.data.get_data_for_json(row_num)#header = self.data.is_header(row_num)

method =self.data.get_request_method(row_num)

url=self.data.get_request_url(row_num)

res=run_method.run_main(method, url, request_data)returnjson.loads(res)#根据依赖的key去获取执行依赖测试case的响应,然后返回

defget_data_for_key(self, row):

depend_data=self.data.get_depend_key(row)

response_data=self.run_dependent()

json_exe=parse(depend_data)

madle=json_exe.find(response_data)return [math.value for math inmadle][0]if __name__ == '__main__':

order={"data": {"_input_charset": "utf-8","body": "京东订单-1710141907182334","it_b_pay": "1d","notify_url": "http://order.imooc.com/pay/notifyalipay","out_trade_no": "1710141907182334","partner": "2088002966755334","payment_type": "1","seller_id": "yangyan01@tcl.com","service": "mobile.securitypay.pay","sign": "kZBV53KuiUf5HIrVLBCcBpWDg%2FnzO%2BtyEnBqgVYwwBtDU66Xk8VQUTbVOqDjrNymCupkVhlI%2BkFZq1jOr8C554KsZ7Gk7orC9dDbQlpr % 2BaMmdjO30JBgjqjj4mmM % 2Flphy9Xwr0Xrv46uSkDKdlQqLDdGAOP7YwOM2dSLyUQX % 2Bo4 % 3D","sign_type": "RSA","string": "_input_charset=utf-8&body=京东订单-1710141907182334&it_b_pay=1d&notify_url=http://order.imooc.com/pay/notifyalipay & out_trade_no = 1710141907182334 & partner = 2088002966755334 & payment_type = 1 & seller_id = yangyan01 @tcl.com & service = mobile.securitypay.pay & subject = 京东订单 - 1710141907182334 & total_fee = 299 & sign = kZBV53KuiUf5HIrVLBCcBpWDg % 2FnzO % 2BtyEnBqgVYwwBtDU66Xk8VQUTbVOqDjrNymCupkVhlI % 2BkFZq1jOr8C554KsZ7Gk7orC9dDbQlpr % 2BaMmdjO30JBgjqjj4mmM % 2Flphy9Xwr0Xrv46uSkDKdlQqLDdGAOP7YwOM2dSLyUQX % 2Bo4 % 3D &sign_type = RSA","subject": "京东订单-1710141907182334","total_fee": 299},"errorCode": 1000,"errorDesc": "成功","status": 1,"timestamp": 1507979239100}

res= "data.out_trade_no"json_exe=parse(res)

madle=json_exe.find(order)print[math.value for math in madle][0]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python是一种广泛使用的编程语言,因其易学易用、灵活性和可扩展性而备受欢迎。requestsPython的一个库,它提供了一种简单且易于使用的方式来发送HTTP请求。pytest是Python的另一个库,它提供了一种用于编写和运行测试的框架。allure是一个测试报告生成工具,可以为测试结果提供美观和易读的报告。 在接口自动化测试中,可以使用Pythonrequests库来发送HTTP请求,并使用pytest框架来编写和运行测试。可以使用Excel来存储测试数据、预期结果和实际结果。使用allure工具可以生成美观的测试报告。 以下是使用Python requests、pytest和allure进行接口自动化测试的一般步骤: 1. 安装Pythonrequests、pytest和allure 2. 创建一个Excel文件,输入测试数据、预期结果和实际结果 3. 创建一个pytest测试文件,并使用requests库发送HTTP请求,比较预期结果和实际结果 4. 在pytest测试文件中添加allure装饰器,以便生成测试报告 5. 运行pytest测试文件并生成allure测试报告 例如,以下是一个使用Python requests、pytest和allure进行接口自动化测试的示例代码: ```python import requests import pytest import allure import openpyxl @allure.title("测试接口") @pytest.mark.parametrize("test_input, expected", [(1, "success"), (2, "fail")]) def test_api(test_input, expected): # 从Excel文件中获取测试数据和预期结果 wb = openpyxl.load_workbook("testdata.xlsx") sheet = wb.active test_data = sheet.cell(row=test_input, column=1).value expected_result = sheet.cell(row=test_input, column=2).value # 发送HTTP请求 response = requests.get("http://example.com/api", params=test_data) actual_result = response.text # 比较预期结果和实际结果 assert actual_result == expected_result, f"预期结果:{expected_result},实际结果:{actual_result}" # 添加allure描述 allure.attach("请求参数", str(test_data)) allure.attach("预期结果", str(expected_result)) allure.attach("实际结果", str(actual_result)) ``` 在上面的代码中,使用了pytest的parametrize装饰器来传递测试数据和预期结果。使用openpyxl库从Excel文件中获取测试数据和预期结果。使用requests库发送HTTP请求并比较预期结果和实际结果。使用allure的装饰器来添加测试描述。最后,运行pytest测试文件并生成allure测试报告。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值