一、上篇文章我们使用了 unittest + ddt 驱动 excel 文件做数据驱动测试,本篇文章我们采用 unittest + ddt 驱动 json 文件来实现数据驱动测试,话不多说上源码。。。
ddt.file_data:装饰测试方法,参数是文件名。文件可以是 json 或者 yaml 类型。
注意:如果文件是以 “.yml”或者".yaml" 结尾,ddt 会作为 yaml 类型处理,其他文件都会作为 json 文件处理。
如果文件是列表,列表的值会作为测试用例参数,同时,会作为测试用例方法名后缀显示。
如果文件是字典,字典的 key 会作为测试用例方法的后缀显示,字典的 value 会作为测试用例参数。
如下图为我的数据文件,且文件中数据类型为字典

首先我们先熟悉下 file_data() 读取字典数据的应用
注意:测试用例形参要与 json 文件中的键一致
from ddt import ddt, file_data
import unittest
@ddt
class Test(unittest.TestCase):
@file_data("D:\work_doc\CodeFile\dcs_class6\data.json")
def test01(self, username, password):
print(username, password)
if __name__ == '__main__':
unittest.main()#
运行结果如下
"""
Ran 3 tests in 0.004s
Evan 123456
Lvan 123456
Alex 123456
"""实例如下
import timefrom ddt
import ddt, file_data
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
@ddt
class Test(unittest.TestCase):
def setUp(self) -> None:
self.dr = webdriver.Chrome()
self.dr.get("http://cms.duoceshi.cn/xxx/xxxx/xxx")
self.dr.maximize_window()
self.dr.implicitly_wait(10)
def tearDown(self) -> None:
self.dr.find_element(By.ID, "loginBtn").click()
time.sleep(2)
self.dr.quit()
def cms_login(self, username, password):
self.dr.find_element(By.ID, "userAccount").send_keys(username)
self.dr.find_element(By.ID, "loginPwd").send_keys(password)
@file_data("D:\work_doc\CodeFile\dcs_class6\data.json")
def testcase(self, username, password):
self.cms_login(username, password)
if __name__ == '__main__':
unittest.main()如下图为我的数据文件,且文件中数据类型为列表一
首先我们先熟悉下 file_data() 读取列表数据的应用
from ddt import ddt, file_data, unpack
import unittest
@ddt
class Test(unittest.TestCase):
@file_data("D:\work_doc\CodeFile\dcs_class6\data.json")
def test01(self, data):
testdata = data.split("||")
print(testdata[0], testdata[1])
if __name__ == '__main__':
unittest.main()
# 运行结果如下
"""
Ran 3 tests in 0.004s
Evan 123456
Lvan 123456
Alex 123456
"""实例如下
import time
from ddt import ddt, file_data
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
@ddt
class Test(unittest.TestCase):
def setUp(self) -> None:
self.dr = webdriver.Chrome()
self.dr.get("http://cms.duoceshi.cn/xxx/xxxx/xxxxx")
self.dr.maximize_window()
self.dr.implicitly_wait(10)
def tearDown(self) -> None:
self.dr.find_element(By.ID, "loginBtn").click()
time.sleep(2)
self.dr.quit()
def cms_login(self, username, password):
self.dr.find_element(By.ID, "userAccount").send_keys(username)
self.dr.find_element(By.ID, "loginPwd").send_keys(password)
@file_data("D:\work_doc\CodeFile\dcs_class6\data.json")
def test01(self, data):
testdata = data.split("||")
self.cms_login(testdata[0], testdata[1])
if __name__ == '__main__':
unittest.main()如下图为我的数据文件,且文件中数据类型为列表二

首先我们先熟悉下 file_data() 读取列表数据的应用
import unittest
from ddt import ddt, file_data
@ddt
class Test(unittest.TestCase):
@file_data("test_data_list.json")
def test_demo1(self, data):
print(data)
if __name__ == '__main__':
unittest.main()
# 运行结果如下
"""
Ran 3 tests in 0.004s
['admin', '123456']
['Evan', '123456']
['Lvan', '123456']
"""实例如下
from selenium import webdriver
from ddt import ddt, file_data
import unittest
from time import sleep
from selenium.webdriver.common.by import By
@ddt
class CmsLogin(unittest.TestCase):
def setUp(self) -> None:
self.dr = webdriver.Chrome()
self.dr.get("http://cms.duoceshi.cn/xxx/xxxx/xxxxx")
self.dr.maximize_window()
self.dr.implicitly_wait(10)
def tearDown(self) -> None:
sleep(2)
self.dr.quit()
def cmslogin(self, username, password):
self.dr.find_element(By.ID, "userAccount").send_keys(username)
self.dr.find_element(By.ID, "loginPwd").send_keys(password)
self.dr.find_element(By.ID, "loginBtn").click()
@file_data("test_data_list.json")
def test_cms_login(self, data):
username = data[0]
password = data[1]
self.cmslogin(username, password)
if __name__ == '__main__':
unittest.main()