我有一堆端到端的仪器测试(依赖于浓缩咖啡),开始我们的启动活动,然后在我们的应用程序中导航(最终创建几个活动)。结束时
每个
测试我们的
@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.
}
}
所以我怎么能断言这个应用已经死了
打扫卫生?
每一个
测试。另外,如果断言失败,我也不确定这是否有效。
或者在拆卸中执行某种应用程序终止:
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
倍数