下载 HTMLTestRunner 模块

下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html

保存路径:将下载的HTMLTestRunner.py文件复制到Python安装路径下的Lib目录

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

修改HTMLTestRunner(针对Python3)

因为HTMLTestRunner是基于Python2开发的,如果在Python3环境下使用,需要对其部分内容进行修改。使用notepad++打开HTMLTestRunner.py文件,修改如下内容:

第94行,将 import StringIO修改成import io

第539行,将 self.outputBuffer = StringIO.StringIO()修改成self.outputBuffer= io.StringIO()

第631行,将 print >> sys.stderr, ‘\nTime Elapsed: %s‘  %(self.stopTime-self.startTime)修改成print(sys.stderr, ‘\nTimeElapsed: %s‘ % (self.stopTime-self.startTime))

第642行,将 if not rmap.has_key(cls):修改成 if not cls in rmap:

第766行,将 uo = o.decode(‘latin-1‘)修改成 uo = e

第772行,将 ue = e.decode(‘latin-1‘)修改成 ue = e

一个简单的例子

# -*- coding: utf-8 -*-
from selenium import webdriver
from HTMLTestRunner import HTMLTestRunner #导入HTMLTestRunner模块
import unittest,time class BaiduIdeTest(unittest.TestCase):
#三引号表示doc string类型注释,用来描述函数、类和方法
'''baidu search testing'''
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.baidu.com/" def test_baidu_ide(self):
'''Search Keyword'''
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys("HTMLTestRunner")
driver.find_element_by_id("su").click()
time.sleep(5)
self.assertEqual(u"HTMLTestRunner_百度搜索", driver.title) def tearDown(self):
self.driver.quit() if __name__ == "__main__":
#构造测试套件
testsuit = unittest.TestSuite()
testsuit.addTest(BaiduIdeTest("test_baidu_ide"))
#按照一定格式获取当前时间,%Y表示带世纪的年(2019),%y表示不带世纪的年(19),time.strftime()表示获得当前时间并格式化字符串
    now = time.strftime("%Y%m%d_%H%M%S")
    #将当前时间加入到报告文件名称中
    filename = './'+now+'result.html'
#定义测试报告存放路径,通过open()方法以二进制写模式('wb')打开当前目录下的result.heml,如果没有,则自动创建。
fp = open('./result.html','wb')
#定义测试报告,调用HTMLTestRunner模块下的HTMLTestRunner类,stream 指定测试报告文件,title 定义测试报告的标题,description 定义测试报告的副标题
runner = HTMLTestRunner(stream=fp,title='自动化测试报告',description='用例执行情况:')
#通过HTMLTestRunner的run()方法来运行测试套件中的测试用例
runner.run(testsuit)
#关闭测试报告
fp.close()
你可以根据测试需要添加多个测试,例如再新加一个类以及测试方法,然后通过 testsuit.addTest 把类和方法加进去,如果需要添加的测试比较多,可以通过discover方法,例子如下:
from unittest import defaultTestLoader
case_path = './TestCases' 
def get_allcase():
discover = unittest.defaultTestLoader.discover(case_path, pattern="test_Create_GL_Indicative_Quoted.py")
testsuite = unittest.TestSuite()
testsuite.addTest(discover)
return testsuite if __name__ == "__main__":
now = time.strftime("%Y%m%d_%H%M%S")
filename = 'C:\MIG_Portal_Automation\TestReports/' + now + 'result.html'
fp = open(filename, 'wb')
runner = HTMLTestRunner(stream=fp, title='automation report', description='case execution status')
runner.run(get_allcase())
fp.close()
 

