我在运行 androidTest 时遇到问题。它找不到我的测试。奇怪的是,它只适用于某个模块。我测试并配置其 build.gradle 文件的其他模块正在运行。这里有一些信息。非常感谢任何帮助!
输出如下:
Running tests
$ adb shell am instrument -w -r -e debug false -e class my.package.MainActivityTest my.package.module.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Test running failed: Unable to find instrumentation info for: ComponentInfo{my.package.test/android.support.test.runner.AndroidJUnitRunner}
Empty test suite.
测试位于
myModule/src/androidTest/java/my/package/MainActivityTest.java
看起来如下:
@LargeTest
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void thisTestIsntExecuted() {
assertEquals(1, 2);
}
}
modules build.gradle 文件具有以下设置:
android {
defaultConfig {
(...)
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
dependencies {
(...)
testImplementation "junit:junit:4.12"
androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.1"
androidTestImplementation "com.android.support.test:runner:1.0.1"
}
设备上的检测运行器:
instrumentation:my.package.test/android.support.test.runner.AndroidJUnitRunner (target=my.package.module)
//编辑:我仔细查看了运行命令的输出和设备上的检测运行器。我忘记在两个地方添加 .module
。
最佳答案
我发现解决问题了。如上面的评论所述,我可以使用以下命令手动启动测试:
adb shell am instrument -w -r -e debug false -e class my.package.module.MainActivityTest my.package.test/android.support.test.runner.AndroidJUnitRunner
Android Studio 为我创建的运行配置执行了命令:
adb shell am instrument -w -r -e debug false -e class my.package.module.MainActivityTest my.package.module.test/android.support.test.runner.AndroidJUnitRunner
所以你可以看到路径是错误的,module
部分太多了。
此路径是 testApplicationId
并且可以通过 gradle 更改,see here .如果未设置,则通过获取 applicationId
并向其添加 .test
来自动创建该值。这导致我的路径错误,可能是因为我正在根据构建变体生成 applicationId。
不要忘记同步和清理。
关于android - 无法在某些模块中运行 androidTest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48230164/