python之单元测试_生成测试报告
(1)HTMLTestRunner.py的下载路径:https://pan.baidu.com/s/1Yk2E8d8bIo5_rmpussOE9Q 提取码:0jae
(2)HTMLTestRunner.py的存放到python安装的路径的lib文件夹下面,如下图所示:

(3)以加减乘除的计算为例,创建三个类:(1)mathMethod.py(2)testMathMethod.py(3)testSuit.py

(1)mathMethod.py
class MathMethod:
def __init__(self, a, b):
self.a = a
self.b = b
def add(self):
return self.a+self.b
def sub(self):
return self.a-self.b
def chengfa(self):
return self.a*self.b
def div(self):
return self.a/self.b
(2)testMathMethod.py
import unittest
from 单元测试.mathMethod import MathMethod # 对mathmethod进行单元测试
# TestCase
# 下面都是测试用例
class TestMathMethod(unittest.TestCase):
def setUp(self):
print("开始测试啦!") def test_add(self):
try:
t = MathMethod(5, 4).add()
self.assertEqual(t, 9, "出错啦")
print(t)
except AssertionError as e:
print("单元测试出错啦,错误是%s")
raise e def test_sub(self):
try:
t = MathMethod(9, 6).sub()
self.assertEqual(t, 3, "出错啦")
print(t)
except AssertionError as e:
print("单元测试出错啦,错误是%s")
raise e def test_chengfa(self): try:
t = MathMethod(5, 5).chengfa()
self.assertEqual(t, 25, "出错啦")
print(t)
except AssertionError as e:
print("单元测试出错啦,错误是%s")
raise e def test_div(self):
try:
t = MathMethod(25, 5).div()
self.assertEqual(t, 5, "出错啦")
print(t)
except AssertionError as e:
print("单元测试出错啦,错误是%s")
raise e def tearDown(self):
print("测试结束了!") (3)testSuit.py
import unittest
import time
from 单元测试.testMathMethod import TestMathMethod
import HTMLTestRunner # 作用把所有测试用例集合起来,放在一个测试集里面。
suite = unittest.TestSuite()
suite.addTest(TestMathMethod("test_add"))
suite.addTest(TestMathMethod("test_sub"))
suite.addTest(TestMathMethod("test_chengfa"))
suite.addTest(TestMathMethod("test_div"))
now = time.strftime('%Y-%m-%d_%H_%M_%S')
# 执行测试集
filePath = "pyResult" + now + ".html"
fp = open(filePath, 'wb')
# 生成报告的title,描述
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title="2019-1-6 test report",
verbosity=2) # 执行测试用例出结果
runner.run(suite)
fp.close()
(4)运行testSuit生成测试报告:

请大家支持原创,尊重原创,如要转载,请注明出处:“转载自:https://www.cnblogs.com/xiaoyunyun100fen/”:谢谢!!如有疑问,欢迎大家留言区艾特我哈。
python之单元测试_生成测试报告的更多相关文章
- python使用 HTMLTestRunner.py生成测试报告
HTMLTestRunner.py python 2版本 下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html 使用时,先建立一个”PyDe ...
- Python+Selenium+Unittest+HTMLTestRunner生成测试报告+发送至邮箱,记一次完整的cnblog登录测试示例,
测试思路:单个测试集.单个测试汇成多个测试集.运行测试集.生成测试报告.发送至邮箱. 第一步:建立单个测试集,以cnblog登录为例. 测试用例: cnblog的登录测试,简单分下面几种情况:(1)用 ...
- python使用HTMLTestRunner.py生成测试报告
这里我使用的是python selenium webdriver环境,浏览器驱动安装见selenium 1.下载HTMLTestRunner.py:http://tungwaiyip.info/sof ...
- Karma +Jasmine+ require JS进行单元测试并生成测试报告、代码覆盖率报告
1. 关于Karma Karma是一个基于Node.js的JavaScript测试执行过程管理工具(Test Runner). 该工具可用于测试所有主流Web浏览器,也可集成到CI(Continuou ...
- python自动化之(自动生成测试报告)
前言: 用python执行测试脚本, 测试报告是记录我们测试过程的问题, 方便我们对整个测试过程的把控. 这里引用的是别人写好的模板, 我们拿过来用就OK, 能力强者可自行编写模板 测试报告图模板: ...
- 基于python xlsxwriter、xlrd 生成测试报告
import xlsxwriter,xlrd ''' 思路: 1.获取数据 2.整合数据 3.写入文件 ''' #筛选 def filt(category,table,filt_name=None,r ...
- Python学习笔记_生成验证码
import random def verification_code(): num = [str(x) for x in range(10)] # 列表生成器0-9 upper = [chr(x) ...
- python之单元测试框架—unittest
一. 什么是单元测试?单元测试的对象是什么? 1: 什么是单元测试? 按照定义,单元测试就是对单个模块或者单个类或者单个函数进行测试,一般是开发做的,按照阶段分,一般就是单元测试.集成测试.系统测试. ...
- Python单元测试框架之pytest 2 -- 生成测试报告
From: https://www.cnblogs.com/fnng/p/4768239.html Python单元测试框架之pytest -- 生成测试报告 2015-08-29 00:40 by ...
随机推荐
- C# 当中 LINQ 的常规用法(Lambda 方式)
仅以本篇博文记录 LINQ 相关操作的基本知识,原型参考自 MSDN 相关知识,中间加以自己的理解与 DEMO. 1. IEnuemrable<T>.Select() Select 方法比 ...
- PHP 扩展管理
一直对 PHP 扩展了解的似是而非,每次安装扩展都要百度教程,很容易出现各种错误.所幸整理下管理扩展的所有操作,方便日后操作. 查看已加载的扩展 输出 phpinfo(): 使用 get_loaded ...
- Once More
Topic Link http://ctf5.shiyanbar.com/web/more.php 1)源代码分析 发现 ereg()函数使得password必须是数字或字母同时长度必须是小于8val ...
- leetcode — single-number
/** * Source : https://oj.leetcode.com/problems/single-number/ * * * Given an array of integers, eve ...
- leetcode — palindrome-partitioning-ii
import java.util.Arrays; /** * * Source : https://oj.leetcode.com/problems/palindrome-partitioning-i ...
- SpringCloud-Greenwich版本新特性探索(1)---SpringCloudGateway
一.前言 1.SpringCloudGateway是SpringCloud新推出的网关框架,比较于上一代Zuul,功能和性能有很大的提升.Zuul1.x采用的是阻塞多线程方式,也就是一个线程处理一个连 ...
- Dapper, 批量插入,批量更新, 以及in, like
1. 批量插入 public async Task CreateBusinessItemAsync(IEnumerable<BusinessItemsEntity> businessIte ...
- Java 学习笔记 二维数组和对象数组
定义二维数组 int[][] a = new int[4][5]; 可以不指定列数 int[][] a = new int[4][]; 获取行 int i = a.length(); 如果使用第一个例 ...
- Maven项目POM文件错误,提示“Plugin execution not covered by lifecycle configuration”的解决方案
一. 问题 Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-depend ...
- Go开发之路(目录)
知识点 1. Go语言 简介 2. Go语言 基本语法 3. Go语言 strings以及strconv的使用 4. Go语言 时间和日期类型 5. Go语言 指针类型 6. Go语言 流程控制 7. ...