1.参数化简介
参数化是自动化测试的一种常用技巧,测试人员可以将测试代码中的某些变量的输入使用参数来代替。我们以测试百度搜索功能为例,每次测试搜索功能,都要测试搜索框中输入的不同的搜索内容,在进行这个测试过程中,除了搜索框中的数据在变化,测试的步骤也是重复的,这时就可以使用参数化的方式来解决测试数据变化,测试步骤不变的问题。参数化就是把测试需要用到的参数写到数据集合里,让测试程序自动从这个集合里面取数据,同时每条数据都生成一个对应的测试用例。
2.参数化使用方法
我们使用 Appium 测试框架编写测试用例时,通常会结合单元测试框架一起使用。使用测试框架的参数化机制,可以减少代码重复。参数化的使用方法是,在测试代码前添加装饰器完成测试数据的传输。示例代码如下(Python 版和 Java 版)。
Python 版本
@pytest.mark.parametrize("argvnames",argvalues)
复制代码
Java 版本
@ParameterizedTest@ValueSource(strings = argvalues)
复制代码
不同编程语言提供的单元测试框架爱支持的参数传递方式也不一样,但都会在测试用例上添加一个装饰器用以帮助参数化的实现,以 Python 语言提供的单元测试框架 pytest 为例,pytest 自带了参数化功能,在测试用例上添加参数化需要用到装饰器 @pytest.mark.parametrize(),同时需要传入两个参数“argvnames” 与 “argvalues”,第一个参数需要一个或者多个变量来接收列表中的每组数据,第二个参数传递存储数据的列表。测试用例需要使用同名的字符串接收测试数据(与“argvnames” 里面的名字一致),且列表有多少个元素就会生成并执行多个测试用例。下面示例使用参数化定义 3 组数据,每组数据都存放在一个数据序列中。分别将数据序列传入(test_input,expected)参数中,示例代码如下(Python 版和 Java 版)。
Python 版本
# content of test_expectation.pyimport pytest
@pytest.mark.parametrize("test_input,expected",[("3+5",8),("2+4",6),("6+9",42)])def test_eval(test_input,expected): assert eval(test_input == expected)
复制代码
运行结果如下:
...test_expectation.py ..Ftest_input = '6+9',expected = 42 @pytest.mark.parametrize("test_input,expected", [("3+5",8),("2+4",6),("6+9",42)]) def test_eval(test_input,expected):> assert eval(test_input) == expected
E AssertionError:assert 54 == 42E + where 54 = eval('6*9')
test_expection.py:6:AssertionError
复制代码
Java 版本
public class BookParamTest{ @ParameterizedTest @MethodSource("intProvider") void testWithExplicitLocalMethodSource(int first,int second,int sum){ assertEquals(first + second , sum); } static Stream<Arguments> intProvider(){ return Stream.of( arguments(3,5,8), arguments(3,5,6), arguments(6,9,42) ); }}
复制代码
运行结果如下:
...org.opentest4j.AssertionFailedError:Expected : 8Actual : 6<Click to see difference>
at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)at org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:62)at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:150)at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:145)at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:527)...
复制代码
从上面的运行结果可以看出,执行的 3 条测试用例分别对应 3 组数据,但测试步骤完全相同,只是传入的测试数据发生了变化。
3.案例演示
本测试案例使用“雪球”应用,打开雪球 App,点击 App 页面上的搜索输入框,且在搜索输入框中输入“alibaba”关键词,然后在关键词联想出来的列表里点击“阿里巴巴”项(见图 5-19),选择股票分类,获取股票类型为“BABA”的股票价格,最后验证价格在预期的 10%范围浮动。
这个案例使用了参数化机制和 Hamcrest 断言机制,核心代码如下(Python 版和 Java 版。)
(1)Python 演示代码
from appium import webdriverimport pytestfrom hamcrest import *
class TestXueqiu: #省略 #参数化 @pytest.mark.parametrize("keyword,srock_type,expect_price",[ ('alibaba','BABA',170) ('xaiomi','01810',8.5) ]) def test_search self,keyword,stock_type,expect_price): # 点击搜索项 self.driver.find_element_by_id("home_search").click() # 向搜索框中输入"keyword" self.driver.find_element_by_id( "com.xueqiu.android:id/search_input_text" ).send_keys(keyword) #点击搜索结果 self.driver.find_element_by_id("name").click() #获取价格 price = float(self.driver.find_element_by_xpath( "//*[contains(@resource-id,'stockCode')\ and @text ='%s']/../../..\ //*[contains(@resource-id,'current_price')]" % stock_type ).text ) # 使用断言 assert_that(price,close_to(expect_price,expect_price * 0.1)) ...
复制代码
(2)Java 演示代码
import org.junit.jupiter.params.ParameterizedTest;import org.junit.jupiter.params.provider.Arguments;import org.junit.jupiter.params.provider.MethodSource;import java.util.stream.Stream;
import static org.hamcrest.MathcherAssert.assertThat;import static org.hamcrest.Matchers.closeTo;import static org.junit.jupiter.params.provider.Arguments.arguments;
public class XueqiuTest{ //省略 @ParameterizedTest @MethodSource void testSearch(String keyword,String stockType,float expectPrice){ //点击搜索项 driver.findElement(By.id("home_search")).click(); //向搜索框输入"keyword" driver.findElement(By.id("com.xueqiu.android:id/search_input_text"\ )).sendKeys(keyword); //点击搜索结果 driver.findElement(By.id("name")).click(); //获取价格 String format = String.format("//*[contains(@resource-id,\ 'stockCode' and @text='%s')]/../../..//*[contains(@resource-id,\ 'current_price')]",stockType); String text = driver.findElement(By.xpath format)).getText(); double price = Double.parseDouble(text); assertThat(price , closeTo(expectPrice,expectPrice * 0.1)); } static Stream<Arguments> testSearch(){ return Stream.of( arguments("alibaba","BABA",170), arguments("xiaomi","01810",8.5) ); } }
复制代码
上面的代码传入了两组测试数据,每组有 3 个数据,分别为搜索关键词、股票类型和股票价格。在执行测试用例时,分别将两组数据传入测试代码中,用以搜索不同的关键词,并使用 Hamcrest 实现股票价格的断言。
搜索微信公众号:TestingStudio 霍格沃兹的干货都很硬核
评论