1、上篇文章咱們使用了 unittest + ddt 驅動 excel 文件作數據驅動測試,本篇文章咱們採用 unittest + ddt 驅動 json 文件來實現數據驅動測試,話很少說上源碼。。。web
注意:若是文件是以 「.yml」或者".yaml" 結尾,ddt 會做爲 yaml 類型處理,其餘文件都會做爲 json 文件處理。 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 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/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()
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()