Android Espresso:按顺序运行多个测试

时间:2014-06-10 22:58:16

标签: android testing android-testing android-espresso

我尝试使用Espresso for Android进行一系列测试。看来,在运行之间,活动不会被关闭。无论一次测试后的应用程序状态如何,它都会进行下一次测试。

我需要从一个全新的应用程序开始运行我的每个测试。在Robotium中,这是使用tearDown()方法中的solo.finishOpenedActivites()处理的。
http://robotium.googlecode.com/svn/doc/com/robotium/solo/Solo.html#finishOpenedActivities()

如何使用Espresso实现这一目标?

3 个答案:

答案 0 :(得分:8)

错误报告中提供的修复程序的问题是,这只会在完成整个套件时执行。如果您希望在每次测试后都有一个干净的活动堆栈,则需要手动执行某些操作。我写了一个小课程,与上面提到的故障单上的修复程序非常相似,但可以在任何时间点执行。

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import com.google.android.apps.common.testing.testrunner.ActivityLifecycleMonitor;
import com.google.android.apps.common.testing.testrunner.ActivityLifecycleMonitorRegistry;
import com.google.android.apps.common.testing.testrunner.Stage;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;


public final class ActivityFinisher implements Runnable {

    public static void finishOpenActivities() {
        new Handler(Looper.getMainLooper()).post(new ActivityFinisher());
    }

    private ActivityLifecycleMonitor activityLifecycleMonitor;

    public ActivityFinisher() {
        this.activityLifecycleMonitor = ActivityLifecycleMonitorRegistry.getInstance();
    }

    @Override
    public void run() {
        final List<Activity> activities = new ArrayList<Activity>();

        for (final Stage stage : EnumSet.range(Stage.CREATED, Stage.STOPPED)) {
            activities.addAll(activityLifecycleMonitor.getActivitiesInStage(stage));
        }

        for (final Activity activity : activities) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }
}

答案 1 :(得分:3)

SebastianGröbler写的解决方案完美无缺。 尽管如此,这些库已经在最新的Espresso和Android测试库中重命名,这里是为Espresso 2.x编写的相同类。

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.support.test.runner.lifecycle.ActivityLifecycleMonitor;
import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import android.support.test.runner.lifecycle.Stage;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;


public final class ActivityFinisher implements Runnable {

    public static void finishOpenActivities() {
        new Handler(Looper.getMainLooper()).post(new ActivityFinisher());
    }

    private final ActivityLifecycleMonitor activityLifecycleMonitor;

    private ActivityFinisher() {
        this.activityLifecycleMonitor = ActivityLifecycleMonitorRegistry.getInstance();
    }

    @Override
    public void run() {
        final List<Activity> activities = new ArrayList<Activity>();

        for (final Stage stage : EnumSet.range(Stage.CREATED, Stage.STOPPED)) {
            activities.addAll(activityLifecycleMonitor.getActivitiesInStage(stage));
        }

        for (final Activity activity : activities) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }
}

答案 2 :(得分:2)

GoogleInstrumentation实际上会尝试完成所有已打开的活动,但由于存在一个愚蠢的错误而无法正常执行:https://code.google.com/p/android-test-kit/issues/detail?id=66

问题中链接的补丁修复了这个问题。