【问题标题】:Cannot run androidTest in certain module无法在某些模块中运行 androidTest
【发布时间】: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 中。 (见testImplementation vs androidTestImplementation
  • 您可以尝试编辑您的运行配置以进行测试并检查仪器和测试运行器吗?
  • @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


【解决方案1】:

我发现要解决这个问题。如上面的评论所述,我可以使用以下命令手动启动测试:

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。

别忘了同步和清理。

【讨论】:

  • 只能在 MainActivity 上运行吗?
  • 不,这只是一个例子。诀窍在于不同的包路径
猜你喜欢
  • 2022-09-28
  • 1970-01-01
  • 2020-09-19
  • 2020-08-08
  • 1970-01-01
  • 2018-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多