技术分享 | app自动化测试(Android)-- 参数化用例

参数化是自动化测试的一种常用技巧,可以将测试代码中的某些输入使用参数来代替。以百度搜索功能为例,每次测试搜索场景,都需要测试不同的搜索内容,在这个过程里面,除了数据在变化,测试步骤都是重复的,这时就可以使用参数化的方式来解决测试数据变化,测试步骤不变的问题。

参数化就是把测试需要用到的参数写到数据集合里,让程序自动去这个集合里面取值,每条数据都生成一条对应的测试用例,直到集合里的值全部取完。

使用方法

使用 Appium 测试框架编写测试用例时,通常会结合单元测试框架一起使用。使用测试框架的参

  • Python 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">@pytest.mark.parametrize("argvnames",argvalues) </pre>

  • Java 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">@ParameterizedTest @ValueSource(strings = argvalues) </pre>

不同语言的单测框架支持的参数传递方式也不一样。一般情况,会在测试用例上添加一个装饰器,以python语言的 pytest 为例,在测试用例上添加参数化需要的装饰器 @pytest.mark.parametrize() ,这里需要传入两个参数 “argnamest” 与 “argvalues”,第一个参数需要一个或者多个变量来接收列表中的每组数据,第二个参数传递存储数据的列表。测试用例需要使用同名的字符串接收测试数据(与“argvnames”里面的名字一致),且列表有多少个元素就会生成并执行多个测试用例。下面示例使用使用参数化定义三组数据,每组数据都存放在一个元组中,分别将元组中的数据传入(test_input,expected)参数中,示例代码如下:

  • Python 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">`# content of test_expectation.py
import 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` </pre>

运行结果如下:

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">`...
test_expectation.py ..F

test_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 == 42
E + where 54 = eval('6*9')

test_expectation.py:6: AssertionError` </pre>

  • Java 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">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) ); } } </pre>

运行结果如下:

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">`...
org.opentest4j.AssertionFailedError:
Expected :8
Actual :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)
...` </pre>

上面的运行结果可以看出,执行的三条测试用例分别对应三组数据,测试步骤完全相同,只是传入的测试数据发生了变化。

案例

使用“雪球”应用,打开雪球 APP,点击页面上的搜索输入框输入“alibaba”,然后在搜索联想出来的列表里面点击“阿里巴巴”,选择股票分类,获取股票类型为“BABA”的股票价格,最后验证价格在预期价格的 10%范围浮动,另一个搜索“小米”的结果数据测试步骤类似。

这个案例使用了参数化机制和 Hamcrest 断言机制,示例代码片断如下:

[图片上传失败...(image-a40bb3-1656485877938)]

参数化核心示例代码:

Python 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">`from appium import webdriver
import pytest
from hamcrest import *

class TestXueqiu:
# 省略...
# 参数化
@pytest.mark.parametrize("keyword, stock_type, expect_price", [
('alibaba', 'BABA', 170),
('xiaomi', '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))
...` </pre>

Java 版本

<pre class="copy-codeblocks" style="font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; font-size: 15.008px; display: block; position: relative; overflow: visible; color: rgb(34, 34, 34); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">`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.MatcherAssert.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)
    );
}

}` </pre>

上面的代码,传入了两组测试数据,每组有三个数据分别为搜索关键词,股票类型,及股票价格。在执行测试用例时,分别将两组数据传入测试步骤中执行,对应搜索不同的关键词,使用 Hamcrest 来实现股票价格的断言。
获取更多相关资料戳:https://qrcode.ceba.ceshiren.com/link?name=article&project_id=qrcode&from=jianshu&timestamp=1656485903&author=wuyue

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,542评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,822评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,912评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,449评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,500评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,370评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,193评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,074评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,505评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,722评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,841评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,569评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,168评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,783评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,918评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,962评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,781评论 2 354

推荐阅读更多精彩内容