接口自动化框架设计
一、第一包config包
二、第二个包组建接口api
三、第三个包 testcase
四、第四个包report 报告包
五、utils 包存放工具类
六、第六个包run 包就是运行
七、结果:
报告:
接口自动化连框架设计实战:
第一步:新建一个新的项目
第二步:在项目下建6个包
命名:
第一个包:新建config包
存放所有接口的参数,新建config包(命名config)
(1)新建config包,
(2)新建config文件(.py)
配置所有的接口基本信息
#登录接口
dl_post_url = “http://cms.duoceshi.cn/cms/manage/loginJump.do”
data = {‘userAccount’: ‘admin’, ‘loginPwd’: 123456}
headers = {“Content-Type”: “application/x-www-form-urlencoded”}
#查询用户接口
lmcx_url= “http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do”
lmcx_data = {‘parentId’: ‘’, ‘categoryName’: “”, ‘page’: 1}
lmcx_headers = {“Content-Type”: “application/x-www-form-urlencoded”}
第二个包:api包
源代码:
import requests
s=requests.Session()
from cms_config.config import *
class Cms(object):
def init(self):
pass
def Dl(self):
r=s.post(url=dl_post_url,data=dl_data,json=dl_headers)
return r.text
def lmcx(self):
r=s.post(url=lmcx_url,data=lmcx_data,json=lmcx_headers)
return r.text
if name == ‘main’:
c=Cms()
print(c.Dl())
print(c.lmcx())
第三个包:testcase 存放所有的用例
#存放所有的用例
import unittest
from cms_api.api import *
class Test_case(unittest.TestCase):
def setUp(self) -> None:
pass
def tearDown(self) -> None:
pass
def test001(self):
x=Cms().Dl()
def test(self):
y=Cms().lmcx()
if name == ‘main’:
unittest.main()
第四个包:report 包 存放所有报告
第五个包:工具类包
第六包:run包 运行所有的用例
import unittest,os,time
from cms_utils.HTMLTestRunner3_New import HTMLTestRunner
from cms_utils.mail3 import SendMail
p=os.path.join(os.path.abspath(os.path.dirname(os.getcwd())))
testcase_path=os.path.join(os.path.abspath(os.path.dirname(os.getcwd())),“cms_testcase”)
print(testcase_path)bg_path=os.path.join(os.path.abspath(os.path.dirname(os.getcwd())),“cms_report”)
print(bg_path)new=time.strftime("%y-%m-%d %H-%M-%S")
file=bg_path+"/"+str(new)+"_repot.html"
def bg(x):
d=unittest.defaultTestLoader.discover(start_dir=testcase_path,pattern=x)
f=open(file,“wb”)
r=HTMLTestRunner(stream=f,description=“执行用例情况如下”,title=“cms接口自动化框架”,tester=“hz12”)
r.run(d)
def yj():
fs=SendMail(send_msg=file,attachment=file)
fs.send_mail()
if name == ‘main’:
bg(‘cms*.py’)
yj()
备注:
邮件模板:
点击设置,选择pop3