Python+Selenium之HTMLTestRunner的更多相关文章

  1. Python Selenium unittest+HTMLTestRunner实现 自动化测试及发送测试报告邮件

    1.UI测试框架搭建-目录结构 2. 文件介绍 2.1.baseinfo->__init__.py 配置文件定义基础参数 #-*-coding:utf-8-*- #测试用例配置参数 base_u ...

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

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

  3. Python&Selenium借助html-testRunner生成自动化测试报告

    一.摘要 本博文将介绍Python和Selenium进行自动化测试时,借助html-testRunner 生成自动化测试报告 安装命令:pip install html-testRunner 二.测试 ...

  4. Python+Selenium+Unittest+HTMLTestRunner生成测试报告+发送至邮箱,记一次完整的cnblog登录测试示例,

    测试思路:单个测试集.单个测试汇成多个测试集.运行测试集.生成测试报告.发送至邮箱. 第一步:建立单个测试集,以cnblog登录为例. 测试用例: cnblog的登录测试,简单分下面几种情况:(1)用 ...

  5. 【转】【Python + selenium】linux和mac环境,驱动器chromedriver和测试报告HTMLTestRunner放置的位置

    感谢: 作者:gz_tester,文章:<linux和mac环境,chromedriver和HTMLTestRunner放置的位置> 使用场景 配置python selenium 环境 使 ...

  6. Python Selenium设计模式-POM

    前言 本文就python selenium自动化测试实践中所需要的POM设计模式进行分享,以便大家在实践中对POM的特点.应用场景和核心思想有一定的理解和掌握. 为什么要用POM 基于python s ...

  7. python+selenium 自动化测试实战

    一.前言: 之前的文章说过, 要写一篇自动化实战的文章, 这段时间比较忙再加回家过11一直没有更新博客,今天整理一下实战项目的代码共大家学习.(注:项目是针对我们公司内部系统的测试,只能内部网络访问, ...

  8. python selenium 测试环境的搭建及python mysql的连接

    又来一篇傻瓜教程啦,防止在学习的小伙伴们走弯路. 1.python 环境搭建 python官网:https://www.python.org/downloads/  选择最新版本python下载(如果 ...

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

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

随机推荐

  1. Tomcat与Web.xml配置

    1.编码配置 <Connector acceptCount=”100″ connectionTimeout=”20000″ disableUploadTimeout=”true” enableL ...

  2. whereis libjpeg.so.7

    在服务器上调用import ImageFont时报如下错误 ImportError: The _imagingft C module is not installed (服务器为Centos5.5, ...

  3. C#中如何向数组中动态添加元素

    转自:https://blog.csdn.net/qq_35938548/article/details/78325558 背景:现需要向数组中循环插入字符串,但C#中的数组是不支持动态添加元素的,只 ...

  4. swagger接口文档

    1 在Visual Studio 中创建一个Asp.NET  WebApi 项目,项目名:Com.App.SysApi(本例创建的是 .net 4.5 框架程序) 2  打开Nuget 包管理软件,查 ...

  5. Regex 常用的正则表达式

    .校验数字的表达式 数字:^[0-9]*$ n位的数字:^\d{n}$ 至少n位的数字:^\d{n,}$ m-n位的数字:^\d{m,n}$ 零和非零开头的数字:^(0|[1-9][0-9]*)$ 非 ...

  6. 离线安装 python 第三方库

     离线安装 python 第三方库 首先你需要在联网的服务器上已经安装了一个第三方库,比如是paramiko,也就是说你已经执行了 pip install paramiko    ,小提示: 如果在安 ...

  7. 【bzoj4836】二元运算 分治FFT

    Description 定义二元运算 opt 满足 现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问.每次询问给定一个数字 c 你需要求出有多少对 (i, j) 使 ...

  8. 题解 P4140 【奇数国 】

    题目链接 首先,按照题意,把前$60$个素数打出来$[2$ $-$ $281]$. 因为只有$60$个,再加上本宝宝极其懒得写线性筛于是每一个都$O(\sqrt{n})$暴力筛就好了. 代码如下: # ...

  9. 题解 P1720 【月落乌啼算钱】

    题目链接 定义一个函数比较好求. #include<bits/stdc++.h>//万能头文件 using namespace std; double F(int x)//定义函数,为了保 ...

  10. loj #6046. 「雅礼集训 2017 Day8」爷

    #6046. 「雅礼集训 2017 Day8」爷 题目描述 如果你对山口丁和 G&P 没有兴趣,可以无视题目背景,因为你估计看不懂 …… 在第 63 回战车道全国高中生大赛中,军神西住美穗带领 ...