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

我有一堆端到端的仪器测试(依赖Espresso)启动我们的启动器活动,然后在我们的应用程序中导航(最终创建几个活动).在每次测试结束时,我们的@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,但我认为这不适用于多种行为.

最佳答案 您描述的问题可以通过使用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文件示例:

>将AndroidTestOrchestrator与Android支持库一起使用 – github link
>将AndroidTestOrchestrator与AndroidX测试库一起使用 – github link

官方Android文档 – link.

点赞