• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

请求pytest对接口自动化测试数据进行参数化


Recommended Posts

接口自动化的核心就是request+pytest,request库用作发送HTTP请求,而pytest就是对发送的数据进行参数化

gctujfagnal1475.png

以上是做参数化前要准备的东西

接下来我们看看代码上具体如何实现

#封装一个请求接口,形参分别为接口所需要传递参数。

定义添加项目(自身,

name='测试23123122',

aliasname='122 ',

started='2022-03-16 ',

ended='2022-03-16 ',

desc='123123 ',

id=0):

url=self.host '/project/add '

head={ ' Content-Type ' : ' application/x-www-form-urlencoded;charset=UTF-8'}

数据={

名称' :名称,

别名' :别名,

开始了:开始了,

结束' :结束,

表名的:号,

id': id

}

res=self.s.post(url=url,headers=head,data=datas,)

# 将数据中的编号提取出来

#打印(分辨率文本)

# return res.json()['id']

# 用正则将编号提取出来

# id=re.findall(r''id': ').*?)"",res.text)

# 发现时列表后通过索引下标提取

打印(研究文本)

返回资源

接下来具体看用例处如何实现(pytest)

导入pytest

从演示01。API。项目经理。添加项目导入Opmsaddproject

从demo01.com.readCsv进口ReadCsv

#通过我们封装的读取战斗支援车方法读取战斗支援车文件中的参数化数据

para=ReadCsv().readAll(start=1,end=34)

打印(类型(段落))

#pytest参数化

@ pytest。马克。参数化('名称,别名,开始,结束,desc”,第10段)

定义测试_添加项目_01(名称,别名,开始,结束,desc):

主持人=' 。53356 .38868868666

ad=Opmsaddproject(host=host)

res=ad.addProject(名称,别名,开始时间,结束时间,desc)

#提取接口返回的密码

re_code=res.json()['code']

#断言代码,断言其实就是判断,当重新编码等于0时用例算通过

断言re_code==0

jvlnzymcroo1476.png

最终我们看到的结果就是

qtotjsgskbz1477.png

按照我们的用例结果,脚本跑的完全没问题,错误11个正确24个

以上就是一个接口自动化的核心代码

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now