接口测试之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)