无法在某些模块中 运行 androidTest

Cannot run androidTest in certain module

我在 运行 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)

//编辑:我仔细查看了 运行 命令的输出和设备上的检测 运行ner。我忘记在两个地方添加.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工作室为我创建的运行配置执行命令:

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。

别忘了同步和清理。