如何生成HTMLTestRunner测试报告。接上篇文章,对于unittest框架,运行后,测试结果不便于查看,同时多个case存在的时候,可能会导致case result记录不正确的情况。

为此,引入了HTMLTestRunner.py,它是Python标准库unittest模块的一个扩展。它可以生成直观的HTML测试报告。

一、下载HTMLTestRuner

首先,下载HTMLTestRuner.py文件。

下载地址:Python2.7版本:http://tungwaiyip.info/software/HTMLTestRunner.html

Python3.0版本:https://github.com/huilansame/HTMLTestRunner_PY3

二、生成HTMLTestRuner报告

三、测试报告详情

四、参考代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : chen
# @File : run_main.py
# @Software: PyCharm
import os
import unittest
import time
import HTMLTestRunner
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart cur_path = os.path.dirname(os.path.realpath(__file__)) def add_case(caseName='case',rule='test*.py'):
case_path = os.path.join(cur_path, caseName)
if not os.path.exists(case_path):
os.mkdir(case_path)
print('test case path:%s' %case_path)
discover = unittest.defaultTestLoader.discover(case_path,
pattern=rule,
top_level_dir=None)
print(discover)
return discover def run_case(all_case,reportName='report'):
now = time.strftime('%Y_%m_%d_%H_%M_%S')
report_path = os.path.join(cur_path, reportName)
if not os.path.exists(report_path):
os.mkdir(report_path)
report_abspath = os.path.join(report_path, now + 'result.html')
print('test case path:%s' %report_abspath)
fp = open(report_abspath, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
title='自动化测试报告:',
description='用例执行情况')
runner.run(all_case)
fp.close() def get_report_file(report_path):
lists = os.listdir(report_path)
lists.sort(key=lambda fn: os.path.getmtime(os.path.join(report_path, fn)))
print('最新测试生成报告:' + lists[-1])
report_file = os.path.join(report_path, lists[-1])
return report_file def send_mail(sender,password,receiver,smptserver,report_file,port):
with open(report_file, 'rb') as f:
mail_body = f.read() msg = MIMEMultipart()
body = MIMEText(mail_body,_subtype='html',_charset='utf-8')
msg["subject"] = "自动化测试报告"
msg["from"] = sender
msg["to"] = receiver
msg.attach(body) att = MIMEText(open(report_file,'rb').read(), 'base64', 'utf-8')
att['Content-Type'] = 'application/octet-stream'
att['Content-Disposition'] = 'attachment; filename="report.html"'
msg.attach(att)
try:
smtp = smtplib.SMTP_SSL(smptserver,port)
except:
smtp = smtplib.SMTP()
smtp.connect(smptserver,port) smtp.login(sender,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
print('test report email has send out !') if __name__ == "__main__":
all_case = add_case()
run_case(all_case)
report_path = os.path.join(cur_path,'report')
report_file = get_report_file(report_path) from jiekou_test.config import readConfig
sender = readConfig.sender
password = readConfig.password
smtp_server = readConfig.smtp_server
port = readConfig.port
receiver = readConfig.receiver
send_mail(sender,password,receiver,smtp_server,report_file,port)
写在最后的话:这些都是小编自己一个字一个字敲上去的,原创算不上,可能很多类似的资料,小编写这个的目的是为了激励自己在学习道路上养成良好的习惯,所以转载请注明出处,谢谢!

自动化测试基础篇--Selenium unittest生成测试报告(HTMLTestRunner)的更多相关文章

  1. 自动化测试基础篇--Selenium unittest简介

    一.什么是unittest unittest是Python单元测试框架,类似于JUnit框架. unittest中有4个重要的概念:test fixture, test case, test suit ...

  2. 3.5 unittest生成测试报告HTMLTestRunner

    3.5 unittest生成测试报告HTMLTestRunner 前言批量执行完用例后,生成的测试报告是文本形式的,不够直观,为了更好的展示测试报告,最好是生成HTML格式的.unittest里面是不 ...

  3. python接口自动化测试(七)unittest 生成测试报告

    用例的管理问题解决了后,接下来要考虑的就是报告我问题了,这里生成测试报告主要用到 HTMLTestRunner.py 这个模块,下面简单介绍一下如何使用: 一.下载HTMLTestRunner下载: ...

  4. 自动化测试基础篇--Selenium发送测试报告邮件

    来自:https://www.cnblogs.com/sanzangTst/p/8377870.html 发邮件需要用到python两个模块,smtplib和email,这俩模块是python自带的, ...

  5. 自动化测试基础篇--Selenium框架设计(POM)

    一.自动化测试框架 感谢木棉花的漂泊分享,内容转自链接:http://www.cnblogs.com/fengyiru6369/p/8053035.html 1.什么是自动化测试框架 简单来说,自动化 ...

  6. 自动化测试基础篇--Selenium简单的163邮箱登录实例

    摘自https://www.cnblogs.com/sanzangTst/p/7472556.html 前面几篇内容一直讲解Selenium Python的基本使用方法.学习了什么是selenium: ...

  7. 自动化测试基础篇--Selenium元素定位

    摘自https://www.cnblogs.com/sanzangTst/p/7457111.html 一.Selenium元素定位的重要性: Web自动化测试的操作:获取UI页面的元素,对元素进行操 ...

  8. 自动化测试基础篇--Selenium文件上传send_keys

    摘自https://www.cnblogs.com/sanzangTst/p/8358165.html 文件上传是web页面上很常见的一个功能,自动化成功中操作起来却不是那么简单. 一般分两个场景:一 ...

  9. 自动化测试基础篇--Selenium鼠标键盘事件

    摘自https://www.cnblogs.com/sanzangTst/p/7477382.html 前面几篇文章我们学习了怎么定位元素,同时通过实例也展示了怎么切换到iframe,怎么输入用户名和 ...

随机推荐

  1. WebStorm 快键键

    ctrl+/ 单行注释ctrl+shift+/ 块注释ctrl+shift+ +/- 展开/折叠ctrl+alt+L 格式化代码ctrl+shift+ up/down 上下移动句子 Alt+回车 导入 ...

  2. exe4j生成的exe反编译成java代码

    很早以前写了一个java串口小程序,现在只有exe4j打包后的源程序了,最近又要用,折腾了一下发现其实要找回来也很简单,这里记录一下,以免以后忘记. exe4j只是将java程序,使用自己的方式打包了 ...

  3. R语言列表list函数

    列表是R语言中的对象,它包含不同类型的元素,比如 - 数字,字符串,向量和另一个列表等.一个列表还可以包含一个矩阵或一个函数作为它的元素.使用list()函数创建列表. 创建一个列表 下面是一个例子来 ...

  4. TypeScript的简单介绍和win环境安装

    TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类的面向对象编程.特点是一门强类型语言. 安装: 1 首先 ...

  5. 常用工具说明--mongodb、mysql解压版、IDEA配置maven

    Mongodb的安装.配置 1.去官网下载mongodb安装包,mongodb官网.点击右上角的 Download,下载对应的msi安装包 2.安装程序,选择 Custom,自定义安装路径,比如安装在 ...

  6. [转]SAPUI5 (01) - OpenUI5环境搭建

    本文转自:http://blog.csdn.net/stone0823/article/details/53750094 版权声明:本文为博主原创文章,转载请注明出处:http://blog.csdn ...

  7. <深入理解JavaScript>学习笔记(3)_全面解析Module模式

    简介 Module模式是JavaScript编程中一个非常通用的模式,一般情况下,大家都知道基本用法,本文尝试着给大家更多该模式的高级使用方式. 首先我们来看看Module模式的基本特征: 模块化,可 ...

  8. Vue 多路由文件的合并

    Vue 多路由文件的合并 1.使用的是ES6 数组的合并方法 let routes = new Set([...routes1, ...homerouters]);2.两个路由文件,导出的实际上就是一 ...

  9. 撩课-Python-每天5道面试题-第4天

    一. for循环和while循环中的else代表什么意思? 当for循环和while循环顺利的遍历完成时, 就会执行else分支 如果循环过程中, 碰到continue, 只要没有打断循环, 就会继续 ...

  10. K:栈和队列的比较

    栈和队列的相同点: 都是线性结构,即数据元素之间具有"一对一"的逻辑关系 都可以在顺序存储结构和链式存储结构上进行实现 在时间代价上,插入和删除操作都需常数时间:在空间代价上,情况 ...