Android Espresso:按顺序运行多个testing

我正在尝试使用Espresso for Android进行一系列testing。 看来在运行之间活动没有被closures。 无论一次testing后的应用程序状态如何,其左侧都会进行下一次testing。

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

这怎么可以用浓咖啡完成?

Solutions Collecting From Web of "Android Espresso:按顺序运行多个testing"

提供的错误报告修复的问题是,这只会在整个套件完成时执行。 如果你想在每次testing后有一个干净的活动堆栈,你需要手动做一些事情。 我写了一个和上面提到的票据上的修改几乎一样的小class,但是可以在任何时候执行。

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(); } } } } 

解决scheme塞巴斯蒂安Gröbler写作完美。 尽pipe如此,这些图书馆已经在最新的Espresso和Android Test Library中重新命名,这里是为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(); } } } } 

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

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