最近在用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. Jsoup解析XML

    先导入jsoup.jar  包 方法1:不推荐,了解即可 方法 方法3: 后期学习主流

  2. adobe air for ios 应用完成appstore评论

    1,跳转到App Store:NSString *str = [NSString stringWithFormat:@"http://itunes.apple.com/us/app/id%d ...

  3. git删除和提交

    //删除git分支git branch -D BranchNamegit branch -r -D origin/BranchNamegit push origin -d BranchName//提交 ...

  4. .NET Core 事件总线,分布式事务解决方案:CAP 基于Kafka

    背景 相信前面几篇关于微服务的文章也介绍了那么多了,在构建微服务的过程中确实需要这么一个东西,即便不是在构建微服务,那么在构建分布式应用的过程中也会遇到分布式事务的问题,那么 CAP 就是在这样的背景 ...

  5. 爬虫---爬虫er与反爬虫er之间的斗争 转发

    转自:昵称:python修行路 https://www.cnblogs.com/zhaof/p/7326260.html

  6. lumen----------A facade root has not been set.

    1.新拉下来的lumen源码,直接使用Log::info是不行的.汇报如下图错误 解决办法如下图,因为lumen需要设置一些开关

  7. JavaScript与java差异

  8. asp.net 去掉小数点后面多余的0

    很多时候,比如gridview内,不想现实从数据库带出的多余小数 ,比如 4.01000. 那就可以做一个函数: /// <summary> /// 去掉小数点后多余的0, 0本身显示为0 ...

  9. 梳理vue双向绑定的实现原理

    Vue 采用数据劫持结合发布者-订阅者模式的方式来实现数据的响应式,通过Object.defineProperty来劫持数据的setter,getter,在数据变动时发布消息给订阅者,订阅者收到消息后 ...

  10. visualization of filters keras 基于Keras的卷积神经网络(CNN)可视化

    https://adeshpande3.github.io/adeshpande3.github.io/ https://blog.csdn.net/weiwei9363/article/detail ...