乐趣区

关于单元测试: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'])
退出移动版