批量执行完用例后,生成的测试报告是文本形式的,不够直观,为了更好的展示测试报告,最好是生成HTML格式的。

unittest里面是不能生成html格式报告的,需要导入一个第三方的模块:HTMLTestRunner

一、导入HTMLTestRunner

1.这个模块下载不能通过pip安装了,只能下载后手动导入,下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html

2.Download下HTMLTestRunner.py文件就是我们需要下载的包。

3.下载后手动拖到python安装文件的Lib目录下

4.验证安装成功 执行 import HTMLTestRunner 没有报错证明配置成功。

报告路径

 # encoding = utf - 8
import unittest
import sys
import HTMLTestRunner
import time
import os #被测试类
class myclass(object):
"""classmethod 修饰符对应的函数不需要实例化,
不需要 self 参数,但第一个参数需要是表示自身
类的 cls 参数,可以来调用类的属性,类的方法,
实例化对象等。"""
@classmethod
def sum(cls,a,b):
return a + b #将两个传入参数进行相加操作
@classmethod
def sub(cls, a, b):
return a - b #将两个传入参数进行相减操作 class mytest(unittest.TestCase):
# @classmethod
# def setUpClass(cls):
# """初始化类固件"""
# print("-----setUpClass") # @classmethod
# def tearDownClass(cls):
# """重构类固件"""
# print("--tearDownClas") #初始化工作
def setUp(self):
self.a = 3
self.b = 1
print("--setUp") #退出清理工作
def tearDown(self):
print("--tearDown") #具体的测试用例,一定要以test开头
def test1sum(self):
# res=3/0 抛出异常,执行结果为F
#断言两数之和的结果是否是4
res=myclass.sum(self.a,self.b)
self.assertEqual(res,4,'test sum fail')
print('testsum方法') def testsub(self):
#断言两数之差的结果是否是2
self.assertEqual(myclass.sub(self.a,self.b),2,'test sub fail')
print('testsub方法') class aa(unittest.TestCase):
# num=2
# @unittest.skip('skipping')
# @unittest.skipIf(num>1,'num已经大于1')
@unittest.skipUnless(sys.platform == "linux", "requires linux")
def testaa(self):
print('testaa') if __name__ == '__main__':
testCase = unittest.TestLoader().loadTestsFromTestCase(mytest)
#将多个测试类加载到测试集合中
suite = unittest.TestSuite([testCase])
# 创建时间
cur_time = time.strftime('%Y-%m-%d_%H_%M_%S')
# 文件路径
filename = os.path.dirname(os.path.abspath('.'))+'\\report\\'+cur_time+'.html'
print("===================================")
print("当前文件路径是:",os.path.abspath('.'))
print("===================================")
print("当前文件路径的上一级文件路径是:",os.path.dirname(os.path.abspath('.')))
# 写入一个文件
f = open(filename,"wb")
# 报告详情
runner = HTMLTestRunner.HTMLTestRunner(stream=f,title="测试报告",description='测试报告描述')
runner.run(suite) # 原始版本
# f = open("./report.html","wb")
# runner = HTMLTestRunner.HTMLTestRunner(stream=f,title="测试报告",description="测试报告描述")
# runner.run(suite)
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>测试报告</title>
<meta name="generator" content="HTMLTestRunner 0.8.2"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <style type="text/css" media="screen">
body { font-family: verdana, arial, helvetica, sans-serif; font-size: 80%; }
table { font-size: 100%; }
pre { } /* -- heading ---------------------------------------------------------------------- */
h1 {
font-size: 16pt;
color: gray;
}
.heading {
margin-top: 0ex;
margin-bottom: 1ex;
} .heading .attribute {
margin-top: 1ex;
margin-bottom: 0;
} .heading .description {
margin-top: 4ex;
margin-bottom: 6ex;
} /* -- css div popup ------------------------------------------------------------------------ */
a.popup_link {
} a.popup_link:hover {
color: red;
} .popup_window {
display: none;
position: relative;
left: 0px;
top: 0px;
/*border: solid #627173 1px; */
padding: 10px;
background-color: #E6E6D6;
font-family: "Lucida Console", "Courier New", Courier, monospace;
text-align: left;
font-size: 8pt;
width: 500px;
} }
/* -- report ------------------------------------------------------------------------ */
#show_detail_line {
margin-top: 3ex;
margin-bottom: 1ex;
}
#result_table {
width: 80%;
border-collapse: collapse;
border: 1px solid #777;
}
#header_row {
font-weight: bold;
color: white;
background-color: #777;
}
#result_table td {
border: 1px solid #777;
padding: 2px;
}
#total_row { font-weight: bold; }
.passClass { background-color: #6c6; }
.failClass { background-color: #c60; }
.errorClass { background-color: #c00; }
.passCase { color: #6c6; }
.failCase { color: #c60; font-weight: bold; }
.errorCase { color: #c00; font-weight: bold; }
.hiddenRow { display: none; }
.testcase { margin-left: 2em; } /* -- ending ---------------------------------------------------------------------- */
#ending {
} </style> </head>
<body>
<script language="javascript" type="text/javascript"><!--
output_list = Array(); /* level - 0:Summary; 1:Failed; 2:All */
function showCase(level) {
trs = document.getElementsByTagName("tr");
for (var i = 0; i < trs.length; i++) {
tr = trs[i];
id = tr.id;
if (id.substr(0,2) == 'ft') {
if (level < 1) {
tr.className = 'hiddenRow';
}
else {
tr.className = '';
}
}
if (id.substr(0,2) == 'pt') {
if (level > 1) {
tr.className = '';
}
else {
tr.className = 'hiddenRow';
}
}
}
} function showClassDetail(cid, count) {
var id_list = Array(count);
var toHide = 1;
for (var i = 0; i < count; i++) {
tid0 = 't' + cid.substr(1) + '.' + (i+1);
tid = 'f' + tid0;
tr = document.getElementById(tid);
if (!tr) {
tid = 'p' + tid0;
tr = document.getElementById(tid);
}
id_list[i] = tid;
if (tr.className) {
toHide = 0;
}
}
for (var i = 0; i < count; i++) {
tid = id_list[i];
if (toHide) {
document.getElementById('div_'+tid).style.display = 'none'
document.getElementById(tid).className = 'hiddenRow';
}
else {
document.getElementById(tid).className = '';
}
}
} function showTestDetail(div_id){
var details_div = document.getElementById(div_id)
var displayState = details_div.style.display
// alert(displayState)
if (displayState != 'block' ) {
displayState = 'block'
details_div.style.display = 'block'
}
else {
details_div.style.display = 'none'
}
} function html_escape(s) {
s = s.replace(/&/g,'&amp;');
s = s.replace(/</g,'&lt;');
s = s.replace(/>/g,'&gt;');
return s;
} /* obsoleted by detail in <div>
function showOutput(id, name) {
var w = window.open("", //url
name,
"resizable,scrollbars,status,width=800,height=450");
d = w.document;
d.write("<pre>");
d.write(html_escape(output_list[id]));
d.write("\n");
d.write("<a href='javascript:window.close()'>close</a>\n");
d.write("</pre>\n");
d.close();
}
*/
--></script> <div class='heading'>
<h1>测试报告</h1>
<p class='attribute'><strong>Start Time:</strong> 2019-07-01 23:11:41</p>
<p class='attribute'><strong>Duration:</strong> 0:00:00.001000</p>
<p class='attribute'><strong>Status:</strong> Pass 2</p> <p class='description'>测试报告描述</p>
</div> <p id='show_detail_line'>Show
<a href='javascript:showCase(0)'>Summary</a>
<a href='javascript:showCase(1)'>Failed</a>
<a href='javascript:showCase(2)'>All</a>
</p>
<table id='result_table'>
<colgroup>
<col align='left' />
<col align='right' />
<col align='right' />
<col align='right' />
<col align='right' />
<col align='right' />
</colgroup>
<tr id='header_row'>
<td>Test Group/Test case</td>
<td>Count</td>
<td>Pass</td>
<td>Fail</td>
<td>Error</td>
<td>View</td>
</tr> <tr class='passClass'>
<td>mytest</td>
<td>2</td>
<td>2</td>
<td>0</td>
<td>0</td>
<td><a href="javascript:showClassDetail('c1',2)">Detail</a></td>
</tr> <tr id='pt1.1' class='hiddenRow'>
<td class='none'><div class='testcase'>test1sum</div></td>
<td colspan='5' align='center'> <!--css div popup start-->
<a class="popup_link" onfocus='this.blur();' href="javascript:showTestDetail('div_pt1.1')" >
pass</a> <div id='div_pt1.1' class="popup_window">
<div style='text-align: right; color:red;cursor:pointer'>
<a onfocus='this.blur();' onclick="document.getElementById('div_pt1.1').style.display = 'none' " >
[x]</a>
</div>
<pre> pt1.1: </pre>
</div>
<!--css div popup end--> </td>
</tr> <tr id='pt1.2' class='hiddenRow'>
<td class='none'><div class='testcase'>testsub</div></td>
<td colspan='5' align='center'> <!--css div popup start-->
<a class="popup_link" onfocus='this.blur();' href="javascript:showTestDetail('div_pt1.2')" >
pass</a> <div id='div_pt1.2' class="popup_window">
<div style='text-align: right; color:red;cursor:pointer'>
<a onfocus='this.blur();' onclick="document.getElementById('div_pt1.2').style.display = 'none' " >
[x]</a>
</div>
<pre> pt1.2: </pre>
</div>
<!--css div popup end--> </td>
</tr> <tr id='total_row'>
<td>Total</td>
<td>2</td>
<td>2</td>
<td>0</td>
<td>0</td>
<td>&nbsp;</td>
</tr>
</table> <div id='ending'>&nbsp;</div> </body>
</html>

测试报告

Selenium3+python自动化011-unittest生成测试报告(HTMLTestRunner)的更多相关文章

  1. 3.5 unittest生成测试报告HTMLTestRunner

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

  2. python自动化之(自动生成测试报告)

    前言:  用python执行测试脚本, 测试报告是记录我们测试过程的问题, 方便我们对整个测试过程的把控. 这里引用的是别人写好的模板, 我们拿过来用就OK, 能力强者可自行编写模板 测试报告图模板: ...

  3. Python自动化 unittest生成测试报告(HTMLTestRunner)03

    批量执行完用例后,生成的测试报告是文本形式的,不够直观,为了更好的展示测试报告,最好是生成HTML格式的. unittest里面是不能生成html格式报告的,需要导入一个第三方的模块:HTMLTest ...

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

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

  5. 自动化测试基础篇--Selenium unittest生成测试报告(HTMLTestRunner)

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

  6. python之单元测试_生成测试报告

    (1)HTMLTestRunner.py的下载路径:https://pan.baidu.com/s/1Yk2E8d8bIo5_rmpussOE9Q 提取码:0jae (2)HTMLTestRunner ...

  7. Selenium2+python自动化(unittest)

    # coding:utf-8from selenium import webdriverimport unittestimport timeclass Bolg(unittest.TestCase): ...

  8. selenium3+python自动化50-环境搭建(firefox)

    前言 有不少小伙伴在安装selenium环境后启动firefox报错,因为现在selenium升级到3.0了,跟2.0的版本还有有一点区别的. 安装环境过程中主要会遇到三个坑: 1.'geckodri ...

  9. selenium3+python自动化50-环境搭建(firefox)【转载】

    前言 有不少小伙伴在安装selenium环境后启动firefox报错,因为现在selenium升级到3.0了,跟2.0的版本还有有一点区别的. 安装环境过程中主要会遇到三个坑: 1.'geckodri ...

  10. Selenium3+python自动化006+自动化测试概述

    自动化测试概述 1.自动化分类: (1)单元测试自动化: 单元测试(Unit):模拟各种异常场景,外部依赖较少,且可以做测试单元到最小的一种测试方法. Java单元测试框架Junit.TestNG; ...

随机推荐

  1. Git安装配置及第一次上传项目到GitHub

    平时的学习工作少不了保存自己的Code到代码库,这里必须要使用到Git与GitHub. 1.   关于Git的安装 下载Git:下载地址:https://git-scm.com/downloads  ...

  2. VSCode 完美整合前后端框架(angular2+.NET core)

    首先打开命令行查看本地.NET版本. 通过命令行安装模板. dotnet new --install Microsoft.AspNetCore.SpaTemplates::* 创建demo目录,并用v ...

  3. VBA操作IE

    1.参照项目   Microsoft Internet Controls   Microsoft HTML Object   2.sample Sub GetIEItem() Dim objIE As ...

  4. node模块化开发基本知识学习笔记

    传统非模块化开发缺点: 1.命名冲突 2.文件依赖 标准的模块化规范: 1.AMD-requirejs 2.CMD-seajs 服务器端模块化规范: 1.CommonJS-Node.js 模块化相关的 ...

  5. Asp.net core 3.1+EF Core2.2.6+Oracle.EntityFrameworkCore2.1.19连接Oracle数据库

    Asp.net Core 3.1+EF Core2.2.6+Oracle.EntityFrameworkCore2.1.19连接Oracle数据库 1.前言 本次主要采用Asp.net core3.1 ...

  6. 康托展开+反向bfs

    康托展开+反向bfs hdu 1043 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1043 #include <iostream> # ...

  7. 使用 VMware Workstation Pro 安装新的虚拟机

     一.连接服务器 (1)“文件”右键 (2)输入用户名.密码连接服务器 二.创建新的虚拟机 (1) (2) (3) (4) (5) (6) (7) (8)  三.配置服务器 配置文档 - 链接:htt ...

  8. gogs搭建git服务教程

    使用gogs搭建自己的git服务!!! 一.GIT服务器搭建方式 上一节课我们讲过GIT是一个分布式版本管理系统,既然是分布那么必定会涉及远程通信,那么GIT是采用什么协议进行远程通信的呢? git支 ...

  9. 【内推】2020微软苏州Office365众多核心团队热招150+研发精英!欢迎推荐

    2020微软苏州Office365众多核心团队热招150+研发精英!欢迎推荐 大家好,目前微软Office365核心团队在美丽宜居的苏州有150多的社招职位虚位以待,欢迎大家自荐,推荐,转发!除以下列 ...

  10. python之三目运算符的替代品?

    # 不知曾几何时,你是否也觉得Python的三目运算写起来很麻烦呢?(没有过) # 比如: a, b = 3, 4 c = a if a > b else b d = a if a < b ...