肖sir__介面測試之python+rquest+unittest分層自動化框架

xiaolehua發表於2024-04-10

介面測試之介面po框架

一、新建一個專案

介面自動化框架設計實戰:

第一包:config

案例:

#登入介面
dl_url = 'http://cms.duoceshi.cn/cms/manage/loginJump.do'
dl_d = {'userAccount': 'admin', 'loginPwd': '123456'}
dl_h = "Content-Type:application/x-www-form-urlencoded"

#欄目介面
lm_url= "http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do"
lm_d= {'parentId': '', 'categoryName': '', 'page': '1'}
lm_h = {"Content-Type": "application/x-www-form-urlencoded"}

第二包:api 組建介面包

#組建併傳送介面
import requests
from config.cms_config import *
s=requests.Session()
class Cms(object):
def __init__(self):
pass
def dl(self):
jk=s.post(url=dl_url,data=dl_d,json=dl_h)
#print(jk.text)
return jk
def lm(self):
jk1=s.post(url=lm_url,data=lm_d,json=lm_h)
# print(jk1.text)
return jk1
if __name__ == '__main__':
c=Cms()
c.dl()
c.lm()

第三個包:testcase存放用例,

import  unittest
from api.cms_api import *
class Cms_Test(unittest.TestCase):
def setUp(self) -> None:
pass
def tearDown(self) -> None:
pass
def test001(self):
s=Cms().dl()
def test002(self):
l=Cms().lm()
if __name__ == '__main__':
V=Cms_Test()
V.test001()
V.test002()

第四個包:report包 報告包

第五包:utils包工具類包

第六個包:

from utils.HTMLTestRunner3_New import  HTMLTestRunner
import unittest,os,time
from utils.mail3 import SendMail
xm_path=os.path.join(os.path.abspath(os.path.dirname(os.getcwd()))) #專案路徑
print(xm_path)
test_path=os.path.join(os.path.abspath(os.path.dirname(os.getcwd())),'testcase')#測試用例路徑
print(test_path)
report_path=os.path.join(os.path.abspath(os.path.dirname(os.getcwd())),'report')#報告路徑
print(report_path)
new=time.strftime('%y-%m-%d %H-%M-%S')
f=report_path+"/"+str(new)+'_report.html'
def bg(x):
d=unittest.TestLoader().discover(start_dir=test_path,pattern=x)
file=open(f,"wb")
r=HTMLTestRunner(stream=file,description="用例執行情況",tester='hz',title="cms介面po框架")
r.run(d)
def yj():
fs=SendMail(send_msg=f,attachment=f)
fs.send_mail()
if __name__ == '__main__':
bg("cms*.py")
yj()

二、郵箱設定

(1)

FDZRXWYAVDSFMFTM

相關文章