先简单说说什么是参数化,已百度为例平时我们测试搜索,每次我们测试一个不同的搜索内容,都需要更改参数的值。在这个过程里面,除了数据在变动以外,其他步骤都是重复的。
这个时候我们就可以使用参数化的方式来代替数据的变动。参数化顾名思义就是把不同的参数,写到一个列表里,或者说写到一个集合里面。然后让程序自动去这个列表里面取值,直到列表为空便结束。
使用方法@pytest.mark.parametrize("argnamest",argvalues)
源码参数:
parametrize(self,argnames, argvalues, indirect=False, ids=None, scope=None)
argnames:参数名称
argvalues:参数对应值,类型必须为list
单个参数格式为:("参数名",[value]
@pytest.mark.parametrize("content",[1,3])
import pytest from appium import webdriver import time # todo pytest里面类名也要已test开头 class Test_Search_(): def setup_class(self): desired_caps = { "platformName": "Android", "platformVersion": "5.1", "deviceName": "127.0.0.1:62001", "appPackage": "com.android.settings", "appActivity": ".Settings", "noreset": "True" } # todo 获得驱动对象 self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# 参数化,循环从列表里面取值 @pytest.mark.parametrize("content",[1,3]) def test_search(self,content): self.driver.find_element_by_id('com.android.settings:id/search').click() self.driver.find_element_by_id('android:id/search_src_text').send_keys(content) time.sleep(1) if __name__ == '__main__': pytest.main(["-s", "search.py"])
多个参数格式为:参数名后面跟对应的参数值
@pytest.mark.parametrize(("username", "password"), [("1585", "mg123456"), ("13929", "a12345")])
import pytest from appium import webdriver import time # todo pytest里面类名也要已test开头 class Test_Search_(): def setup_class(self): desired_caps = { "platformName": "Android", "platformVersion": "5.1", "deviceName": "127.0.0.1:62001", "appPackage": "com.android.settings", "appActivity": ".Settings", "noreset": "True" } # todo 获得驱动对象 self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) # 多个参数参数化 @pytest.mark.parametrize(("username", "password"), [("15885", "mg123456"), ("18929", "a12345")]) def test_search(self,username,password): self.driver.find_element_by_id('com.android.settings:id/search').click() self.driver.find_element_by_id('android:id/search_src_text').send_keys(username,password) time.sleep(1) if __name__ == '__main__': pytest.main(["-s", "search.py"])