首页 > 解决方案 > Espresso 测试未在 Android 12 上运行

问题描述

我的 Pixel 4a 已升级到 Android 12,现在,当我尝试在该设备上运行 Espresso 测试时,没有运行任何测试。Android Studio 显示“测试通过 0 通过”。没有进行任何测试。测试仍然在 Android 11 设备上按预期运行。

我的测试方法用 org.junit.Test 注释。我的测试类用 androidx.test.filters.LargeTest 注释。

我确保升级 Android Studio。我在 build.gradle 中将 compileSdkVersion 和 targetSdkVersion 更新为 31。我检查了我的测试依赖项并将它们更新为以下版本:

junit:junit:4.13.2
androidx.test.ext:junit-ktx:1.1.3
androidx.test:runner:1.4.0
androidx.test:rules:1.4.0
androidx.test.uiautomator:uiautomator:2.2.0
androidx.test.espresso:espresso-core:3.4.0
androidx.work:work-testing:2.7.0

经过所有这些更改后,问题仍然存在,我也在 AVD 上看到过。

有没有其他人遇到过这个问题?

标签: androidjunitandroid-espresso

解决方案


我如何解决 Android 12。

如果你想使用测试编排器

确保您拥有最新的协调器和测试运行器版本。

androidTestUtil 'androidx.test:orchestrator:1.4.1'
androidTestImplementation 'androidx.test:runner:1.4.0'

确保这是在您testOptionsapp/build.gradle.

execution 'ANDROIDX_TEST_ORCHESTRATOR

或者,如果您不想使用测试协调器

从 app/build.gradle 的 testOptions 中删除以下行。

 execution 'ANDROIDX_TEST_ORCHESTRATOR
 

我如何解决 Android 11。

(如果您对在 Android 12 上运行 Orchestrator 感兴趣,请在上述第一步之外执行以下步骤)

在 androidTest 文件夹中创建一个 AndroidManifest.xml 并添加以下查询权限以通过允许 Orchestrator 包与测试包通信来解决“未找到测试”

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="your.package.name">

<!-- This Manifest will only apply changes to androidTest builds -->

<queries>
    <package android:name="androidx.test.orchestrator.OrchestratorService"/>
</queries>

<application android:forceQueryable="true" />

推荐阅读