【发布时间】:2018-01-12 16:19:26
【问题描述】:
我在运行 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);
}
}
模块 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)
//edit:我仔细查看了运行命令的输出和设备上的检测运行程序。我忘了在两个地方添加.module。
【问题讨论】:
-
junit 仅包含在测试中,不包含在 androidTests 中。 (见
testImplementationvsandroidTestImplementation) -
您可以尝试编辑您的运行配置以进行测试并检查仪器和测试运行器吗?
-
@Zoe 感谢您的回答!你的意思是我漏掉了这行:
androidTestImplementation "junit:junit:4.12"我添加了它但没有变化。 -
@DavidRawson 感谢您的评论。在运行配置页面上并不是真正的选项。我认为他们被 AS3 淘汰了。 InstrumentationRunner 是通过 gradle 设置的。
-
于是我找到了运行测试的方法。我看到当我在 AS 中单击运行时,触发的 adb 调用是:
adb shell am instrument -w -r -e debug false -e class my.package.module.MainActivityTest my.package.module.test/android.support.test.runner.AndroidJUnitRunner手动调用时:adb shell am instrument -w -r -e debug false -e class my.package.module.MainActivityTest my.package.test/android.support.test.runner.AndroidJUnitRunner测试运行正常。因此,当我在运行时单击 AS 时,检测信息的包错误。我必须摆脱模块部分。
标签: android gradle junit junit4 android-testing