前言 :
post和get的接口测试
1、主要是通过传递参数、消息头、请求方式、以及请求结果的对比来进行参数化
文章目录
一、get请求
对于get请求的操作
标题get请求内容分析
分析不带请求头、请求体的内容:
1、get请求中不传请求头、请求体
get请求代码:
import requests
response = requests.get('http://127.0.0.1/captchaImage')
print(response.text)
get请求取打印结果
思路分析:要按字典取值去取
1、将.text 换成.json
import requests
response = requests.get('http://127.0.0.1/captchaImage')
content = response.json()
# 比如:我取'img内容'
print(content['img'])
二、post请求
获取到打印结果的uid、token内容
三、post函数的实践
1、思路
1、登录打印返回值的token 和uid
2、把token和uid应用到考试接口去
1、登录封装到class类中,取到返回值的token和uid
####1.2 代码实现
# coding=utf-8
import requests
import json
class Name1():
def test_login(self):
headers1 = {"Content-Type":"application/json"}
payload ={"userName":'yangyun',"password":"666666","remenberMe":"false","ldstatus":"true","logo":"pc"}
# print(payload)
url = 'http://127.0.0.1/login'
res = requests.post(url=url,json=payload,headers=headers1)
content = res.json()
# print(content)
token1 = content['data']['token']
uid1 = content['data']['uid']
return token1,uid1
if __name__ == '__main__':
name1 = Name1()
name1.test_login()
print(name1.test_login())
2、调用class方法取到token和uid,存入到考试接口的信息头和body参数中
2.1代码实现:
因为实际项目,参数和ip有删减,但是大体不变
from 接口测试.config import Name1
import requests
class Data(object):
def __init__(self):
self.name1 = Name1()
def test_kaoshi(self):
get_data = self.name1.test_login()
headers1 = {"token": get_data[0]}
payload = {"adminId": get_data[1],
"typeId": "20200528095000497432f3b8746a37ca", "examTime": "1"}
url = 'http://127.0.0.1/exam/access/TestPaperController/commitExamResult'
res = requests.post(url=url,json=payload,headers=headers1)
print(res.json())
if __name__ == '__main__':
data = Data()
data.test_kaoshi()