最近在用python3.6+unittest+requests做自动化接口测试。发现一个问题,unittest中使用第3方插件parameterized进行参数化,再生成html报告时,运行就会失败。很奇怪,最终没有让这2个同时实现。

经过一段时间后,发现有2种方法;

1、用discover+测试报告

在run.py中,使用discover()方法找到要运行的测试用例,然后放到测试报告中:

 import time
import unittest
from BeautifulReport import BeautifulReport test_dir = './test_case'
discover = unittest.defaultTestLoader.discover(test_dir,pattern='*testcase.py') if __name__ == '__main__':
now = time.strftime("%Y-%m-%d %H_%M_%S")
html_file = r'D:\report'
suite.addTest(ParametrizedTestCase.parametrize(WorkPlaceTestCase, 'test_b2_addplace')) # 测试用例加入到测试套件
runner = unittest.TextTestRunner()
BeautifulReport(discover).report(filename='测试报告' + now, description='测试', log_path=html_file)
runner.run(discover)

2、使用ParametrizedTestCase类

这个感觉绕了很大的弯,是最开始使用的。

百度到了一个人写的可参数化的ParametrizedTestCase类。再改动了一下,加上比较简单的list。虽然看起来很Low。但是最终目的还算是实现了。

接口测试用例的类如下:

 import unittest
from pylibrary.PyLib import * class ParametrizedTestCase(unittest.TestCase): #可参数化的类
""" TestCase classes that want to be parametrized should
inherit from this class.
"""
def __init__(self, methodName='runTest', param=None):
super(ParametrizedTestCase, self).__init__(methodName)
self.param = param
@staticmethod
def parametrize(testcase_klass,defName=None, param=None): #参数化方法
""" Create a suite containing all tests taken from the given
subclass, passing them the parameter 'param'.
"""
testloader = unittest.TestLoader()
testnames = testloader.getTestCaseNames(testcase_klass)
suite = unittest.TestSuite()
if defName !=None:
for name in testnames:
if name==defName:
suite.addTest(testcase_klass(name, param=param))
else:
for name in testnames:
suite.addTest(testcase_klass(name, param=param))
return suite list = [('2018-09-26', '2018-09-26', '测试', '', 49, 'TotalCallNum'),
('2018-09-26', '2018-09-26', '测试', '', 60, 'TotalCallAnswered'),
('2018-09-26', '2018-09-26', '测试', '', 60, 'RingNum'),
('2018-09-13', '2018-09-13', '测试', '', 4, 'TotalCallNum_Transfer')] class Interface_report1(ParametrizedTestCase):
def test_Agreport1(self):  #要被测试的用例 self.num = self.param[4]
self.report_num1 = PyLib().getAgReport(self.param[0],self.param[1],self.param[2],self.param[3])
self.report_num=self.report_num1[0][self.param[5]]
self.assertEqual(self.num,self.report_num)

再用run.py调用上面的testcase

 from interface.Interface_report1 import *
from BeautifulReport import BeautifulReport if __name__ == '__main__': now = time.strftime("%Y-%m-%d %H_%M_%S")
# # 构造测试集
# suite = unittest.TestSuite()
# suite.addTest(Interface_report1('test_Agreport1'))
#
htmlfile = 'D:\\Python_code\\report\\'
# fp = open(htmlfile, 'wb')
# runner = HTMLTestRunner(stream=fp, title='测试报告', description='用例执行情况')
# runner.run(suite)
# fp.close()
suite = unittest.TestSuite()
#suite.addTest(Interface_report1('test_Agreport1'))
for i in list: #使用list
suite.addTest(ParametrizedTestCase.parametrize(Interface_report1, 'test_Agreport1',param=i)) BeautifulReport(suite).report(filename='测试报告'+now,description='测试报告',log_path=htmlfile)

最后运行就可以了,生成的报告用的beautifulreport。感觉还是比较好看的

