##Python接口测试
环境部署
安装python
- 进入官网 https://www.python.org/下载你想用的版本(本文介绍的版本是3.7的版本)。
- 安装
- 添加系统环境变量:在Path中新建“D:\Python37”(
D:\Python37为python安装目录
) - 验证 python 是否安装成功,在cmd中输入python
安装 requests库(接口测试时需要导入这个库
)
- 进入 Scripts 目录“cd Python37\Scripts”
- 输入 pip install requests
接口测试
执行测试的大致流程如下:
- 拿到接口的url地址
- 查看接口是用什么方式发送
- 添加请求头,请求体
- 发送查看返回结果,校验返回结果是否
实际代码如下(例子中使用的是get请求):
# 导包
import requests
# 给接口地址定义变量名称
url = "http://v.juhe.cn/weather/index"
para = {"cityname":"北京","key":"221ec2c9d854d2859310ea808cb760f"}
r = requests.get(url,params=para)
# print(r.status_code)
print(r.text)
# 获取json数据
res = r.json()
print(res)
输出结果
"D:\Program Files (x86)\python38\python.exe" E:/python/day1/第一个接口测试.py
{'resultcode': '101', 'reason': '错误的请求KEY', 'result': None, 'error_code': 10001}
Process finished with exit code 0