unittest中使用第3方插件parameterized进行参数化

示例代码

baidu.py

# _*_ coding:utf-8 _*_
import csv,unittest #导入csv模块
from time import sleep
from selenium import webdriver
from module import baidumodule
class baidu(unittest.TestCase):
    @classmethod
    def setUpClass(cls):#类中最先执行
        cls.driver = webdriver.Chrome()
        cls.driver.maximize_window()  # 最大化窗口
        cls.driver.implicitly_wait(10)  # 隐式等待
        cls.search = baidumodule(cls.driver)  # 将driver传给aidumodule这个类
        with open("file.csv","r") as name:
            cls.lines = name.readlines()#以行读取整个文件
    @classmethod
    def tearDownClass(cls): #类中最后执行
        cls.driver.quit()

    def test_search(self):
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[0])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'selenium_百度搜索')
        sleep(2)
    def test_search1(self):
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[1])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'selenium2_百度搜索')
        sleep(2)
    def test_search2(self):
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[2])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'selenium3_百度搜索')
        sleep(2)
    def test_search3(self):
        search = self.search
        lines = self.lines
        driver = self.driver
        search.login(lines[3])
        sleep(1)
        title = driver.title
        self.assertEqual(title,'webdriver_百度搜索')
        sleep(2)

if __name__ == "__main__": #如果直接执行将执行以下代码,调用不执行以下代码
    unittest.main()

module.py

class baidumodule():
    def __init__(self,driver,):
        self.dr = driver #不能在类中再次导入webdriver 两边的driver等于两个窗口,直接让调用方传入driver即可
    def login(self,values):
        login_dr = self.dr
        login_dr.get("https://www.baidu.com/")
        login_dr.find_element_by_xpath("//*[@id='kw']").send_keys(values)
        login_dr.find_element_by_xpath("//*[@id='su']").click()

file.csv

selenium
selenium2
selenium3
webdriver

可以看到我们上面使用的参数化方法也是可行的,但是感觉挺麻烦写那么多重复代码,我们使用第三方插件parameterized来完成

github项目地址

首先在DOS中键入以下命令安装parameterized

python -m pip install parameterized

D:\ python -m pip install parameterized #根据个人配置也可能是python3

	Collecting parameterized
		  Downloading https://files.pythonhosted.org/packages/65/d4/b0b626eb263a4c2aa3ca3cd20ea3db410db837f7f6b5d3fc4a6c4bee3631/parameterized-0.6.1-py2.py3-none-any.whl
	Installing collected packages: parameterized
	Successfully installed parameterized-0.6.1

使用:

from parameterized import parameterized, param

baidu.py改写如下:

# _*_ coding:utf-8 _*_
import csv,unittest #导入csv模块
from time import sleep
from selenium import webdriver
from module import baidumodule
from parameterized import parameterized, param #导入parameterized
class baidu(unittest.TestCase):
    @classmethod
    def setUpClass(cls):#类中最先执行
        cls.driver = webdriver.Chrome()
        cls.driver.maximize_window()  # 最大化窗口
        cls.driver.implicitly_wait(10)  # 隐式等待
        cls.search = baidumodule(cls.driver)  # 将driver传给aidumodule这个类
    @classmethod
    def tearDownClass(cls): #类中最后执行
        cls.driver.quit()
        
	#开始使用parameterized
    @parameterized.expand([ #引用装饰器,准备参数,传入下方的函数
        ("selenium"), #此处为file.csv中的数据
        ("selenium2"),
        ("selenium3"),
        ("webdriver"),
    ])
    def test_search(self,search_str):#当执行到此会自动接收上面传入的参数
        aa = '_百度搜索'
        search = self.search
        driver = self.driver
        search.login(search_str)
        sleep(1)
        title = driver.title#获取当前title
        self.assertEqual(title,search_str+aa)#判断title结果
        sleep(2)

if __name__ == "__main__": #如果直接执行将执行以下代码,调用不执行以下代码
    unittest.main()

执行baidu.py结果:

....
----------------------------------------------------------------------
Ran 4 tests in 23.059s

OK

为了方便断言也可以这样写

baidu.py

# _*_ coding:utf-8 _*_
import csv,unittest #导入csv模块
from time import sleep
from selenium import webdriver
from module import baidumodule
from parameterized import parameterized, param#导入parameterized
class baidu(unittest.TestCase):
    @classmethod
    def setUpClass(cls):#类中最先执行
        cls.driver = webdriver.Chrome()
        cls.driver.maximize_window()  # 最大化窗口
        cls.driver.implicitly_wait(10)  # 隐式等待
        cls.search = baidumodule(cls.driver)  # 将driver传给aidumodule这个类
    @classmethod
    def tearDownClass(cls): #类中最后执行
        cls.driver.quit()
        
	#开始使用parameterized
    @parameterized.expand([
        ("selenium","selenium_百度搜索"), #此处为file.csv中的数据
        ("selenium2","selenium2_百度搜索"),
        ("selenium3","selenium3_百度搜索"),
        ("webdriver","webdriver_百度搜索"),
    ])
    def test_search(self,search_str,assrt_str):#接收上面的两个参数
        search = self.search
        driver = self.driver
        search.login(search_str)
        sleep(1)
        title = driver.title
        self.assertEqual(title,assrt_str)
        sleep(2)

if __name__ == "__main__": #如果直接执行将执行以下代码,调用不执行以下代码
    unittest.main()

执行结果:

....
----------------------------------------------------------------------
Ran 4 tests in 23.704s

OK

扩展阅读
ddt参数化

©️2020 CSDN 皮肤主题: 撸撸猫 设计师:设计师小姐姐 返回首页