帮助你学习API接口测试。
安装
克隆或下载项目,安装依赖。
$ pip install -r requirements.txt
- 启动接口服务
$ python api_server.py
* Serving Flask app "api_server.py" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 208-740-173
Flask Web框架可以非常简单的方式实现API,项目中的所有API都在api_server.py
文件中。
- 接口测试库
所有例子使用Requests 库调用,你也可以使用其他API测试工具,如Postman、JMeter等。
- URL (http://www.xxx.com/v1/login)
- 方法:GET/POST/PUT/DELETE
- Auth
- Header
- 参数类型(form-data/json..)
- 参数值(id=1,name=tom)
- 参数加密方式
- 返回值(错误码/提示信息/数据)
import requests
r = requests.get("http://127.0.0.1:5000/")
result = r.json()
print(result)
返回结果:
{"code": 10200, "message": "Welcome to API testing"}
import requests
name = "tom"
r = requests.get("http://127.0.0.1:5000/user/"+name)
result = r.json()
print(result)
返回结果:
{"code": 10200, "message": "hello, tom"}
import requests
uid = "1"
r = requests.get("http://127.0.0.1:5000/id/"+uid)
result = r.json()
print(result)
返回结果:
{"code": 10101, "message": "user id null"}
{"code": 10200, "data": {"age": 22, "id": 1, "name": "tom"}, "message": "success"}
- 方法一
import requests
payload = {'q': 'selenium'}
r = requests.get("http://127.0.0.1:5000/search/", params=payload)
result = r.json()
print(result)
- 方式二
import requests
r = requests.get("http://127.0.0.1:5000/search/?q=selenium")
result = r.json()
print(result)
返回结果:
{"code": 10200, "data": ["selenium教程", "seleniumhq.org", "selenium环境安装"], "message": "success"}
参数类型为:from-data
/x-www-from-urlencode
格式
import requests
payload = {'username': 'admin', "password": "a123456"}
r = requests.post("http://127.0.0.1:5000/login", data=payload)
result = r.json()
print(result)
返回结果:
{"code": 10102, "message": "username or passwrord is None"}
{"code": 10203, "message": "username or passwrord is null"}
{"code": 10104, "message": "username or password error"}
{"code": 10200, "message": "login success"}
参数类型为:JSON
格式
import requests
payload = {'name': 'jack', "age": 22, "height": 177}
r = requests.post("http://127.0.0.1:5000/add_user", json=payload)
result = r.json()
print(result)
返回结果:
{"code": 10102, "message": "key null"}
{"code": 10103, "message": "name null"}
{"code": 10104, "message": "name exist"}
{"code": 10105, "message": "format error"}
{"code": 10200, "message": "add success",
"data": {
"age": 22, "height": 177, "name": "jack"
},
}
import requests
headers = {"Content-Type": "application/json",
"token": "3d80caXELzU1aWmHwxl0TzW7jtterObm8l5EeAfipnhyaKmhFl8KdhFRvy4"}
r = requests.post("http://127.0.0.1:5000/header", headers=headers)
result = r.json()
print(result)
返回结果:
{
"code": 10200, "message": "header ok!",
"data": {
"Content-Type": "application/json",
"token": "3d80caXELzU1aWmHwxl0TzW7jtterObm8l5EeAfipnhyaKmhFl8KdhFRvy4"
},
}
import requests
auth = ("admin", "admin123")
r = requests.post("http://127.0.0.1:5000/auth", auth=auth)
result = r.json()
print(result)
返回结果:
{"code": 10101, "message": "Authorization None"}
{"code": 10102, "message": "Authorization null"}
{"code": 10103, "message": "Authorization fail!"}
{"code": 10200, "message": "Authorization success!"}
import requests
files = {'file': open('D:\\log.txt', 'rb')}
r = requests.post("http://127.0.0.1:5000/upload", files=files)
result = r.json()
print(result)
返回结果:
{"code": 10200, "message": "upload success!"}
GET
请求,一般用作获取数据接口。
import requests
r = requests.get("http://127.0.0.1:5000/phone/1")
result = r.json()
print(result)
返回结果:
{"code": 10101, "message": "The phone id is empty"}
{"code": 10201, "message": "get success",
"data": {
"id": 1, "name": "小米手机", "price": 1999
}
}
PUT
请求,一般用作更新数据接口。
import requests
data = {"name":"华为手机", "price": "3999"}
r = requests.put("http://127.0.0.1:5000/phone/1", data=data)
result = r.json()
print(result)
返回结果:
{"code": 10102, "message": "The updated phone id is empty"}
{"code": 10202, "message": "update success",
"data": {
"id": 1, "name": "华为手机", "price": "3999"
}
}
DELETE
请求, 一般用作删除数据接口。
import requests
r = requests.delete("http://127.0.0.1:5000/phone/1")
result = r.json()
print(result)
返回结果:
{"code": 10103, "message": "The deleted phone id is empty"}
{"code": 10203, "message": "delete success"}
import requests
s = requests.Session()
r = s.post("http://127.0.0.1:5000/user_login", data={"username": "jack", "password": "123"})
result = r.json()
print(result)
r2 = s.get("http://127.0.0.1:5000/user_data")
result2 = r2.json()
print(result2)
返回结果:
{"code": 10200, "message": "login success"}
{"code": 10200, "message": "hello, jack"}