1、问题现象
在使用HTMLTestRunner生成测试报告时,出现程序运行不报错,但不能生成报告的情况。
刚开始找了很久没发现问题,后来加上打印信息,发现根本没执行生成报告这部分代码。最后网上找到原因:pycharm 在运行测试用例的时候 默认是以unittest 框架来运行的,所以不能生成测试报告。
需要设置成不要用unittest框架运行:
HTMLTestRunner.pyw文件经过修改的,需要下载:链接:https://pan.baidu.com/s/1BtF4Xus3kecI8qfTAy4z7w 提取码:2gtj
#unittest测试框架 #pytest比unittest全面一点 #导包 import requests#导入接口用的包 import unittest from pyJIAO.APP.APP.aa.baogao4 import Test01
from test1.PO3.zhiliao.HTMLTestRunnerNew import HTMLTestRunner import time
class Test(unittest.TestCase): # 用于测试用例执行前的初始化工作 def setUp(self): print("test start")
def test_bbb(self): print("test bbb")
# 用于测试用例执行之后的善后工作 def tearDown(self): print("test end")
if __name__ == '__main__': # 实例化测试套件 suite = unittest.TestSuite() # 加载测试用例 suite.addTest(Test("test_bbb")) #获取当前时间 now = time.strftime("%Y-%m-%d %H_%M_%S") # 定义报告存放路径 filename = now + 'result.html' #报告存放在当前目录 fp = open(filename, 'wb') # 定义测试报告 runner = HTMLTestRunner(stream=fp, title='测试报告', description='用例执行情况:') runner.run(suite) fp.close() # 关闭报告文件
2、如何判断是否以unittest框架运行?
如下,只要运行环境是unittest in ...的,就都是以unittest框架运行的

3、解决方案
只要不以unittest框架方式运行就可解决不能生成报告的问题。可自己新增一个不以unittest框架方式运行的环境。

1)如下点击edit configurations-->选中python-->点击+

2)点击python

3)填写环境名称和脚本路径

4)选择步骤三种新增的环境,运行脚本,即可生成报告

5、用HTMLTestRunner必须下载好文件放到python的lid目录也行,直接放到更目录也行,最终是要导入的:

二、还有一种比HTMLTestRunner跟加好用的,BeautifulReport
安装下载:一个报告:pip install BeautifulReport
执行代码如下:
import unittest from BeautifulReport import BeautifulReport
if __name__ == '__main__': test_suite = unittest.defaultTestLoader.discover('E:\\pythonJIAO\\test1\\jiekou\\scripts', pattern='jieko*.py') result = BeautifulReport(test_suite) result.report(filename='测试报告', description='测试deafult报告', report_dir='E:\\pythonJIAO\\test1\\PO3\\scripts', theme='theme_default')
"""
BeautifulReport.report report ( filename -> 测试报告名称, 如果不指定默认文件名为report.html description -> 测试报告用例名称展示 report_dir='.' -> 报告文件写入路径 theme='theme_default' -> 报告主题样式 theme_default theme_cyan theme_candy theme_memories )
BeautifulReport.add_test_img
""" 生成的报告是不是比较好看:

三、用pytest生成报告最方便,报告如下,具体看另外文章 pytest生成报告的插件: pip install pytest-html 安装pytest和allure: pip install pytest 和 pip install allure-pytest 查看:pip list
生成报告命令:pytest --html=report/report.html

|