parameterized是python的一个参数库,支持unittest,Nose,pytest单元测试框架
1.安装parameterized(https://github.com/wolever/parameterized)
DOS上直接输入: python -m pip install parameterized即可在线安装
2.以百度搜索为例:
import unittest
from time import sleep
from parameterized import parameterized
from selenium import webdriver
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(2)
# 通过 parameterized实现参数化
@parameterized.expand([
("case1", "selenium"),
("case2", "unittest"),
("case3", "paramenterized"),
]) # @parameterized.expand()来装饰测试用例
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) # verbosity=2输出更详细的执行日志
运行结果如下:
来源:oschina
链接:https://my.oschina.net/u/4387530/blog/3390314