最近在用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. Python 多线程和线程池

    一,前言 进程:是程序,资源集合,进程控制块组成,是最小的资源单位 特点:就对Python而言,可以实现真正的并行效果 缺点:进程切换很容易消耗cpu资源,进程之间的通信相对线程来说比较麻烦 线程:是 ...

  2. linux awk 常见字符串处理

    awk指定输出列: awk '{print $0} file' #打印所有列awk '{print $1}' file #打印第一列 awk '{print $1, $3}' file #打印第一和第 ...

  3. jQuery的属性操作

    下面介绍jQuery属性操作: .val() 这是一个读写双用的方法,用来处理input的value,当方法没有参数的时候返回input的value值,当传递了一个参数的时候,方法修改input的va ...

  4. visual studio 启动报 activityLog.xml文件 错误

    1.在安装目录里面找到 devenv.exe 这个文件的所在位置C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE 2.点击左下角图标, ...

  5. (转载)js日期格式化转化

    原文地址:http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html // 对Date的扩展,将 Date 转化为指定格式 ...

  6. 接口自动化框架(java)--1.项目概述

    项目github地址: https://github.com/tianchiTester/API_AutoFramework 这套框架的报告是自己封装的 1.测试基类TestBase: 接口请求的te ...

  7. window系统下搭建本地的NuGet Server

    1. NuGet.Config文件所在的目录: C:\Users\xxx\AppData\Roaming\NuGet 2.将nupkg为结尾的文件放在 项目的Packages目录下.(注意是和web. ...

  8. requests 可以玩接口自动化测试,爬虫也是可以滴

    import requests #1.带参的get请求: url ='URL_你的' requests.get(url,params={"key":"value" ...

  9. javascript中 for-in和 for-of的区别

    其中for-of是ES6新增的迭代语法 在MDN上的解释: for...in语句以任意顺序遍历一个对象的可枚举属性.对于每个不同的属性,语句都会被执行.for...of语句在可迭代对象(包括 Arra ...

  10. Caravel–一款开源OLAP+数据可视化分析前端工具,支持Druid和Kylin

    参考此文:http://lxw1234.com/archives/2016/06/681.htm