在之前的案例中,我们完成了自劢化测试的基本能力,也能完成测试的执行工作,但 是还没有做到将测试的结果以报表的形式输出,接下来,在之前的测试基础上加上测试报告的输出。
HTMLTestRunner 介绍
HTMLTestRunner 是 python 标准库 unittest 的扩展,可以生成一个直观的测试报告。 在使用之前需要将 HTMLTestRunner.py 文件放如到 python 的安装目弽下,例如我的 就是 c:\Python27 目录。
生成测试报告
下我们在之前的 LMD 登陆测试的脚本中先来看看 HTMLTestRunner 是如何使用的,将 login_auto.py 的内容修改如下:
#coding=utf-8
__author__ = 'Administrator'
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
import time
import unittest
import HTMLTestRunner
class login_test_case(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.maximize_window()
self.driver.get('http://www.chuangyijia.com/login')
def tearDown(self):
self.driver.quit()
def test_login(self):
self.driver.find_element_by_id('email').send_keys('810155067@
qq.com')
self.driver.find_element_by_id('pwd').send_keys('a654321')
self.driver.find_element_by_id('submit').click()
#self.driver.implicitly_wait(5)
time.sleep(3)
WebDriverWait(self.driver,30).until(expected_conditions.visib
ility_of_element_located((By.CSS_SELECTOR,'.logo')))
print self.driver.title
is_title = expected_conditions.title_is(u'首页-创意家')
self.assertTrue(is_title(self.driver))
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(login_test_case("test_login"))
Report_file =
u"H:\\pydj\\Lmd_auto_test\\Report\\Result.html"
Rf = file(Report_file,'wb')
Case_run =
HTMLTestRunner.HTMLTestRunner(stream=Rf,title=u'LMD 登陆测试
',description=u"测试报告输出")
Case_run.run(suite)
上面的代码叧是在原有基础上做了修改,加入了 import HTMLTestRunner 这句,还有 后面的
suite = unittest.TestSuite()
创建一个测试套对象
suite.addTest(login_test_case("test_login"))
将登陆的测试用例添加到测试套中
Report_file = u"H:\\pydj\\Lmd_auto_test\\Report\\Result.html"
设置测试报告输出的位置及文件名
49
Rf = file(Report_file,'wb')
使用 python 标准库 file 打开测试报告文件,wb 是以二进制写的模式
打开。
Case_run=HTMLTestRunner.HTMLTestRunner(stream=Rf,title=u'LMD
登陆测试',description=u"测试报告输出")
创建一个 HTMLTestRunner 的对象,并且将上面打开的用于输出测试报
告的对象传入,title 是 html 报告页面的 title,description 对测试
报告的描述
Case_run.run(suite)
开始运行测试套
最后感谢每一个认真阅读我文章的人,下面这个网盘链接也是我费了几天时间整理的非常全面的,希望也能帮助到有需要的你!
这些资料,对于想转行做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助……
如果你不想一个人野蛮生长,找不到系统的资料,问题得不到帮助,坚持几天便放弃的感受的话,可以点击下方小卡片加入我们群,大家可以一起讨论交流,里面会有各种软件测试资料和技术交流。
点击文末小卡片领取 |
敲字不易,如果此文章对你有帮助的话,点个赞收个藏来个关注,给作者一个鼓励。也方便你下次能够快速查找。
自学推荐B站视频:
零基础转行软件测试:25天从零基础转行到入职软件测试岗,今天学完,明天就业。【包括功能/接口/自动化/python自动化测试/性能/测试开发】