DDT: Data-Drivern Tests against unittest extension library. Allows the use of different test data to run a test , and by displaying a plurality of test cases
1. Online Download
In Dos input:
python -m pip install ddt
Successful installation results:
2. Use DDT5 different parametric manner Baidu search
test_baidu_ddt.py
import unittest from ddt import ddt, data, file_data, unpack from time import sleep from selenium import webdriver @ddt class TestBaidu(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.base_url = "https://www.baidu.com" def baidu_search(self, search_key): self.driver.get(self.base_url) self.driver.find_element_by_id("kw") .send_keys (SEARCH_KEY) self.driver.find_element_by_id ( " SU " ) .click () SLEEP ( . 3 ) # parameterization of 1: List @data ([ " Case1 " , " Selenium " ], [ " Case2 " , " the unittest " ], [ " Case3 " , " Python " ]) @unpack DEF test_search1 (Self, Case, SEARCH_KEY): Print ( " the first set of test cases: ", case) self.baidu_search (SEARCH_KEY) self.assertEqual (self.driver.title, SEARCH_KEY + " _ Baidu search " ) # parameterization of 2: tuples @data (( " Case1 " , " Selenium " ), ( " Case2 " , " the unittest ' ), ( " Case3 " , " Python " )) @unpack DEF test_search2 (Self, Case, SEARCH_KEY): Print ( " second set of test cases: " ,case) self.baidu_search (SEARCH_KEY) self.assertEqual (self.driver.title, SEARCH_KEY + " _ Baidu search " ) # parameterization of the 3: Dictionary @data ({ " SEARCH_KEY " : " Selenium " }, { " SEARCH_KEY " : " the unittest " }, { " SEARCH_KEY " : " Python " }) @unpack DEF test_search3 (Self, SEARCH_KEY): Print ( " third set of test cases: ", search_key) self.baidu_search (search_key) self.assertEqual (self.driver.title, search_key + " _ Baidu search " ) # parameterization of 4: File parametric @file_data ( " D: /Test1/test_csv_pa_ddt/ddt_data_file.json " ) DEF test_search4 (Self, SEARCH_KEY): Print ( " IV set of test cases: " , SEARCH_KEY) self.baidu_search (SEARCH_KEY) self.assertEqual (self.driver.title, SEARCH_KEY + " _ Baidu search " ) # Parametrizations 5: yaml file @file_data ( "D:/Test1/test_csv_pa_ddt/ddt_data_file.yaml") def test_search5(self, case): search_key = case[0]["search_key"] print("第五组测试用例:", search_key) self.baidu_search(search_key) self.assertEqual(self.driver.title, search_key + "_百度搜索") @classmethod def tearDownClass(cls): cls.driver.quit() if '__name__' == '__main__': unittest.main(verbosity=2)
The parametric json 4: (ddt_data_file.json)
{ "case1":{"search_key": "selenium"}, "case2":{"search_key": "unittest"}, "case3":{"search_key": "python"} }
Parametric 5 yaml: (ddt_data_file.yaml)
case1: - search_key: "python" case2: - search_key: "unittest" case3: - search_key: "ddt"
Note: If you do not install yaml, unable to perform parametric 5
Dos command-line installation:
python -m pip install pyyaml
FIG successfully installed as follows:
Finally, the results are: