多测师拱墅校区肖sir_高级金牌讲师_接口自动化_unittest框架
接口分层思想:
===================================
接口分层:
第一步:新建一个项目
第二步:在项目下新建6个包(包名自定义)
第三步:
对应包,详解:
(1)第一个包config 包 存放所有的接口参数
#登录接口
url1=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
data1={‘userAccount’:‘admin’,‘loginPwd’:123456}
h1={‘Content-Type’:‘application/x-www-form-urlencoded’}
#栏目查询接口
url2 = “http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do”
d2={‘parentId’:‘’,‘categoryName’:‘’,‘page=1’:“”}
h2 = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
(2)第二个包创建api包:组建接口(导入request库)
import requests
from config.cms_config import *
s=requests.Session()
class Cms(object):
def init(self):
pass
def dl(self):
r=s.post(url=url1 ,data=data1,json=h1)
print(r.text)
return r.text
#
def lmcx(self):
l=s.post(url=url2,data=d2,json=h2)
print(l.text)
return l
if name == ‘main’:
c=Cms()
c.dl()
c.lmcx()
(3)第三个包:testcase包 ,用例包
import unittest
from api.cms_api import *
class Cms_Test_Case(unittest.TestCase):
def setUp(self) -> None:
pass
def tearDown(self) -> None:
pass
def test1(self):
jg=Cms().dl()
# self.assertEqual(jg[‘msg’], “登录成功!”)
def test2(self):
jg2=Cms().lmcx()
# self.assertEqual(jg2[‘msg’],“查询栏目成功”)
if name == ‘main’:
unittest.main()
第四个包:run包:运行包
import unittest ,os,time
from utils.HTMLTestRunner3_New import HTMLTestRunner
from utils.mail3 import SendMail
#项目路径
testcase_path=os.path.join(os.path.abspath(os.path.dirname(os.getcwd()))) #print(testcase_path)
#报告路径
bg_path=os.path.join(os.path.abspath(os.path.dirname(os.getcwd())),“report”)#print(bg_path)
#测试用例路径
yl_path=os.path.join(os.path.abspath(os.path.dirname(os.getcwd())),“testcase”)#print(yl_path)
new=time.strftime(“%y-%m-%d %H-%M-%S”)
file=bg_path+“/”+str(new)+‘_api.html’
def Sy_run(ylname):
d=unittest.defaultTestLoader.discover(start_dir=yl_path,pattern=ylname)
f=open(file,‘wb’)
r=HTMLTestRunner(stream=f,description=“用例执行情况”,title=“cms接口测试框架报告”)
r.run(d)
def sfyj():
yj=SendMail(send_msg=file,attachment=file)
yj.send_mail()
if name == ‘main’:
Sy_run(“cms*.py”)
sfyj()
第5个包:report报告包,存放报告
第6个包:utils包,存放第三方工具包如报告模板,邮件模板
===================================================================
邮件发送:
(1)注册一个163邮件或qq邮件 账号
(2)登录账号
(3)在设置中====pop3点击
(4)开启邮件协议
(5)通过微信扫扫,点击发送
(6)获取授权码
(7)将授权码填入邮件模板中:
(7)在run中运行:
查看报告和邮件报告