| 
                         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) 
 
                          (编辑:南平站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |