HTMLTestRunner 框架可用来天生可视化测试陈诉,并能很好的与 unittest 框架团结行使,接下来我们以一段代码来展示一下 HTMLTestRunner 的行使。
- if __name__=='__main__':
-
- from HTMLTestRunner import HTMLTestRunner
-
- testData = [
-
- (10, 9, 19),
-
- (12, 13, 25),
-
- (12, 10, 22),
-
- (2, 4, 6)
-
- ]
-
- suite = unittest.TestSuite()
-
- for i in testData:
-
- suite.addTest(ExtendTestCaseParams.parametrize(ApiTestSample,'test_jiafa',canshu=i))
-
- currentTime = time.strftime("%Y-%m-%d %H_%M_%S")
-
- result_path = './test_results'
-
- if not os.path.exists(path):
-
- os.makedirs(path)
-
- report_path = result_path + '/' + currentTime + "_report.html"
-
- reportTitle = '测试陈诉'
-
- desc = u'测试陈诉详情'
-
- with open(report_path, 'wd') as f:
-
- runner = HTMLTestRunner(stream=f, title=reportTitle, description=desc)
-
- runner.run(suite)
测试功效如下:
下面具体讲授一下 html 陈诉的天生代码:
- runner = HTMLTestRunner(stream=fp, title=reportTitle, description=desc)
HTMLTestRunner 中的 stream 暗示输入流,这里我们将文件描写符转达给 stream,title 参数暗示要输出的测试陈诉主题名称,description 参数是对测试陈诉的描写。在行使 HTMLTestRunner 时,有几点必要留意:
1)HTMLTestRunner 模块非 Python 自带库,必要到 HTMLTestRunner 的官网下载
该安装包;
2)官网的 HTMLTestRunner 模块仅支持 Python 2.x 版本,假如要在 Python 3.x中,必要修改部门代码,修改的代码部门请自行上网搜刮;
假如必要天生 xml 名目,只需将上面代码中的
- runner = HTMLTestRunner(stream=fp, title=reportTitle, description=desc)
-
- runner.run(suite)
修改为如下代码
- import xmlrunner
-
- runner = xmlrunner.XMLTestRunner(output='report')
-
- runner.run(suite)
4、接口测试分类
前面各人对接口哀求,测试框架和测试功效可视化方面有了深入的相识。有了前面的基本,对付接下来领略和编写接口测试会有很大辅佐。这里我们先来讲授一下接口测试与单位测试的区别。单位测试只针对函数举办多组参数测试,包罗正常和非常参数组合。而接口测试是针对某一接口举办多组参数测试。现实接口测试中,我们又将接口测试分为两种:
1)单接口测试;
2)多接口测试。
对付单接口测试,只需针对单个接口测试,测试数据按照接口文档中的参数法则来计划测试用例;对多接口测试,起主要确保接口之间挪用逻辑正确,然后再按照接口文档中的参数法则来计划用例举办测试。下面我就按照这两种差异环境的接口测试,用现实项目代码展示一下。
4.1 单接口测试
- class TestApiSample(ExtendTestCaseParams):
-
- def setUp(self):
-
- pass
-
- def tearDown(self):
-
- pass
-
- def register(self, ip, name, desc):
-
- url = 'http://%s/api/v1/reg' % ip
-
- headers = {"Content-Type": "application/x-www-form-urlencoded"}
-
- para = {"app_name": name, "description": desc}
-
- req = self.Post(url, para, headers)
-
- return req
-
- def test_register(self):
-
- for index, value in enumerate(self.param):
-
- print('Test Token {0} parameter is {1}'.format(index, value))
-
- self.ip = self.param[1]
-
- self.name = self.param[2]
-
- self.desc = self.param[3]
-
- self.expectedValue = self.param[4]
-
- req = self.grant_register(self.ip, self.name, self.desc)
-
- self.assertIn(req.status_code, self.expectedValue, msg="Test Failed.")
-
- if __name__=='__main__':
-
- import random
-
- import string
-
- ip = '172.36.17.108'
-
- testData = [
-
- (1, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200),
-
- (2, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200),
-
- (3, ip, ''.join(random.sample(string.ascii_letters + string.digits, 7)), '', 200)
-
- ]
-
- suite = unittest.TestSuite()
-
- for i in testData:
-
- suite.addTest(ExtendTestCaseParams.parametrize(TestApiSample,'test_register',canshu=i))
-
- currentTime = time.strftime("%Y-%m-%d %H_%M_%S")
-
- path = './results'
-
- if not os.path.exists(path):
-
- os.makedirs(path)
-
- report_path = path + '/' + currentTime + "_report.html"
-
- reportTitle = '接口测试陈诉'
-
- desc = u'接口测试陈诉详情'
-
- with open(report_path, 'wd') as f:
-
- runner = HTMLTestRunner(stream=f, title=reportTitle, description=desc)
-
- runner.run(suite)
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|