android - 仅当应用程序 100% 完成时,才在仪器测试后正确清理/拆卸

标签 android android-espresso

我有一堆端到端的仪器测试(依赖于 Espresso)启动我们的启动器 Activity ,然后在我们的应用程序中导航(最终创建几个 Activity )。在每个 测试结束时,我们的@After 注释拆卸方法执行一些清理。

我们遇到的问题是,在测试完成(断言成功或失败)后,应用程序仍在“运行”,因此一些清理实际上导致应用程序崩溃。如果断言成功,这会导致误报,或者隐藏测试失败(我们只看到崩溃而不是失败的断言)。

这是一个例子:

import android.app.Instrumentation;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;

import com.example.SplashActivity;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static android.support.test.InstrumentationRegistry.getInstrumentation;

public class ExampleTest {

    @Rule
    public ActivityTestRule<SplashActivity> splashActivityTestRule
            = new ActivityTestRule<>(SplashActivity.class, true, false);

    Instrumentation.ActivityMonitor splashActivityMonitor;

    @Before
    public void setUp() {
        splashActivityMonitor = new Instrumentation.ActivityMonitor(SplashActivity.class.getName(), null, false);
        getInstrumentation().addMonitor(splashActivityMonitor);
    }

    @Test
    public void someTest() throws Exception {
        // ... other test-specific setup before starting splash activity

        // start first activity
        splashActivityTestRule.launchActivity(new Intent());

        // a bunch of espresso steps that result in several other activities
        // ... creating and adding Instrumentation.ActivityMonitor for each one

        // assert something
    }

    @After
    public void tearDown() {
        // clear shared prefs to prepare for next test
        PreferenceManager.getDefaultSharedPreferences(InstrumentationRegistry.getTargetContext())
                .edit()
                .clear()
                .apply();

        // At this point the app is still running. Maybe a UI is still loading that was not relevant to the test, 
        // or some mock web request is in flight. But at some point after the final assert in our test, the app needs
        // to get something from shared prefs, which we just cleared, so the app crashes.
    }
}

如您所见,应用程序在拆卸方法期间仍在运行。我们在此处对应用程序状态所做的任何更改都可能导致应用程序崩溃。

那么,在执行此清理操作之前,我如何断言该应用已停止运行?

我提出的一些可能(但丑陋)的解决方案:

在最后断言之后,继续导航回到应用程序中的某个中性点(即使用 espresso 退出并返回启动画面)。这应该可行,但它会向每个 测试添加许多其他步骤。此外,我不确定如果断言失败,这是否有效。

或者在拆解中执行某种应用终止:

public void tearDown() {
    // finish all tasks before cleaning up
    ActivityManager activityManager =
            (ActivityManager) InstrumentationRegistry.getTargetContext().getSystemService(Context.ACTIVITY_SERVICE);

    List<ActivityManager.AppTask> appTasks = activityManager.getAppTasks();
    for (ActivityManager.AppTask appTask : appTasks) {
        appTask.finishAndRemoveTask();
    }

    // clear shared prefs to prepare for next test
    PreferenceManager.getDefaultSharedPreferences(InstrumentationRegistry.getTargetContext())
            .edit()
            .clear()
            .apply();
}

更新:

我知道我可以使用 ActivityTestRule.afterActivityFinished() docs但我认为这不适用于多个 actvities。

最佳答案

您描述的问题可以使用AndroidTestOrchestrator 解决。来自 Android 官方文档:

When using AndroidJUnitRunner version 1.0 or higher, you have access to a tool called Android Test Orchestrator, which allows you to run each of your app's tests within its own invocation of Instrumentation.

被测应用程序将在每次测试运行后自动清理。

启用 AndroidTestOrchestrator

build.gradle 文件示例:

  1. AndroidTestOrchestrator 与 Android 支持库一起使用 - github link
  2. AndroidTestOrchestrator 与 AndroidX 测试库结合使用 - github link

官方 Android 文档 - link .

关于android - 仅当应用程序 100% 完成时,才在仪器测试后正确清理/拆卸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54079434/

相关文章:

android - 当我从 Marshmallow 中的应用程序设置更改权限时,应用程序停止响应

java - 在 android 中使用 robolectric 和 flavor 进行测试期间的类重复

android - 多个匹配语句时 Espresso 测试失败,但使用较小的匹配语句通过

Android - 测试谷歌地图信息窗口点击

android - Espresso 可以在不启动 Activity 的情况下工作吗?

java - 意式 Espresso 。 NoSuchMethodError UsageTracker

android - react native : Could not find support-vector-drawable. aar

android - 这个安卓 Activity 不工作

android - support.v7.widgets 和 android.widgets 的区别 Toolbar, Fragment

android - Espresso - typeText 不工作