今天学习了parameterized参数化库,但是执行下面这段很简单的代码时却让我很疑惑。老是报这个错误:
报错的代码:
from parameterized import parameterized
import unittest
from time import sleep
from selenium import webdriver
class TestBaiduData(unittest.TestCase):
"""TestBaiduData"""
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.base_url = "https://www.baidu.com"
#通过Parameterized实现参数化
@parameterized.expand([
("case1","selenium"),
("case2","unittest"),
("case3","parameterized")]
)
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)
def test_search(self,name,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)
思考了半天,最后发现只要将baidu_search()方法放到@parameterized.expand()之前就可以了。
from parameterized import parameterized
import unittest
from time import sleep
from selenium import webdriver
class TestBaiduData(unittest.TestCase):
"""TestBaiduData"""
@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)
#通过Parameterized实现参数化
@parameterized.expand([
("case1","selenium"),
("case2","unittest"),
("case3","parameterized")]
)
def test_search(self,name,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)
结果: