android – 仅在应用程序100%完成时才进行仪器测试后正确清理/拆卸
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android – 仅在应用程序100%完成时才进行仪器测试后正确清理/拆卸,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3541字,纯文字阅读大概需要6分钟。
内容图文
![android – 仅在应用程序100%完成时才进行仪器测试后正确清理/拆卸](/upload/InfoBanner/zyjiaocheng/775/2c97849107754544a4523f67b3d9b5ff.jpg)
我有一堆端到端的仪器测试(依赖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.
内容总结
以上是互联网集市为您收集整理的android – 仅在应用程序100%完成时才进行仪器测试后正确清理/拆卸全部内容,希望文章能够帮你解决android – 仅在应用程序100%完成时才进行仪器测试后正确清理/拆卸所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。