selenium+Python(生成html测试报告)

当自动化测试完成后,我们需要一份漂亮且通俗易懂的测试报告来展示自动化测试成果,仅仅一个简单的log文件是不够的

HTMLTestRunner是Python标准库unittest单元测试框架的一个扩展,它生成易于使用的HTML测试报告,下载后,将其复制到Python的安装目录即可,

例如,Windows,放在...\python27\Lib目录下

补充知识:

1、Python注释

普通注释用#表示

文本注释,放在类或者方法下面:""" 注释内容 """或者  ‘‘‘注释内容  ‘‘‘

2、测试报告以测试时间来命名,防止报告被覆盖

time.time():获取当前时间戳

time.ctime():当前时间的字符串形式

time.localtime():当前时间的struct_time形式

time.strftime():用来获取当前时间,可以讲师时间格式化为字符串

下面是项目集成生成测试报告源码:

test_case文件下的两个测试用例

test_baidu.py

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Baidu(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.baidu.com/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_baidu(self):
        u"""百度搜索用例"""
        driver = self.driver
        driver.get(self.base_url + "/?tn=98012088_5_dg&ch=12")
        driver.find_element_by_id("kw").clear()
        driver.find_element_by_id("kw").send_keys("select")
        driver.find_element_by_id("su").click()

        time.sleep(3)

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

  test_firefox.py

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class FireFox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://start.firefoxchina.cn/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_firefox(self):
        u"""Firefox搜索用例"""
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("search-key").clear()
        driver.find_element_by_id("search-key").send_keys("selenium webdriver")
        driver.find_element_by_id("search-submit").click()
        time.sleep(5)

    def tearDown(self):
        self.driver.quit()
if __name__ == "__main__":
    unittest.main()

  执行测试用例的run_test.py

#coding=utf-8
import unittest, doctest
import HTMLTestRunner
import time
#相对路径定义用例存放的路径和报告存放路径
test_dir=‘./test_case‘
test_dir1=‘./report‘#查找对应路径下的测试用例放到discover中
discover=unittest.defaultTestLoader.discover(test_dir,pattern=‘test*.py‘)
#定义带有当前测试时间的报告,防止前一次报告被覆盖
now=time.strftime("%Y-%m-%d %H_%M_%S")
filename=test_dir1+ ‘/‘ +now+ ‘result.html‘
#二进制打开,准备写入文件
fp = file(filename, ‘wb‘)
#定义测试报告
runner =HTMLTestRunner.HTMLTestRunner(
	stream=fp,
	title=u‘搜索测试报告‘,
	description=u‘用例执行情况‘)
runner.run(discover)

  最后生成的生成的html测试报告如下:

时间: 2024-08-28 16:45:06

selenium+Python(生成html测试报告)的相关文章

selenium+python之HTML测试报告

r一.准备 1.本节用到的模块--HTMLTestRunner 1)下载:下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html 鼠标右键→目标另存为,保存到本地. 2)安装:将下载的HTMLTestRunner.py文件复制到Python安装目录下即可 ...\Python36\Lib 3)验证:在Python交互模式下引入HTMLTestRunner模块,如系统没有报错,则说明添加成功 2.修改HTMLTestRunner 因为HTM

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

1.说明 在我前一篇文件(Python+Selenium----使用HTMLTestRunner.py生成自动化测试报告1(使用IDLE ))中简单的写明了,如何生产测试报告,但是使用IDLE很麻烦,而且在实际的项目中也不方便,所以,查了很多资料来研究如何在PyCharm中生成测试报告,故此记录一下(命名什么的不规范就不要纠结了). 2.步骤 第一步:下载HTMLTestRunner.py 参考:Python+Selenium----使用HTMLTestRunner.py生成自动化测试报告1(使

selenium+python+unittest多线程生成报告(BeautifulReport)

前言 selenium多线程跑用例,这个前面一篇已经解决了,如何生成一个测试报告这个是难点,刚好在github上有个大神分享了BeautifulReport,完美的结合起来,就能生成报告了. 环境必备: python3.6 : BeautifulReport不支持2.7 tomorrow : pip install tomorrow安装 BeautifulReport : github下载后放到/Lib/site-packages/目录下 BeautifulReport 1.BeautifulR

Python 同一文件中,有unittest不执行“if __name__ == '__main__”,不生成HTMLTestRunner测试报告的解决方案

1.问题:Python中同一个.py文件中同时用unittest框架和HtmlReport框架后,HtmlReport不被执行. 2.为什么?其实不是HtmlReport不被执行,也不是HtmlReport不生成测试报告,是因为if __name__ == '__main__'中的代码根本没执行好嘛! 3.解决方案的来源:因为最开始我的main代码中没有写print打印语句.没有生成HTML报告,我也在网上找了很久的方法,后来才怀疑是不是没有运行main方法,于是写了个print语句,果然没有运

Python用HTMLTestRunner生成html测试报告

小编的主机:mac 一.引入HTMLTestRunner包 1.下载HTMLTestRunner.py,已上传到网盘,点击下载 2.将HTMLTestRunner.py复制到python安装目录的Lib文件夹下. 可能有的人不知道python安装地址的Lib文件夹在哪里.小编用的是mac,放的地址为:/资源库/Frameworks/Python.framework/Versions/3.6/lib/python3.6 因为看见里面有很多.py结尾的文件,常用到的os.py都在里面,放这里准没错.

selenium + python自动化测试unittest框架学习(二)

1.unittest单元测试框架文件结构 unittest是python单元测试框架之一,unittest测试框架的主要文件结构: File >report >all_case.py >test_case >__init__.py >test_case1..... >public >__init__.py >login.py >loginout.py test_case文件夹主要存放测试用例,且测试用例命名以test_开头 public文件夹是test

转 生成 HTMLTestRunner 测试报告

转自:http://www.cnblogs.com/hero-blog/p/4128575.html 04.生成 HTMLTestRunner  测试报告 1.HTMLTestRunner 是 Python 标准库的 unittest 模块的一个扩展.它生成易于使用的 HTML 测试报告 1>下载HTMLTestRunner.py文件,地址为: http://tungwaiyip.info/software/HTMLTestRunner.html Windows平台: 将下载的文件放入...\P

Selenium2 + Python3.6实战(五):生成HTML测试报告 Invalid argument

今天在学习自动化测试的高级应用,第一篇就是生成HTML测试报告,由于测试报告的名称没有进行设置,所以每次运行测试之前若没有手动修改之前的报告名称,运行时就会把原来的报告覆盖.这样做显然很麻烦,最好的解决办法就是在报告名称中加入当前时间,这样生成的报告既不会重叠,又能更清晰地知道报告生成的时间. 但是却在运行代码的过程中,出现了报错: Traceback (most recent call last):  File "D:/Python/selenium/test_baidu.py",

selenium + python自动化测试unittest框架学习(一)selenium原理及应用

unittest框架的学习得益于虫师的<selenium+python自动化实践>这一书,该书讲得很详细,大家可以去看下,我也只学到一点点用于工作中,闲暇时记录下自己所学才能更加印象深刻.unittest框架学习需要掌握以下知识点: (1)Selenium原理及工具使用 (2)webdriver元素定位,id,name,class name,css seletor,xpath (3)python语言基础,模块,参数化,语言,装饰器 (4)unittest框架认识及学习:模块化,断言,参数化,数