​这是我参与8月更文挑战的第19天,活动详情查看:8月更文挑战 

接上一篇,我们直接复制出来后进过改造,形成了一个python测试用例,但是这里的参数过于臃肿,我们试着把这些参数给提出来,第一步,我们先小步改造,先提到同一个文件中,用变量保存。我们可以把url,headers,data,断言都提出来。

我们先放在一个文件中保存。提出的代码如下;

我们暂且叫他ce.txt。

/openapi/api/v2|{\r\n\t\r\n \"userInfo\": {\r\n \"apiKey\": \"\",\r\n \"userId\": \"\"\r\n }\r\n}|{'Content-Type': "application/json", 'User-Agent': "PostmanRuntime/7.19.0",'Accept': "*/*",'Cache-Control': "no-cache", 'Postman-Token': "25132ec6-9d02-421c-ab22-773b1fd70035,65c29f56-030a-4d3d-862f-ad0de3ed50a6",'Host': "openapi.tuling123.com",'Accept-Encoding': "gzip, deflate",'Content-Length': "78",'Connection': "keep-alive", 'cache-control': "no-cache"}|code|POST
内容如上面的,我们接下来去封装解析这个文档的函数。
即readtxt.py
def get():
reslut = []
f=open("case.txt","r")
all=f.readlines()
for item in all:
dictone={}
reslut_all=item.split("|")
dictone["url"]=reslut_all[0]
dictone['data']=reslut_all[1]
dictone['headers']=reslut_all[2]
dictone['assert']=reslut_all[3]
dictone['method']=reslut_all[4]
reslut.append(dictone)
return reslut
这样我们就能获取到我们的参数了,这里呢,我们需要确定的是我们读取的参数的都转化成了str,然后写进去了字典,最后放在列表里面。那么我们对测试用例的改造。
import requests
import unittest
from config import baseurl
from readtxt import get
restlue=get()
url = baseurl + restlue[0]['url']
class Testcase(unittest.TestCase):
def tearDown(self) -> None:
pass
def setUp(self) -> None:
pass
def testone(self):

response = requests.request(restlue[0]['method'], url, data=restlue[0]['data'], headers=eval(restlue[0]['headers']))

self.assertTrue(restlue[0]['assert'] in response.text)
if __name__=="__main__":
unittest.main()

这是改造后的测试用例,我们执行下看下,结果。

postman接口用例转化为python自动化测试用例(三)_用例

这样我们就完成了测试,有些人说,那么接下来 要在写用例,我是不是可以直接在case文档中去写入呢。
答案是可以。文档我们再加一条。
/openapi/api/v2|{\r\n\t\r\n    \"userInfo\": {\r\n        \"apiKey\": \"\",\r\n        \"userId\": \"\"\r\n    }\r\n}|{'Content-Type': "application/json", 'User-Agent': "PostmanRuntime/7.19.0",'Accept': "*/*",'Cache-Control': "no-cache", 'Postman-Token': "25132ec6-9d02-421c-ab22-773b1fd70035,65c29f56-030a-4d3d-862f-ad0de3ed50a6",'Host': "openapi.tuling123.com",'Accept-Encoding': "gzip, deflate",'Content-Length': "78",'Connection': "keep-alive", 'cache-control': "no-cache"}|code|POST
/openapi/api/v2|{\r\n\t\r\n \"userInfo\": {\r\n \"apiKey\": \"\",\r\n \"userId\": \"\"\r\n }\r\n}|{'Content-Type': "application/json", 'User-Agent': "PostmanRuntime/7.19.0",'Accept': "*/*",'Cache-Control': "no-cache", 'Postman-Token': "25132ec6-9d02-421c-ab22-773b1fd70035,65c29f56-030a-4d3d-862f-ad0de3ed50a6",'Host': "openapi.tuling123.com",'Accept-Encoding': "gzip, deflate",'Content-Length': "78",'Connection': "keep-alive", 'cache-control': "no-cache"}|code|POST
但是我们发现出错误了。

postman接口用例转化为python自动化测试用例(三)_后端_02

仔细看看,发现,在解析用例参数的时候   没有处理到换行符。
所以对获取用例的代码进行修改: 即readtxt.py文件
def get():
reslut = []
f=open("case.txt","r")
all=f.readlines()
for item in all:
dictone={}
reslut_all=item.split("|")
dictone["url"]=reslut_all[0]
dictone['data']=reslut_all[1]
dictone['headers']=reslut_all[2]
dictone['assert']=reslut_all[3]
dictone['method']=reslut_all[4].split("\n")[0]
reslut.append(dictone)
return reslut
这样就可以读取了。执行后,发现只有一条用例,这里我们还要增加一条用例。
def testtwo(self):

response = requests.request(restlue[1]['method'], url, data=restlue[1]['data'], headers=eval(restlue[1]['headers']))

self.assertTrue(restlue[1]['assert'] in response.text)
增加用例后,我们可以去执行

postman接口用例转化为python自动化测试用例(三)_用例_03

这样就执行完毕,可能有人会说,这样麻烦,我维护一条用例,就要新增代码,我不想新增,其实也可以的,后续的章节,我们会介绍