【如何学习Python自动化测试】—— HTMLTestRunner 生成测试报告

11 、HTMLTestRunner 生成测试报告

在之前的案例中,我们完成了自动化测试的基本能力,也能完成测试的执行工作,但是还没有做到将测试的结果以报表的形式输出,接下来,在之前的测试基础上加上测试报告的输出。

11.1 HTMLTestRunner 介绍

HTMLTestRunner是Python编程语言中的一个第三方库,它提供了一个易于使用,易于阅读和易于分享的HTML测试报告。该库适用于运行Python单元测试和集成测试,报告包括每个测试的状态,每个测试的运行时间,每个测试的失败原因等信息,并且可以将这些信息以HTML格式输出以方便查看和分享。这个库的优点是可以使测试报告易于理解,看起来更美观,帮助测试人员更准确和高效地检查测试结果。

11.2 生成测试报告

注意

  • 先安装HTMLTestRunner库: pip install HTMLTestRunner

  • HTMLTestRunner的路径必须是Python的搜索路径中,一般情况下,Python会自动搜索当前目录和Python的标准库。

  • HTMLTestRunner仅支持Python2.x,如果使用Python3.x需要安装HTMLTestRunner_PY3库。

我们在之前的 LMD 登陆测试的脚本中先来看看 HTMLTestRunner 是如何使用的,将 login_auto.py 的内容修改如下:

python 复制代码
#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.visibility_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 这句,还有后面的

python 复制代码
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')
使用 python 标准库 file 打开测试报告文件, wb 是以二进制写的模式打开。

Case_run=HTMLTestRunner.HTMLTestRunner(stream=Rf,title=u'LMD 登陆测试',description=u"测试报告输出")
创建一个 HTMLTestRunner 的对象,并且将上面打开的用于输出测试报 告的对象传入,title 是 html 报告页面的 title,description 对测试 报告的描述

Case_run.run(suite)
开始运行测试套
相关推荐
凉、介4 小时前
Armv8-A virtualization 笔记 (二)
笔记·学习·嵌入式·arm·gic
踩着两条虫5 小时前
「AI + 低代码」的可视化设计器
开发语言·前端·低代码·设计模式·架构
JoneBB5 小时前
ABAP Webservice连接
运维·开发语言·数据库·学习
scan7245 小时前
智能体多个工具调用
python
2401_867623985 小时前
CSS Flex布局中如何设置子元素间距_掌握gap属性的现代用法
jvm·数据库·python
即使再小的船也能远航5 小时前
【Python】安装
开发语言·python
weixin_421725265 小时前
Linux 编程语言全解析:C、C++、Python、Go、Rust 谁更强?
linux·python·go·c·编程语言
Irissgwe6 小时前
类与对象(三)
开发语言·c++·类和对象·友元
没有梦想的咸鱼185-1037-16636 小时前
AI-Python机器学习、深度学习核心技术与前沿应用及OpenClaw、Hermes自动化编程
人工智能·python·深度学习·机器学习·chatgpt·数据挖掘·数据分析
嵌入式小企鹅6 小时前
UiPath推出AI编程“总指挥台”,SiFive发布RISC-V第三代猛兽
人工智能·学习·google·程序员·ai编程·risc-v·开源工具