python自动化接口测试之封装
Posted 说笑谈古松
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python自动化接口测试之封装相关的知识,希望对你有一定的参考价值。
1、新建两个文件夹:interface和case:
insetrequest.py接口封装
# 接口
class req():
def __init__(self):
self.url='http://192.168.1.46:8080/recruit.students'
def get_req(self,s,par,fj_url):
r = s.get(url=self.url+fj_url, params=par)
return r
def post_req(self,s,fj_url,c_data,c_json):
r = s.post(url=self.url+fj_url, data=c_data, json=c_json)
return r
#登录用例
def dl(user,text):
par = {
'account': user,
'pwd': text
}
return par
#新建用例
def xj(name,typeid,canRecruit,remark):
c_data = {
'schoolName': name,
'listSchoolType[0].id': typeid,
'canRecruit': canRecruit,
'remark': remark
}
return c_data
#启用和禁用的用例
def qj(id,a,schid):
qy_json = [{"id":id,"disable":a,"schoolId":schid}]
return qy_json
#学校自主招生的用例
def zz(id,recruitStartTime,recruitEndTime,isStudentRecruitTime):
zz_json = [{"id":id,"recruitStartTime":recruitStartTime,"recruitEndTime":recruitEndTime,"isStudentRecruitTime":isStudentRecruitTime}]
return zz_json
#不同功能的地址封装
def dz(mk):
if mk=='dl':
fj_url = '/login/in'
elif mk=='xj':
fj_url ='/school/manage/addSchoolInfo'
elif mk=='qj':
fj_url ='/school/manage/enableOrDisableSchool'
elif mk == 'zz':
fj_url = '/school/manage/setStudentRecruitTime'
return fj_url
#封装时测试用的,即单元测试
# if __name__ =='__main__':
# s = requests.session()
# #创建对象/实例化
# d=req()
# #登录
# de=d.get_req(dl('admin','660B8D2D5359FF6F94F8D3345698F88C'),dz('dl'))
# #新建学校
# name = str(random.randint(0, 1000)) + '欧阳' + str(random.randint(0, 1000))
# l=d.post_req(dz('xj'),xj(name,2,1,''),None)
# #利用re模块使用正则提起的方式提取出新建学校的id和登录号
# zid=re.findall('为(.+?),密码为.+?id":(.+?),"schoolName',l.text)
# #启用学校
# qyschool=d.post_req(dz('qj'),None,qj("10182",1,"183"))
# # 禁用学校
# jyschool = d.post_req(dz('qj'), None, qj(zid[0][0],0,zid[0][1]))
# #设置学校自主招生
# zizhuzs=d.post_req(dz('zz'),None,zz('183',None,None,'0'))
# #打印
# print(zizhuzs.text)
test_login.py调用接口模块,执行测试用例
import requests
import random, re
from interface import insetrequest as a#调用接口模块,取别名为a
# 登录接口用例执行
def test_login_success():
dd = d.get_req(s, a.dl('admin', '660B8D2D5359FF6F94F8D3345698F88C'), a.dz('dl'))
# print(dd.text)
def test_login_user_Eorr():
dd = d.get_req(s, a.dl('admin1', '660B8D2D5359FF6F94F8D3345698F88C'), a.dz('dl'))
print(dd.text)
def test_login_pwd_Eorr():
dd = d.get_req(s, a.dl('admin1', '660B8D2D5359FF6F94F8D3345698F'), a.dz('dl'))
print(dd.text)
# 新建学校测试用例执行
def new_school_success():
name = str(random.randint(0, 1000)) + '欧阳' + str(random.randint(0, 1000))
l = d.post_req(s, a.dz('xj'), a.xj(name, 2, 1, ''), None)
# print(l.text)
# 利用re模块使用正则提起的方式提取出新建学校的id和登录号
zid = re.findall('为(.+?),密码为.+?id":(.+?),"schoolName', l.text)
return zid
def new_school_Type_Eorr():
name = str(random.randint(0, 1000)) + '欧阳' + str(random.randint(0, 1000))
l = d.post_req(s, a.dz('xj'), a.xj(name, 6, 1, ''), None)
print(l.text)
# 启用学校用例执行
def start_school_success():
qyschool = d.post_req(s, a.dz('qj'), None, a.qj("10182", 1, "183"))
#print(qyschool.text)
def start_school_disable_Eorr():
qyschool = d.post_req(s, a.dz('qj'), None, a.qj("10182", 2, "183"))
print(qyschool.text)
# 禁用学校用例执行
def stop_school_success(zid):
jyschool = d.post_req(s, a.dz('qj'), None, a.qj(zid[0][0], 0, zid[0][1]))
print(jyschool.text)
def stop_school_disable_Eorr(zid):
jyschool = d.post_req(s, a.dz('qj'), None, a.qj(zid[0][0], 4, zid[0][1]))
print(jyschool.text)
#设置学校学生录入时间用例执行
def independent_recruitment_success():
zizhuzs = d.post_req(s, a.dz('zz'), None, a.zz('183', None, None, '0'))
print(zizhuzs)
def independent_recruitment_Eorr():
zizhuzs = d.post_req(s, a.dz('zz'), None, a.zz('183', None, None, '-1'))
print(zizhuzs)
if __name__ == '__main__':
s = requests.session()#保持登录态
# 创建对象/实例化
d = a.req()
# 登录
test_login_success()
# 新建学校
z = new_school_success()
# 利用re模块使用正则提起的方式提取出新建学校的id和登录号
# zid=re.findall('为(.+?),密码为.+?id":(.+?),"schoolName',l.text)
#启用学校
start_school_success()
# 禁用学校
stop_school_success(z)
##设置学校自主招生
independent_recruitment_success()
# #打印
# print(zizhuzs.text)
以上是关于python自动化接口测试之封装的主要内容,如果未能解决你的问题,请参考以下文章