unittest同时支持参数化和生成html报告的更多相关文章

  1. 自动化测试===unittest配套的HTMLTestRunner.py生成html报告源码

    更改版: 全部复制,命名为  HTMLTestRunner.py 文件 #使用方法参见之前的文档:自动化测试===unittest和requests接口测试案例,测试快递查询api(二) " ...

  2. pytest 学习笔记二:兼容unittest、执行方式、生成报告

    1.官方文档上说pytest兼容unittest时,不支持setUpModule 和 tearDownModule,但实际验证是可以的. 验证的场景是py文件中,只有一个测试类, 经验证有多个测试类, ...

  3. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告

    1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请求参数 ...

  4. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(二)

    可以参考 python+requests接口自动化完整项目设计源码(一)https://www.cnblogs.com/111testing/p/9612671.html 原文地址https://ww ...

  5. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(已弃用)

    前言 1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请 ...

  6. unittest 运行slenium(五)---运行代码并生成HTMLTestRunner报告

    整体代码如下: import os import sys import time import datetime import unittest import HTMLTestRunner # git ...

  7. python生成HTMl报告(unittest)

      Python3 使用HTMLTestRunner.py 报错ImportError: No module named 'StringIO'处理方法 HTMLTestRunner.py文件是基于Py ...

  8. 解惑unittest框架中导入HTMLTestRunner模块后正常运行却无法生成HTML报告问题

    1.HTMLTestRunner介绍 HTMLTestRunner是一个第三方的unittest HTML报告库,用于python单元测试框架的TestRunner.它是生成一个HTML报告,以一目了 ...

  9. pytest十六:allure2 生成 html 报告

    allure 是一个 report 框架,支持 java 的 Junit/testng 等框架,当然也可以支持 python 的 pytest 框架,也可以集成到 Jenkins 上展示高大上的报告界 ...

随机推荐

  1. vue-cli +echarts-amap集成echarts和高德地图TypeError: Cannot read property 'dataToPoint' of null解决方案

    由于项目的需求,需要做一种迁徙效果, 最后我们采用了组件化开发,可以说这个坑自己一个人踩,有点累,但也收获不少. vue-cli +echarts-amap集成echarts和高德地图,出现报错,错误 ...

  2. git使用:本地分支merge到远程分支

    背景:为了更加规范维护自动化测试工程,代码提交流程最近更新了,先拉分支到本地修改,完成后同步到远程分支. 前置条件 (1)本地机器可通过ssh与gitlab服务器通信 (2)gitlab上的测试项目中 ...

  3. 未能执行URL

    在 <compilation debug="true"> 下 加入: <buildProviders> <add extension=".h ...

  4. python基础之 序列化,os,sys,random,hashlib

    1.序列化 定义: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.简单地说,JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然 ...

  5. 【LeetCode每天一题】Plus One(加一)

    Given a non-empty array of digits representing a non-negative integer, plus one to the integer.The d ...

  6. MySql修改数据表的基本操作(DDL操作)

    1.查看数据库的基本语句:show databases; 2.选择相应的数据库进入语法:use 数据库名; 3.查看数据库中的表语法:show tables; 4.查看表的基本结构语句:desc 表名 ...

  7. Qt QLabel 显示gif动图

    #include <QMovie> QMovie * move = new QMovie(":/gif/牵着我的手去浪迹天涯.gif"); ui->label_g ...

  8. H.264学习--1

    1.宏块(Macro Block):一个编码图像首先要划分成多个块(4x4 像素)才能进行处理,显然宏块应该是整数个块组成,通常宏块大小为                               ...

  9. [PHP] 解决人人商城收银台不能上传图片问题

    反正网上一大堆,也不知道哪个版本有修复,反正我的没有修复. 问题报错:ReferenceError: angular is not defined 解决如下: 修改文件:addons/ewei_sho ...

  10. 轻量级集群管理软件-ClusterShell

    如果集群数量不多的话,选择一个轻量级的集群管理软件就显得非常有必要了.ClusterShell就是这样一种小的集群管理工具,原理是利用ssh,可以说是Linux系统下非常好用的运维工具  cluste ...