ddt读取json文件测试用例的执行顺序

一. 源码的说明

在源码中,ddt的file_data函数下有这样一段话

意思是说,如果json文件的内容是字典,字典的键名将会作为测试用例名的后缀,字典的值将会作为测试数据,如果这样的话,如果键名字母排序靠前的是不是先执行,字母排序靠后的是不是后执行?

二. 验证猜想

我们来验证一下,先新建一个my.json文件

{"name":"nick","gender":"male","age":"29"}

再新建一个测试用例类test_class_ddt.py

import ddt
import unittest

@ddt.ddt
class test_ddt(unittest.TestCase):

    @ddt.file_data(r"D:\python_workshop\python6\revise\futureloan_API_framework\my.json")
    def test_readData_fromJsonFile(self, a):
        """In case of a dict, keys will be used as suffixes to the name of the
        test case, and values will be fed as test data.
        如果文件的内容是一个字典,键名将作为测试用例的后缀,值将作为测试数据"""
        print("从文件读取数据")
        print(a)

最后创建一个主函数,生成html测试报告

import unittest
from revise.futureloan_API_framework.test_class_ddt import test_ddt
import time, os
from HTMLTestRunnerNew import HTMLTestRunner

suite = unittest.TestSuite()
loader = unittest.TestLoader()
suite.addTests(loader.discover(os.getcwd()))

now = time.strftime("%Y-%m-%d_%H-%M-%S")
fs = open(os.getcwd() + "/DDT_Test_Report_{0}.html".format(now), "wb")

runner = HTMLTestRunner(stream=fs, title="DDT Test Report", tester="xiaozhai")
runner.run(suite)

三. 观察结果

总共运行了3次测试用例,生成了3个测试报告,的确,源码说的key会追加到测试用例名后面作为后缀,这个情况是存在的,但运行的顺序却是随机的(可能因为字典是无序的吧)

第一次测试报告

第二次测试报告

第三次测试报告

猜你喜欢

转载自www.cnblogs.com/cnhkzyy/p/9318626.html