介面測試之unittest框架

何周钦發表於2024-11-21

介面測試之unittest框架

import unittest
import requests
from time import *
from jk.HTMLTestRunner3_New import HTMLTestRunner

class Cms(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None: #類的開始
cls.s=requests.Session()
@classmethod
def tearDownClass(cls) -> None: #類的結束
print("類的結束")
def setUp(self) -> None: #方法開始
print(" 方法的開始")
def tearDown(self) -> None:
print("方法結束")
def test01_dl(self):
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"
jk = self.s.request("post", url=dl_url, data=dl_d, json=dl_h)
print(jk.text)
def test_02_lm(self):
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"}
jk1 =self.s.request("post", url=lm_url, data=lm_d,json=lm_h)
print(jk1.text)
def api(self):
print("我自由,我單獨")
if name == 'main':
# unittest.main() #第一種方法
# tj=unittest.TestSuite() ##第二種方法:

# tj.addTest(Cms("test01_dl"))
# r=unittest.TextTestRunner()
# r.run(tj)  

第三種方法

# lj=r"C:\Users\Administrator\PycharmProjects\hz15\jk"
# d=unittest.TestLoader().discover(start_dir=lj,pattern="jk_bg*")
# r=unittest.TextTestRunner()
# r.run(d)

第四種方法“”

匯入報告模板,生成介面測試報告
lj = r"C:\Users\Administrator\PycharmProjects\hz15\jk"
d = unittest.TestLoader().discover(start_dir=lj, pattern="jk_bg*")
new=strftime("%y-%m-%d %H-%M-%S")
f=lj+"/"+str(new)+"_api.html"
file=open(f,"wb")
r=HTMLTestRunner(stream=file,title="介面自動化測試報告",tester="hz",description="用例執行情況")
r.run(d)

相關文章