开发小学生

导航

Parameterized-unittest参数化

"""
使用@parameterized实现参数化
"""

import unittest
from time import sleep
from selenium import webdriver
from parameterized import parameterized

PARAME = [("case1", "翻译"), ("case2", "英语"), ("case3", "音乐")]


class TestBaidu(unittest.TestCase):
'''数据驱动百度搜索测试'''

@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.base_url = "https://www.baidu.com"
cls.driver.maximize_window()

@classmethod
def tearDownClass(cls):
cls.driver.quit()

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(2)

# 通过parameterized实现参数化,按照参数顺序传参
@parameterized.expand(PARAME)
def test_search(self, name, search_key):
self.baidu_search(search_key)
self.assertEqual(self.driver.title, search_key + "_百度搜索")


if __name__ == "__main__":
unittest.main(verbosity=2

posted on 2021-08-05 22:34  开发小学生  阅读(119)  评论(0编辑  收藏  举报