运行 Android 仪器测试失败

Eup*_*r08 20 android automated-tests android-studio

我尝试为我的 Android 应用程序(Java)实现一些单元和仪器测试,我的单元测试工作正常,但仪器测试失败:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ExampleInstrumentedTest {
    @Rule
    public IntentsTestRule<MainActivity> mActivityRule = new IntentsTestRule<>(MainActivity.class);


    @Test
    public void checkIfCategoriesIsNotEmpty() {
        onView(withId(R.id.header_left_layout)).perform(click());
        onView(withId(R.id.list_view)).check(new ViewAssertion() {
            @Override
            public void check(View view, NoMatchingViewException noViewFoundException) {
                ListView list_categories = (ListView) view;
                ListAdapter adapter = list_categories.getAdapter();
                Assert.assertTrue(adapter.getCount() > 0);
            }
        });
    }

}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行测试时出现此错误:

"Run Android instrumented tests using Gradle" option was ignored because this module type is not supported yet.
Run Code Online (Sandbox Code Playgroud)

我在 build.config 文件中的实现是:

 // UNIT TEST
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.hamcrest:java-hamcrest:2.0.0.0'
    testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.7.1'

    // INSTRUMENTED TEST
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.4.0'
Run Code Online (Sandbox Code Playgroud)

小智 28

我刚刚也遇到了这个问题。我在Android Studio Bumblebee 2021.1.1 发行说明的“Android 测试”部分找到了解决方案。

它基本上相当于取消选中测试设置中使用 Gradle 运行 Android 仪器测试旁边的框。这对我有用。

Android Studio 测试设置

  • 尽管这阻止了错误消息,但在我的案例中实际上没有运行任何测试。甚至没有错误消息,它只是什么也没做。“测试结果”面板显示了用于运行测试的“adb shell”命令,但我看到模拟器上没有运行任何内容,并且“测试结果”面板中没有结果。 (19认同)
  • 我遇到了与@MichaelOsofsky 相同的问题,结果是应用程序崩溃了。检查 logcat,您应该看到堆栈跟踪 (2认同)

Eup*_*r08 2

解决方案是排除模块:protobuf-lite

 androidTestImplementation('androidx.test.espresso:espresso-contrib:3.4.0') {
        exclude module: "protobuf-lite"
    }
Run Code Online (Sandbox Code Playgroud)