在实际测试过程中,有很多测试步骤一样但参数不同的情况。这种情况下,我们可以通过参数化实现编写一个测试,该测试用例可以完成所有相同步骤的测试。
参数化需要用到pytest的装饰器:@pytest.mark.parametrize()
参数化实例:
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from hamcrest import *
import pytest
class TestHamcrest:
def setup(self):
desire_cap = {
"platformName": "Android",
"deviceName": "mvq8aaw4ca5tcamn",
"appActivity": ".view.WelcomeActivityAlias",
"appPackage": "com.xueqiu.android",
"skipServerInstallation": "true",
"noReset": "true",
"unicodeKeyBoard": "true",
"resetKeyBoard": "true"
}
self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desire_cap)
self.driver.implicitly_wait(10)
def teardown(self):
pass
@pytest.mark.parametrize('searchkey,stockCode,expect_price', [
('千百度', '01028', 265),
('小米', '01810', 28)
])
def test_param(self, searchkey, stockCode, expect_price):
self.driver.find_element(MobileBy.ID, "com.xueqiu.android:id/home_search").click()
self.driver.find_element(MobileBy.ID, "com.xueqiu.android:id/search_input_text").send_keys(searchkey)
self.driver.find_element(MobileBy.XPATH,
'//*[@resource-id="com.xueqiu.android:id/listview"]/android.widget.RelativeLayout[1]').click()
price = self.driver.find_element(MobileBy.XPATH,
f"//*[@text={stockCode}]/../../..//*[@resource-id='com.xueqiu.android:id/current_price']")
current_price = float(price.text)
print(current_price)
print(current_price)
print(current_price)
assert_that(current_price, close_to(expect_price, expect_price * 0.1))
- appium的配置中:
-
"unicodeKeyBoard": "true", "resetKeyBoard": "true"
- 是为了能在send_key时输入中文字符,结束后恢复原来的键盘状态。
-