unittest使用parameterized参数化后如何调用添加到测试套件中

写了一个Unittest+Python+execl的一个接口自动化,在参数化的时候遇到了一个问题。具体的“坑”如下

要实现的需求

在execl中涉及或写接口测试用例,然后读取execl中每一行的数据,每一行数据就相当于一条用例

需求实现

path = "F:\InterFace_JIA1\dataconfig\source_user_case.xlsx"
params_list = TestRunCase(path).get_params()
print("params_list:",params_list)

class TestRun(unittest.TestCase):
    #params_list = [(2, 100000, 100001),(1, 100000, 100003)]
    @parameterized.expand(params_list) # 这里参数化了params_list
    def test_run(self, name, expect_res, actual_res):
        self.assertEqual(expect_res, actual_res)

if __name__ == '__main__':
    unittest.main()
用例为:
在这里插入图片描述
结果为:
在这里插入图片描述
先不管接口是不是有问题,从这个运行看,流程是OK的

参数化后调用加入测试条件中

if __name__ == '__main__':
    suite = unittest.TestSuite()
    now = datetime.datetime.now().strftime('%Y-%m-%d_%H_%M_%S')
    filename = "./report/" + now + '_result.html'
    fp = open(filename, 'wb')
    suite.addTest(TestRun('test_run'))
    runner = HTMLTestRunner.HTMLTestRunner(
        stream=fp,
        title=u'测试结果',
        description=u'全部测试用例')
    runner.run(suite)
    fp.close()
    time.sleep(2)
    print("sdasdasdasdasdasdsa")

结果出错

TypeError: 'NoneType' object is not callable

在这里插入图片描述

排查分析

使用unittest.defaultTestLoader.discover,打印所有的case,发现用例格式是“test_run_0”

<unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<main.run.TestRun testMethod=test_run_0>, <main.run.TestRun testMethod=test_run_1>]>]>
if __name__ == '__main__':

    suite = unittest.defaultTestLoader.discover('./', pattern='run.py')
    for case in suite:
        print (case)

重新调用

把test_run改成test_run_0

if __name__ == '__main__':
    suite = unittest.TestSuite()
    now = datetime.datetime.now().strftime('%Y-%m-%d_%H_%M_%S')
    filename = "./report/" + now + '_result.html'
    fp = open(filename, 'wb')
    suite.addTest(TestRun('test_run_0'))
    runner = HTMLTestRunner.HTMLTestRunner(
        stream=fp,
        title=u'测试结果',
        description=u'全部测试用例')
    runner.run(suite)
    fp.close()
    time.sleep(2)
    print("sdasdasdasdasdasdsa")

仍然报错:
提示“test_run_0”找不到

再次分析

发现如图,使用suite = unittest.defaultTestLoader.discover('./', pattern='run.py')即可,直接去掉suite.addTest(TestRun('test_run_0'))
在这里插入图片描述

再次调试

结果就OK了

class TestRun(unittest.TestCase):
    #params_list = [(2, 100000, 100001),(1, 100000, 100003)]
    @parameterized.expand(params_list)
    def test_run(self, name, expect_res, actual_res):
        self.assertEqual(expect_res, actual_res)
        #print(actual_res)
        #self.assertTrue(True, actual_res)

if __name__ == '__main__':

    suite = unittest.defaultTestLoader.discover('./', pattern='run.py')
    # for case in suite:
    #     print (case)
    # suite = unittest.TestSuite()
    now = datetime.datetime.now().strftime('%Y-%m-%d_%H_%M_%S')
    filename = "./report/" + now + '_result.html'
    fp = open(filename, 'wb')
    #suite.addTest(TestRun('test_run_0'))
    runner = HTMLTestRunner.HTMLTestRunner(
        stream=fp,
        title=u'测试结果',
        description=u'全部测试用例')
    runner.run(suite)
    fp.close()
    time.sleep(2)
    print("sdasdasdasdasdasdsa")
    #sendmain(filename, mail_to=['zhangbo@novastar.tech'])

在这里插入图片描述

1 声望
0 粉丝
0 条评论
推荐阅读
WindowsGUI自动化测试框架搭建之需求整理、详细设计和框架设计
1 需求整理1.1 实现目的基于CS架构,模拟用户(鼠标、键盘)操作,达到快速、重复执行测试用例;便于回归测试,快速覆盖主线用例或功能;线上或线下巡检测试,结合持续集成,及时发现运行环境存在的问题;提升个...

虫无涯阅读 397

测试开发专题-目录
专题开篇1. 测试开发简介网络篇1. OSI七层协议2. 常用服务协议3. IP地址4. 网络路由5. 常用命令6. 问题定位系统篇1. 常用系统命令2. 安装KVM虚拟机3. 按需创建KVM测试虚拟机4. Docker容器安装5. 使用Docker测试常...

陈琦1阅读 2.2k

封面图
月光宝盒(vivo流量录制回放平台)正式对外开源
月光宝盒是一个基于流量录制回放的自动化测试平台,通过录制回放取代编写脚本进行自动化回归,提升测试效率和覆盖率。因为其解决方案具有很强的通用性,所以我们把这它开源出来,希望能帮助到有需要的用户。

vivo互联网技术阅读 1.8k

前端高质量交付产品利器之自动化测试
对客户交付高质量的产品是企业的核心目标之一,而单元测试是实现这一目标的重要手段之一。通过单元测试,可以确保产品的每个部分都经过了严格的测试,降低产品出现缺陷的概率,提高产品的可靠性和稳定性。同时,...

Aaron1阅读 597

cookie 时效无限延长方案
自动化测试有2种形式,接口自动化和UI自动化。而UI自动化经常会被登录节点堵塞,例如验证码、图形、滑块等,尽管有些方式可以识别图形和定位滑块位置,但成功率都不高,无法真正意义上实现自动化执行;而http接口...

京东云开发者1阅读 464

封面图
Django笔记三十六之单元测试汇总介绍
在我们创建的每一个 application 下面都有一个 tests.py 文件,我们通过继承 django.test.TestCase 编写我们的单元测试。

Hunter阅读 895

封面图
开源好物推荐:全栈测试平台RunnerGo
做软件测试的同学在工作时应该都碰到过这种情况:接口管理、接口测试用postman、Apipost等接口管理工具,性能测试用jmeter、loadrunner等性能测试工具,接口自动化则是jmeter脚本或者python脚本配合jenkins使用。...

大雨治水阅读 872

1 声望
0 粉丝
宣传栏