Android测试分片

问题描述 投票:6回答:2

任何人都可以解释什么是android中的测试分片意味着完成?如果有人可以分享任何教程将是非常有帮助的。

单词shard意味着整体的一小部分。如何基于一个数字执行分片,以及在什么基础上我应该指定shardIndex?

在开发人员文档中定义。

测试分片

测试运行器支持将单个测试套件拆分为多个分片,因此您可以在同一个Instrumentation实例下轻松地将属于同一分片的测试作为一个组一起运行。每个分片都由索引号标识。运行测试时,使用-e numShards选项指定要创建的单独分片数,使用-e shardIndex选项指定要运行的分片。

例如,要将测试套件拆分为10个分片并仅运行在第二个分片中分组的测试,请使用以下命令:

adb shell am instrument -w -e numShards 10 -e shardIndex 2

android android-testing
2个回答
9
投票

测试分片允许您将测试分成均匀分组。分片索引是您正在运行的“百分比”组。如何划分组是任意的,因为分片的目的是并行化测试。

例如,假设您要运行60个测试,每个测试需要1分钟才能完成。如果您要在单个设备上运行它,则需要一个小时来运行所有测试。现在假设你想通过在一台设备上运行一半测试而另一半在同一时间在另一台设备上运行加速测试,因此总共只需要30分钟。

您可以通过并行运行以下ADB命令来完成此操作。

adb -s DEVICE_1_SERIAL shell am instrument -w -e numShards 2 -e shardIndex 0 > device_1_results // Runs first half of the tests
adb -s DEVICE_2_SERIAL shell am instrument -w -e numShards 2 -e shardIndex 1 > device_2_results // Runs second half of the tests

现在,您只需将负载均匀地分配到两个设备,即可在30分钟内运行所有60个测试,现在可以处理结果。

关于它如何工作的细节,请查看https://android.googlesource.com/platform/frameworks/testing/+/2fe8aed7542ee05ce504d69656475d1948e9c5b2/androidtestlib/src/com/android/test/runner/TestRequestBuilder.java内部的ShardingFilter


5
投票

您也可以通过gradle命令实现此目的。如果您通过gradle任务运行您的单元或ui tests命令将允许您根据分片编号和索引过滤测试。

ANDROID_SERIAL=emulator-5554 ./gradlew connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.numShards=3 -Pandroid.testInstrumentationRunnerArguments.shardIndex=0

ANDROID_SERIAL值可以更改为从adb设备中提取的设备ID

© www.soinside.com 2019 - 2024. All rights reserved.