记录python接口自动化测试--利用unittest生成测试报告(第四目)

前面介绍了是用unittest管理测试用例,这次看看如何生成html格式的测试报告

生成html格式的测试报告需要用到 HTMLTestRunner,在网上下载了一个HTMLTestRunner.py,然后放到python安装路径下的lib目录中。

(我用的python3,是下载的虫师写的那个,下载地址-->链接:https://pan.baidu.com/s/101y-X--o6iSd9WTDv5K4XQ 密码:24xh)

1.执行单个.py文件中的测试用例

# -*-coding:UTF:8-*-

import unittest
from interface.demo import RunMain # 从之前封装的文件中,引入RunMain类
import HTMLTestRunner # 导入HTMLTestRunner模块
import json class TestMethod(unittest.TestCase): # 定义一个类,继承自unittest.TestCase def setUp(self):
self.run = RunMain() # 在初始化方法中实例化一个对象,这样就不需要在每个用例中再进行实例化了 def test01(self):
"""获取办件申请人信息""" #在用例名下添加接口描述,可以增加测试报告可读性
url = 'http://localhost:7001/XXX'
data = {
'controlSeq': ''
}
r = self.run.run_main(url, 'POST', data) # 调用RunMain类中run_main方法
print(r)
re = json.loads(r)
self.assertEqual(re['status'], '', '测试失败')
# globals()['userid'] = 22 #定义全局变量 def test02(self):
"""查询办件进度结果信息接口"""
# print(userid) #使用case1中的全局变量,执行时需要全部执行,不能只执行后面的,不然会报错
url = 'http://localhost:7001/XXX'
data = {
"controlSeq": ""
}
r = self.run.run_main(url, 'GET', data)
print(r)
re = json.loads(r)
self.assertEqual(re["status"], '', '测试失败') # @unittest.skip('test03') # 跳过用例test03
def test03(self):
"""保存办件快递信息接口(审批3.0)"""
url = 'http://localhost:7001/XXX'
data = {
'controlSeq': '',
'seq': '',
'type': ''
}
r = self.run.run_main(url, 'POST', data)
print(r)
# print(type(r)) # 查看返回对象r的类型
re = json.loads(r)
# print(type(re))
self.assertEqual(re['status'], '', '测试失败') if __name__ == "__main__":
# unittest.main()
# print('__name__==__main__')
filename = 'E:/CommonService/interface/report/testresult.html' #测试报告的存放路径及文件名
fp = open(filename, 'wb') # 创测试报告html文件,此时还是个空文件 suite = unittest.TestSuite() # 调用unittest的TestSuite(),理解为管理case的一个容器(测试套件)
suite.addTest(TestMethod('test01')) # 向测试套件中添加用例,"TestMethod"是上面定义的类名,"test01"是用例名
suite.addTest(TestMethod('test02'))
suite.addTest(TestMethod('test03'))
# runner = unittest.TextTestRunner() # 执行套件中的用例
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='接口测试报告', description='测试结果如下: ')
# stream = fp 引用文件流
# title 测试报告标题
# description 报告说明与描述
runner.run(suite) # 执行测试
fp.close() # 关闭文件流,将HTML内容写进测试报告文件

记录python接口自动化测试--利用unittest生成测试报告(第四目)

记录python接口自动化测试--利用unittest生成测试报告(第四目)

2.使用discover()方法批量加载多个.py文件中的测试用例

工程目录如下:

记录python接口自动化测试--利用unittest生成测试报告(第四目)

case是测试用例所在目录,里面包括2个二级目录,存放的都是测试用例;

report存放测试报告的目录;

最外层的run_all_case.py,用它来执行所有用例

(1)直接加载discover中的用例

run_all_case.py

import time
from HTMLTestRunner import HTMLTestRunner
import unittest case_dir = 'E:/CommonService/interface/case' # 定义用例所在路径
"""定义discover方法"""
discover = unittest.defaultTestLoader.discover(case_dir,
pattern='test_*.py',
top_level_dir=None)
"""
1.case_dir即测试用例所在目录
2.pattern='test_*.py' :表示用例文件名的匹配原则,“*”表示任意多个字符,这里表示匹配所有以test_开头的文件
3.top_level_dir=None:测试模块的顶层目录。如果没顶层目录(也就是说测试用例不是放在多级目录
中),默认为 None
""" if __name__ == "__main__":
"""直接加载discover"""
now = time.strftime("%Y-%m-%d %H_%M_%S")
filename = 'E:/CommonService/interface/report/' + now + '_result.html'
fp = open(filename, 'wb')
runner = HTMLTestRunner(stream=fp,
title='UnifiedReporting Test Report',
description='Implementation Example with: ')
runner.run(discover)
fp.close()

(2)通过把discover中的用例加载到测试套件中执行

run_all_case.py

import time
from HTMLTestRunner import HTMLTestRunner
import unittest case_dir = 'E:/CommonService/interface/case' # 定义用例所在路径 """定义discover方法"""
discover = unittest.defaultTestLoader.discover(case_dir,
pattern='test_*.py',
top_level_dir=None)
"""
1.case_dir即测试用例所在目录
2.pattern='test_*.py' :表示用例文件名的匹配原则,“*”表示任意多个字符
3.top_level_dir=None:测试模块的顶层目录。如果没顶层目录(也就是说测试用例不是放在多级目录
中),默认为 None
""" if __name__ == '__main__':
"""通过把discover中的用例加载到测试套件中执行"""
suite = unittest.TestSuite() # 定义一个测试套件
# discover 方法筛选出来的用例,循环添加到测试套件中
for test_suite in discover:
for test_case in test_suite:
suite.addTests(test_case)
print(suite) #打印一下可以看到suite中添加了哪些测试用例
now = time.strftime("%Y-%m-%d %H_%M_%S")
filename = 'E:/CommonService/interface/report/' + now + '_result.html'
fp = open(filename, 'wb')
runner = HTMLTestRunner(stream=fp,
title='UnifiedReporting Test Report',
description='Implementation Example with: ')
runner.run(suite)

(3)把discover加载测试用例的过程封装到一个函数中

run_all_case.py

import time
from HTMLTestRunner import HTMLTestRunner
import unittest def allCase():
"""定义一个函数,封装discover加载测试用例的方法"""
case_dir = 'E:/CommonService/interface/case' # 定义用例所在路径
suite = unittest.TestSuite() # 定义一个测试套件
discover = unittest.defaultTestLoader.discover(case_dir,
pattern='test_*.py',
top_level_dir=None)
# discover 方法筛选出来的用例,循环添加到测试套件中
for test_suite in discover:
for test_case in test_suite:
suite.addTests(test_case)
return suite if __name__ == '__main__':
allsuite = allCase()
# runner = unittest.TextTestRunner()
now = time.strftime("%Y-%m-%d %H_%M_%S")
filename = 'E:/CommonService/interface/report/' + now + '_result.html'
fp = open(filename, 'wb')
runner = HTMLTestRunner(stream=fp,
title='UnifiedReporting Test Report',
description='Implementation Example with: ')
runner.run(allsuite)

测试报告如下:

记录python接口自动化测试--利用unittest生成测试报告(第四目)

上一篇:Zabbix概念、安装以及快速入门


下一篇:BZOJ2940 条纹