其他分享
首页 > 其他分享> > parameterized参数化库的坑之后面紧跟测试方法用例。

parameterized参数化库的坑之后面紧跟测试方法用例。

作者:互联网

今天学习了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)

结果:
在这里插入图片描述

标签:search,self,driver,parameterized,用例,化库,key,cls
来源: https://blog.csdn.net/weixin_43744103/article/details/115269591