一、生成报告

1.先执行一个用例,并生成该用例的报告

# -*- 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,HTMLTestRunner class Baidy(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_baidy(self):
u"""百度搜索"""
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys("selenium webdriver")
driver.find_element_by_id("su").click()
time.sleep(2)
driver.close() def test_baidu_set(self):
u"""百度设置"""
driver = self.driver
# 进入搜索设置页面
driver.get(self.base_url+'/gaoji/preferences.html') menu = driver.find_element_by_id("nr")
menu.find_element_by_xpath(".//option[@value='20']").click()
driver.find_element_by_id("save").click()
driver.switch_to_alert().accept()
time.sleep(2)
driver.close() def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors) if __name__ == "__main__":
# 定义一个单元测试容器
testunit = unittest.TestSuite()
testunit.addTest(Baidy.test_baidy)
testunit.addTest(Baidy.test_baidu_set) # 定义报告存放路径
filename = r'E:\abc\web_testing\result.html'
fp = file(filename, 'wb') # 定义测试报告
runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,
title=u'百度搜索测试报告',
description=u'用例执行情况:') # 运行测试用例
runner.run(testunit)
fp.close()

2.批量执行测试用例,先构造两个用例

# -*- 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,HTMLTestRunner class Baidy(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_baidy(self):
u"""百度搜索"""
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys("selenium webdriver")
driver.find_element_by_id("su").click()
time.sleep(2)
driver.close() def test_baidu_set(self):
u"""百度设置"""
driver = self.driver
# 进入搜索设置页面
driver.get(self.base_url+'/gaoji/preferences.html') menu = driver.find_element_by_id("nr")
menu.find_element_by_xpath(".//option[@value='20']").click()
driver.find_element_by_id("save").click()
driver.switch_to_alert().accept()
time.sleep(2)
driver.close() def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors) if __name__ == "__main__":
unittest.main()

baidy.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 Youdao(unittest.TestCase): def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://www.youdao.com"
self.verificationErrors = []
self.accept_next_alert = True def test_youdao_search(self):
u"""有道搜索"""
driver = self.driver
driver.get(self.base_url + "/")
# driver.find_element_by_id("query").clear()
# driver.find_element_by_id("query").send_keys(u"你好")
# driver.find_element_by_id("qb").click()
driver.find_element_by_id("translateContent").clear()
driver.find_element_by_id("translateContent").send_keys(u"你好")
driver.find_element_by_xpath(".//*[@id='form']/button").click()
time.sleep(2)
driver.close() def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors) if __name__ == "__main__":
unittest.main()

youdao.py

并构造测试套件:根据套件来执行这两个文件的几条用例(baidu.py两条用例,youdao.py三条用例)

代码:

# -*- coding:utf-8 -*-
import unittest
import baidy,youdao
import HTMLTestRunner
import time testunit = unittest.TestSuite() # 将测试用例加入到测试容器(套件)中
testunit.addTest(unittest.makeSuite(baidy.Baidy))
testunit.addTest(unittest.makeSuite(youdao.Youdao)) # 执行测试套件
# runner = unittest.TextTestRunner()
# runner.run(testunit) # 定义报告存放路径,支持相对路径
filename = r"E:\abc\web_testing\test_case\all_test.html"
fp = file(filename, 'wb') runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,
title=u'百度搜索测试报告',
description=u'用例执行情况') # 执行测试用例
runner.run(testunit)
fp.close()

all_test.py

注意,报告的基本格式如下:

#  定义报告存放路径,支持相对路径
filename = r"E:\abc\web_testing\test_case\all_test.html"
fp = file(filename, 'wb') runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,
title=u'百度搜索测试报告',
description=u'用例执行情况') # 执行测试用例
runner.run(testunit)
fp.close()

生成的报告如下:

二、报告文件取当前的时间

在all_test.py中引入time,即import time,几个time的常用函数:

time.time() 获取当前时间戳
time.localtime() 当前时间的 struct_time 形式
time.ctime() 当前时间的字符串形式
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

将all_test.py文件改为:

...
# 取前面时间
now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) # 定义报告存放路径,支持相对路径
filename = r"E:\abc\web_testing\test_case"+ now +'all_test.html'
fp = file(filename, 'wb') runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,
title=u'百度搜索测试报告',
description=u'用例执行情况')
...

执行用例,即报错:

IOError: [Errno 22] invalid mode ('wb') or filename: 'E:\\abc\\web_testing\\test_case2016-12-21 19:43:20all_test.html'

这是因为时间不能用“:”冒号

now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))

将它改成:

now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))

即可

以上内容感谢虫师的相关博客和书籍

python自动化--批量执行测试之生成报告的更多相关文章

  1. 通过SqlClr制作Sql自动化批量执行脚本

    原文:通过SqlClr制作Sql自动化批量执行脚本 通过SqlClr制作Sql自动化批量执行脚本 在与同事一起做项目时,看到同事用sqlclr做批量执行脚本,感觉挺新奇的就上网搜集资料自己模仿跟做了个 ...

  2. python 分析慢查询日志生成报告

    python分析Mysql慢查询.通过Python调用开源分析工具pt-query-digest生成json结果,Python脚本解析json生成html报告. #!/usr/bin/env pyth ...

  3. python自动化之使用allure生成测试报告

    Allure测试报告框架帮助你轻松实现"高大上"报告展示.本文通过示例演示如何从0到1集成Allure测试框架.重点展示了如何将Allure集成到已有的自动化测试工程中.以及如何实 ...

  4. Selenium2+python自动化52-unittest执行顺序

    前言 很多初学者在使用unittest框架时候,不清楚用例的执行顺序到底是怎样的.对测试类里面的类和方法分不清楚,不知道什么时候执行,什么时候不执行. 本篇通过最简单案例详细讲解unittest执行顺 ...

  5. Selenium2+python自动化52-unittest执行顺序【转载】

    前言 很多初学者在使用unittest框架时候,不清楚用例的执行顺序到底是怎样的.对测试类里面的类和方法分不清楚,不知道什么时候执行,什么时候不执行. 本篇通过最简单案例详细讲解unittest执行顺 ...

  6. python +selenium 自带case +生成报告的模板

    https://github.com/huahuijay/python-selenium2这个就是 python +selenium的 里面还自带case 然后也有生成报告的模板 我的: https: ...

  7. python+selenium 批量执行时出现随机报错问题【已解决】

    出现场景:用discover方法批量执行py文件,出现随机性的报错(有时a.py报错,有时b.py报错...),共同特点:均是打开新窗口后,切换最新窗口,但定位不到新窗口的元素,超时报错.由于个人项目 ...

  8. Python实现批量执行华为交换机脚本

    #!/usr/bin/python3 # -*- coding:utf-8 -*- import paramiko import time ssh = paramiko.SSHClient() key ...

  9. Appium+Python之批量执行测试用例

    思考:当存在多个脚本,每个脚本中有多条测试用例时,我们该如何批量执行呢?分析:首先创建2个测试用例脚本(.py文件),每个脚本有2条测试用例,然后批量执行全部测试用例 #Test_01.py # co ...

随机推荐

  1. java内存模型JMM理解整理

    什么是JMM JMM即为JAVA 内存模型(java memory model).因为在不同的硬件生产商和不同的操作系统下,内存的访问逻辑有一定的差异,结果就是当你的代码在某个系统环境下运行良好,并且 ...

  2. 洛谷 2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

    一道水状压,然而不知道是不是太久没做过dp了,我盯着它二十分钟才反应过来.... 还把数组开小了WA了一发QAQ //Twenty #include<algorithm> #include ...

  3. pycharm优化

    一.版本选择 建议安装5.0版本,因为好注册,这个你懂得. 下载地址: https://confluence.jetbrains.com/display/PYH/Previous+PyCharm+Re ...

  4. 服务器IP配置功能实现小结

    1. 服务器网卡配置文件 /etc/sysconfig/network/ifcfg-***(eth0) linux-f1s9:/etc/sysconfig/network # cat ifcfg-et ...

  5. js时间操作getTime(),ios移动端真机上返回显示NAN

    ios移动端,js时间操作getTime(),getFullYear()等返回显示NaN的解决办法及原因 在做移动端时间转化为时间戳时,遇到了一个问题,安卓手机上访问时,能拿到时间戳,从而正确转换时间 ...

  6. 学而有道--思维导图式总结(一):Nosql分类

    前言: 众所周知,学习是需要方法的.作为一名java程序员,我们需要学习无数的技能,然而我们的大脑并不买账,学习了一项知识,时间一久就会遗忘, 如何更好高效的回忆起曾经学习过的知识,是极其重要的. 有 ...

  7. scrapy中的ImagePipeline下载图片到本地、并提取本地的保存地址

    通过scrapy内置到ImagePipeline下载图片到本地 在settings中打开 ITEM_PIPELINES的注释,并在这里面加入 'scrapy.pipelines.images.Imag ...

  8. Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历

    给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...

  9. 01.Hibernate快速入门

    第一步:下载Hibernate5的运行环境 https://sourceforge.net/projects/hibernate/files/hibernate-orm/ 第二步:在数据库创建表 Cr ...

  10. 通过三个DEMO学会SignalR的三种实现方式 转载https://www.cnblogs.com/zuowj/p/5674615.html

    一.理解SignalR ASP .NET SignalR 是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信(即:客户端(Web页面)和服务器端可以互相实时的通知消息 ...