Android 测试分片

Android Test Sharding

谁能解释一下 android 中的测试分片意味着什么? 如果有人可以分享任何教程,那将非常有帮助。

分片这个词的意思是整体的一小部分。 sharding是如何只根据一个数字进行分片的,我应该根据什么来指定shardIndex?

开发者文档中的定义。

Test sharding

The test runner supports splitting a single test suite into multiple shards, so you can easily run tests belonging to the same shard together as a group, under the same Instrumentation instance. Each shard is identified by an index number. When running tests, use the -e numShards option to specify the number of separate shards to create and the -e shard index option to specify which shard to run.

For example, to split the test suite into 10 shards and run only the tests grouped in the second shard, use the following command:

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

测试分片允许您将测试平均分成几组。分片索引是您 "percentage" 所在的组 运行ning。组的划分方式是任意的,因为分片的目的是并行化您的测试。

例如,假设您有 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 项测试,并且现在可以处理结果。

要了解其工作原理的细节,请查看 ShardingFilter inside https://android.googlesource.com/platform/frameworks/testing/+/2fe8aed7542ee05ce504d69656475d1948e9c5b2/androidtestlib/src/com/android/test/runner/TestRequestBuilder.java

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

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

ANDROID_SERIAL 值可以更改为从 adb devices

中提取的设备 ID