unittest写用例之parmeterized(参数化)

mac2024-11-15  9

1.安装驱动(cmd命令) ‘’’ Created on 2019年10月10日

@author: lingh ‘’’ import unittest from selenium import webdriver from time import sleep from parameterized import parameterized class Baidu(unittest.TestCase): @classmethod def setUp(self): self.driver=webdriver.Chrome() self.base_url=“https://www.baidu.com” @classmethod def tearDown(self): self.driver.quit() def baidu_search(self,search_key): self.driver.find_element_by_id(“kw”).send_keys(search_key) self.driver.find_element_by_id(“su”).click() sleep(2) @parameterized.expand([ (“case01”,“selenium”), (“case02”,“unittest”), (“case03”,“python”) ]) def test_search(self,name,search_key): dr=self.driver dr.get(self.base_url) self.baidu_search(search_key) title=dr.title self.assertEquals(title,search_key+"_百度搜索")

if name==‘main’: unittest.main()

注意:1参数化格式: @parameterized.expand([ (“case01”,“selenium”), (“case02”,“unittest”), (“case03”,“python”) ]) 2 方法里引用参数化def test_search(self,name,search_key): 3调用对象self.baidu_search(search_key)

结果: D:\SourceCode\Eclipse-2019-06\Python\inittest_sample\test_baidu_per.py:33: DeprecationWarning: Please use assertEqual instead. self.assertEquals(title,search_key+"_百度搜索") …

Ran 3 tests in 38.026s

OK

最新回复(0)