配置文件
# -*- coding: utf-8 -*-
# 2022/1/16
userame = "th0004"
password = "e2e2fd34c1cfebdf431177db8e49fdd2"
HOST = "http://121.41.14.39:8082"
测试脚本
# -*- coding: utf-8 -*-
# 2022/1/16
"""
接口自动化测试,模拟前端给后端发送协议,并检验返回内容正确性
发现前端不能发现的隐藏bug
及早爆出风险
前端:vue
后端:springboot,后端API,H5手机端,管理员web,商户web
访问层--》代理层---》前端----》后端----》数据库层
post函数的参数改为params时,请求body为None
"字典,""json字符串
post的data参数指后面的请求参数是表单格式
json是json格式
params也是表单格式,但是请求参数放到url内
files是文件上传接口
鉴权机制,封装到headers内部的token,cookies:jsessionid,自定义鉴权
"""
"""
http协议
请求行:请求方法 请求路径 请求协议及协议版本
请求头
空行
请求正文
状态行:协议及版本,状态码,状态描述符
响应头
空行
响应正文
"""
import requests
import pytest
from port.settings import userame, password, HOST
loginurl = f"{HOST}/account/sLogin"
payload = {"username":userame, "password":password}
resp = requests.post(loginurl, data=payload)#
print(resp.text)#返回json字符串
"""{
"code":20000,
"data":{
"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2
NDIzNTA5MzUsInVzZXJJZCI6MTA1MDgsInVzZXJuYW1lIjoidGgwMDA0In
0.g550nnODi0IRLGszxcvk48-WFIDOreSlZkmDxbZRXow"
},
"flag":"松勤教育",
"msg":"成功",
"success":false
}"""
print(resp.json())#返回字典
"""{'code': 20000, 'data':
{'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NDIz
NTA5MzUsInVzZXJJZCI6MTA1MDgsInVzZXJuYW1lIjoidGgwMDA0In0.
g550nnODi0IRLGszxcvk48-WFIDOreSlZkmDxbZRXow'},
'flag': '松勤教育', 'msg': '成功', 'success': False}
"""
print(resp.url)#http://121.41.14.39:8082/account/sLogin
print(resp.headers)
"""
{'Access-Control-Allow-Methods': 'GET,POST,OPTIONS,PUT,DELETE',
'Content-Type': 'application/json;charset=UTF-8',
'Content-Length': '255',
'Date': 'Sun, 16 Jan 2022 15:35:35 GMT',
'Keep-Alive': 'timeout=20',
'Connection': 'keep-alive'}"""
print(resp.cookies)#<RequestsCookieJar[]>
print(resp.encoding)#UTF-8
print(resp.history)#[]
print(resp.elapsed)#0:00:00.057816
print(resp.request.url)#http://121.41.14.39:8082/account/sLogin
print(resp.request.method)#POST
print(resp.request.body)#username=th0004&password=e2e2fd34c1cfe
#bdf431177db8e49fdd2
#post函数的参数改为params时,请求body为None
print(resp.request.headers)
"""
{'User-Agent': 'python-requests/2.25.1',
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*',
'Connection': 'keep-alive',
'Content-Length': '57',
'Content-Type': 'application/x-www-form-urlencoded'}"""