Android 奇怪的测试 fragment

标签 android junit adb sharding instrumentation

我正在 Android 上试验测试 fragment ,我得到了非常奇怪的结果:

+ adb -s emulator-5580 shell am instrument -e numShards 2 -e shardIndex 0 -e class com.package.etc.automation.Tests.SanityTest.SanityTest -w com.package.etc.test/android.support.test.runner.AndroidJUnitRunner

com.package.etc.automation.Tests.SanityTest.SanityTest:..........

Time: 306.578

OK (10 tests)


+ adb -s emulator-5582 shell am instrument -e numShards 2 -e shardIndex 1 -e class com.package.etc.automation.Tests.SanityTest.SanityTest -w com.package.etc.test/android.support.test.runner.AndroidJUnitRunner

com.package.etc.automation.Tests.SanityTest.SanityTest:......................

Time: 645.723

OK (22 tests)

如您所见,adb 将测试分成两个不均衡的组。第二个的测试次数是第一个的两倍,执行的时间也是第一个的两倍。如果你问我,这不是最好的并行性。

是否有可能控制测试的分布,或者至少强制 adb 平均分配测试?

最佳答案

让我们追查一下。

当测试套件为 started 时, TestRequestBuilder建立在 JUnit Filters 之上. ShardingFilter是其中之一,is added .添加它意味着之前添加的Filter"intersected"使用新的方法 public boolean shouldRun(Description description)被调用。如果你看它,更有可能是这个 fragment :

if (description.isTest()) {
    return (Math.abs(description.hashCode()) % mNumShards) == mShardIndex;
}

用您的数字 (numShards=2) 代替,您会注意到,这只是一个奇偶校验。从统计上讲,生成的 HashCode 奇偶校验分布可能不是 50%。此外,当您的测试类中的某些测试被忽略、禁用并与已启用的测试交织在一起时,您甚至可以更加干扰特定方法 hashcode(Junit Description uniqueId 是从方法和类名生成)。

那只是统计数据。正如您在 this answer 中看到的那样:

How the groups are divided is arbitrary

关于Android 奇怪的测试 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45418773/

相关文章:

spring - 为什么我无法将 Controller 实例 Autowiring 到此 Spring Boot 应用程序的 JUnit 类中?找不到@SpringBootConfiguration

android - 从 ADB screenrecord 捕获的屏幕视频导致黑屏

android - 如何在 adb shell 中使用 find 命令

java - 编译时类名未知时调用方法

java - 使用 GridLayoutManager 和 RecyclerView 更改自动适应屏幕的列数

java - 使用 junit 进行迭代测试

java - 我可以在测试运行时跳过 JUnit 测试吗?

Android Studio 无法识别我的设备

java - 递归和 ram 洪水 : Starting a Gradle Daemon, 1 忙 6 停止 守护进程无法重用,使用 --status 了解详情

java - 将一个对象发送到另一个对象内的数组