一、unittest框架进行接口测试
1、第一场景: 创建框架, 组建接口,调用所有的用例
import unittest #导入uittest框架
import requests #导入接口库
from time import *
class Cms_Api(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 testjk_dl_01(self):
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
data={‘userAccount’:‘admin’,‘loginPwd’:123456}
h={‘Content-Type’:‘application/x-www-form-urlencoded’}
jk=self.s.post(url=url,data=data,json=h)
print(jk.text)
def testjk_lmcx_02(self):
url2 = “http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do”
d2={‘parentId’:‘’,‘categoryName’:‘’,‘page=1’:“”}
h2 = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
jk2 = self.s.post( url=url2, data=d2, json=h2)
print(jk2.text)
if name == ‘main’:
unittest.main()
场景2、创建框架, 组建接口,调用部分的用例
import unittest #导入uittest框架
import requests #导入接口库
from time import *
class Cms_Api(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 testjk_dl_01(self):
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
data={‘userAccount’:‘admin’,‘loginPwd’:123456}
h={‘Content-Type’:‘application/x-www-form-urlencoded’}
jk=self.s.post(url=url,data=data,json=h)
print(jk.text)
def testjk_lmcx_02(self):
url2 = “http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do”
d2={‘parentId’:‘’,‘categoryName’:‘’,‘page=1’:“”}
h2 = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
jk2 = self.s.post( url=url2, data=d2, json=h2)
print(jk2.text)
if name == ‘main’:
# unittest.main()
tj=unittest.TestSuite()
tj.addTest((Cms_Api(‘testjk_dl_01’)))
r=unittest.TextTestRunner()
r.run(tj)
场景3、创建框架, 组建接口,调用路径下的用例
import unittest #导入uittest框架
import requests #导入接口库
from time import *
class Cms_Api(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 testjk_dl_01(self):
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
data={‘userAccount’:‘admin’,‘loginPwd’:123456}
h={‘Content-Type’:‘application/x-www-form-urlencoded’}
jk=self.s.post(url=url,data=data,json=h)
print(jk.text)
def testjk_lmcx_02(self):
url2 = “http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do”
d2={‘parentId’:‘’,‘categoryName’:‘’,‘page=1’:“”}
h2 = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
jk2 = self.s.post( url=url2, data=d2, json=h2)
print(jk2.text)
if name == ‘main’:
lj=r"C:\Users\Administrator\PycharmProjects\hz2\jiekou"
wj=unittest.TestLoader().discover(start_dir=lj,pattern=“jk_kj1*.py”)
r=unittest.TextTestRunner()
r.run(wj)
场景4、创建框架, 组建接口,调用路径下的用例并输出报告
import unittest #导入uittest框架
import requests #导入接口库
from time import *
from jiekou.HTMLTestRunner3_New import HTMLTestRunner
class Cms_Api(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 testjk_dl_01(self):
url=“http://cms.duoceshi.cn/cms/manage/loginJump.do”
data={‘userAccount’:‘admin’,‘loginPwd’:123456}
h={‘Content-Type’:‘application/x-www-form-urlencoded’}
jk=self.s.post(url=url,data=data,json=h)
print(jk.text)
def testjk_lmcx_02(self):
url2 = “http://cms.duoceshi.cn/cms/manage/findCategoryByPage.do”
d2={‘parentId’:‘’,‘categoryName’:‘’,‘page=1’:“”}
h2 = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
jk2 = self.s.post( url=url2, data=d2, json=h2)
print(jk2.text)
if name == ‘main’:
lj=r"C:\Users\Administrator\PycharmProjects\hz2\jiekou"
wj=unittest.TestLoader().discover(start_dir=lj,pattern=“jk_kj1*.py”)
bglu=r"C:\Users\Administrator\PycharmProjects\hz2\jiekou"
new=strftime(“%y-%m-%d %H-%M-%S”)
bg=bglu+“/”+str(new)+“_api.html”
f=open(bg,‘bw’)
r=HTMLTestRunner(stream=f,title=“接口自动化测试报告”,description=“用例执行情况如下”)
r.run(wj)
完成并跑出测试报告
============================================
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)