python接口测试代码
普通接口测试代码 -data表单数据
✔传表单格式
✔不会自动加Content-Type: application/json
✔字典会被转成key=value&key=value
import requests # 1. 登录 resp = requests.request( method='post', url="http://api.fbi.com:9225/rest-v2/login/access_token", data={ "email": "bf@qq.com", "password": "bf123456" } ) print(resp.status_code) # 人工判断 if resp.status_code == 200: # 自动判断 print('成功') else: print('失败')普通接口测试代码 -json字符串数据
✔传 JSON 格式
✔自动加请求头:Content-Type: application/json
✔字典会被转成 JSON 字符串
import requests # 1. 登录 resp = requests.request( method='post', url="http://api.fbi.com:9225/rest-v2/login/access_token", json={ "email": "bf@qq.com", "password": "bf123456" } ) print(resp.status_code) # 人工判断 if resp.status_code == 200: # 自动判断 print('成功') else: print('失败')文件上传接口测试代码
import requests # 1. 登录 resp = requests.request( method='post', url="http://api.fbi.com:9225/rest-v2/login/access_token", files={ "email": open("北凡老师pytest高手课.mp4", "rb"), "password": open("北凡老师测试平台实战课.mp4", "rb"), } ) print(resp.status_code) # 人工判断 if resp.status_code == 200: # 自动判断 print('成功') else: print('失败')总结
ps: 拿apiFox为例,可查看对应的python代码
