HTMLTestRunner是Python标准库unittest单元测试框架的一个扩展,可以生成易于使用的HTML测试报告,这个扩展很简单,只有一个HTMLTestRunner.py,下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html。

下载该文件,然后把它放到Python3的lib目录下,我的为:D:\Python35\Lib。

1.修改HTMLTestRunner

HTMLTestRunner.py是基于Python2开发的,如果是Python3环境,需要修改HTMLTestRunner.py文件。

 import StringIO修改为import io
self.outputBuffer = StringIO.StringIO()修改为self.outputBuffer = io.StringIO()
print >>sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)修改为print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))
if not rmap.has_key(cls):修改为if not cls in rmap:
uo = o.decode('latin-1')修改为uo = e
ue = e.decode('latin-1')修改为ue = e

在Python交互模式下引入HTMLTestRunner模块,没有报错,则说明添加成功:

 C:\Users>python3
Python 3.5. (v3.5.2:4def2a2901a5, Jun , ::) [MSC v. bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import HTMLTestRunner
>>>

2.生成HTML测试报告

 # coding:utf-
import unittest
from selenium import webdriver
import time
from HTMLTestRunner import HTMLTestRunner class TestBaidu(unittest.TestCase): def setUp(self):
self.driver = webdriver.Firefox()
self.driver.maximize_window()
self.driver.implicitly_wait()
self.base_url = "http://www.baidu.com" def tearDown(self):
self.driver.quit() def test_baidu(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").click()
time.sleep()
self.assertEqual(driver.title, "selenium_百度搜索") if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(TestBaidu("test_baidu"))
file = open("./resultReport.html", "wb")
runner = HTMLTestRunner(stream=file, title="百度搜索测试报告",description="用例执行情况:")
runner.run(suite)
file.close()

在当前目录打开resultReport.html:

 3.优化测试报告

HTMLTestRunner可以读取doc string类型的注释,因此,只需要在测试类或者测试方法下添加doc string类型的注释即可显示测试类和测试方法的注释。

 # coding:utf-
import unittest
from selenium import webdriver
import time
from HTMLTestRunner import HTMLTestRunner 8 class TestBaidu(unittest.TestCase):
9 """测试百度搜索"""

def setUp(self):
self.driver = webdriver.Firefox()
self.driver.maximize_window()
self.driver.implicitly_wait()
self.base_url = "http://www.baidu.com" def tearDown(self):
self.driver.quit() 19 def test_baidu(self):
20 """搜索关键词selenium"""

driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").click()
time.sleep()
self.assertEqual(driver.title, "selenium_百度搜索") if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(TestBaidu("test_baidu"))
file = open("./resultReport.html", "wb")
runner = HTMLTestRunner(stream=file, title="百度搜索测试报告",description="用例执行情况:")
runner.run(suite)
file.close()

运行脚本,然后在当前目录下打开resultReport.html文件:

4.修改测试报告名称

在测试之前,如果不修改测试报告文件名,则运行结束后,之前的测试报告会被覆盖,为了避免这种情况,我们可以在文件命中添加时间。

Python的time模块为我们提供了很多操作时间的方法:

 # coding:utf-
import time print(time.time()) # 返回当前时间(以秒为单位),自1970-- ::00以来的秒数 print(time.localtime()) # 将1970-- ::00以来的秒转换为本地时间的元组 print(time.ctime()) # 将1970-- ::00以来的以秒为单位的时间转换为本地时间的字符串 print(time.strftime(("%Y-%m-%d %H_%M_%S"))) # 根据设置的格式将时间元组转换为字符串
"""
%Y 带世纪部分的十进制年份,
%m 十进制表示的月份, [,].
%d 十进制表示每月的第几天 [,].
%H 24小时制十进制的小时 [,].
%M 十进制表示的分钟数 [,].
%S 十进制的秒数
%z 时区名称
%a 星期的简称 ,Sun
%A 星期的全称 ,Sunday
%b 月份的简称,Mar
%B 月份的全称,March
%I 12小时制的小时 [,].
%p 本地的AM或PM的等价显示
"""

修改测试脚本:

 if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(TestBaidu("test_baidu"))
4 now_time = time.strftime("%Y-%m-%d %H_%M_%S")
5 file_name = "./"+now_time+"result.html"
6 file = open(file_name, "wb")
runner = HTMLTestRunner(stream=file, title="百度搜索测试报告",description="用例执行情况:")
runner.run(suite)
file.close()

然后运行测试脚本,即可生成不同名称的测试报告。

4.输出项目集成测试报告

修改runnertest.py文件,使其可以生成项目集成测试报告。

 # coding:utf-
import unittest
from HTMLTestRunner import HTMLTestRunner
import time discover = unittest.defaultTestLoader.discover("./testpro/testcase/", "test*.py")
if __name__ == '__main__': 9 now_time = time.strftime("%Y-%m-%d %H-%M-%S")
10 file_name = "./testpro/testresult/"+now_time+"result.html"
11 file = open(file_name, "wb")
12 runner = HTMLTestRunner(stream=file, title="测试报告", description="测试用例执行情况")
runner.run(discover)

执行测试脚本,查看生成的测试报告:

生成HTML测试报告的更多相关文章

  1. Python用HTMLTestRunner生成html测试报告

    小编的主机:mac 一.引入HTMLTestRunner包 1.下载HTMLTestRunner.py,已上传到网盘,点击下载 2.将HTMLTestRunner.py复制到python安装目录的Li ...

  2. Testng生成的测试报告乱码解决办法

    Testng生成的测试报告乱码解决办法 2017-06-16 1 问题描述 乱码是程序编码不统一,比如Java源代码是utf-8,编译是gbk,这时会乱码. 代码如下: org.testng.Repo ...

  3. 转 生成 HTMLTestRunner 测试报告

    转自:http://www.cnblogs.com/hero-blog/p/4128575.html 04.生成 HTMLTestRunner  测试报告   1.HTMLTestRunner 是 P ...

  4. Python+Selenium----使用HTMLTestRunner.py生成自动化测试报告2(使用PyCharm )

    1.说明 在我前一篇文件(Python+Selenium----使用HTMLTestRunner.py生成自动化测试报告1(使用IDLE ))中简单的写明了,如何生产测试报告,但是使用IDLE很麻烦, ...

  5. Python+Selenium----使用HTMLTestRunner.py生成自动化测试报告1(使用IDLE)

    1.说明 自动化测试报告是一个很重要的测试数据,网上看了一下,使用HTMLTestRunner.py生成自动化测试报告使用的比较多,但是呢,小白刚刚入手,不太懂,看了很多博客,终于生成了一个测试报告, ...

  6. python+selenium +unittest生成HTML测试报告

    python+selenium+HTMLTestRunner+unittest生成HTML测试报告 首先要准备HTMLTestRunner文件,官网的HTMLTestRunner是python2语法写 ...

  7. Python3和HTMLTestRunner生成html测试报告

    1.测试环境: Python3.5+unittest+HTMLTestRunner 2.下载HTMLTestRunner.py文件 下载地址 http://tungwaiyip.info/softwa ...

  8. Python&Selenium&pytest借助allure生成自动化测试报告

    一.摘要 本篇博文将介绍Python和Selenium进行自动化测试时,如何借助allure生成自动化测试报告 二.环境配置 首先python环境中安装pytest和pytest_allure_ada ...

  9. Python&Selenium借助HTMLTestRunner生成自动化测试报告

    一.摘要 本篇博文介绍Python和Selenium进行自动化测试时,借助著名的HTMLTestRunner生成自动化测试报告 HTMLTestRunner.py百度很多,版本也很多,自行搜索下载放到 ...

随机推荐

  1. SizeGripStyle 枚举

    成员名称 说明  Auto 需要时会自动显示大小调整的箭头图标    Hide 大小调整的箭头图标被隐藏. (SizeGripStyle=Hide,禁用拖动窗体右下角可以改变大小的功能)    Sho ...

  2. 第六章 深入分析ClassLoader工作机制

    补充(非书中): Java 源程序(.java 文件)在经过 Java 编译器编译之后就被转换成 Java 字节代码(.class 文件).类加载器负责读取Java字节代码,并转换成 java.lan ...

  3. java面试(6)

    1  六大原则 详情参考:设计模式六大原则(转载). 2  UML类之间关系有几种?聚合和组合区别? 类之间可能存在以下几种关系:关联(association).依赖(dependency).聚合(A ...

  4. resharper activate

    K03CHKJCFT-eyJsaWNlbnNlSWQiOiJLMDNDSEtKQ0ZUIiwibGljZW5zZWVOYW1lIjoibnNzIDEwMDEiLCJhc3NpZ25lZU5hbWUiO ...

  5. Python3.x 常用的新特性

    Python3.x 常用的新特性 print() 是函数,不是一个语句 raw_input()输入函数,改为 input() Python 3 对文本和二进制数据做了更为清晰的区分. 文本由unico ...

  6. asp:GridView控件的使用

    使用asp:GridView显示一个统计的表格 cs样式: <style>        table.gridview_m        {            border-colla ...

  7. cxf和axis2使用有感

    CXF框架 个人不喜欢使用wsimport工具: 1.考虑到远端的服务接口发生变化,本地的接口还需要重新同步下 2.项目中无端多了些冗余的代码 这样我们选择cxf的动态调用接口吧,使用DynamicC ...

  8. Timer的缺陷

  9. js如何解析后台传过来的json字符串

    1.js如何解析后台传过来的json字符串? 注意:js是无法直接接收和使用json或者Php的数据,用的话会出现undefined,所以要转换一下. 方式一: var str = '{"r ...

  10. codeforce452DIV2——E. Segments Removal

    题目 Vasya has an array of integers of length n. Vasya performs the following operations on the array: ...