自動化測試之 ddt 驅動 json 文件

1、上篇文章咱們使用了 unittest + ddt 驅動 excel 文件作數據驅動測試,本篇文章咱們採用 unittest + ddt 驅動 json 文件來實現數據驅動測試,話很少說上源碼。。。web

  • ddt.file_data:裝飾測試方法,參數是文件名。文件能夠是 json 或者 yaml 類型。
    • 注意:若是文件是以 「.yml」或者".yaml" 結尾,ddt 會做爲 yaml 類型處理,其餘文件都會做爲 json 文件處理。  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 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()
  • 以下圖爲個人數據文件,且文件中數據類型爲列表

  • 首先咱們先熟悉下 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()
相關文章
相關標籤/搜索