Android Espresso:使用pressBack正确测试关闭的应用 [英] Android Espresso : correctly test closing app with pressBack

查看:522
本文介绍了Android Espresso:使用pressBack正确测试关闭的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用espresso进行一些导航测试。其实,我想检查一下刚开始后是否已经通过使用主屏幕上的Back键关闭了应用程序。
这是我正在使用的一段代码。

I'm trying to implement some navigation tests with espresso. Actually i want to check if the application has been closed by the use of the Back key on the main screen, just after a fresh start. Here is a piece of code i'm using.

class NavigationTests  {
    @get:Rule
    val mActivityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java)

    @Test
    fun backOnEmptyHomeMustExit(){
        Espresso.pressBack()
        Assert.assertTrue(mActivityTestRule.activity==null)
    }
}

实际上我的考试由于以下异常:

Actually i got a test failed because of the following exception :


android.support.test.espresso.NoActivityResumedException:按下并杀死了应用程序

android.support.test.espresso.NoActivityResumedException: Pressed back and killed the app

我已经在stackoverflow中看到了一些有关使用try / catch块的主张,但我想知道是否有更合适的方法来做到这一点?

I've seen some propositions in stackoverflow about using a try/catch block but i'm wondering if there is a more proper way to do this ?

如何测试android应用已使用Espresso关闭

Android-使用pressBack的Espresso测试

编辑:
这样看来此模板是必经之路:

So it seems that this template is the way to go :

try {
    pressBack();
    fail("Should have thrown NoActivityResumedException");
} catch (NoActivityResumedException expected) { 
}


推荐答案

简短答案

使用 Espresso.pressBackUnconditionally()

我已经检查了版本3.1.1

I've checked for version 3.1.1

示例

Espresso.pressBackUnconditionally()
assertTrue(activityRule.activity.isDestroyed)

开发

您可以在 Expresso源代码它将 false 标志传递给 PressBackAction ,以便不会引发异常。

As you can see in Expresso source code it passes false flag to PressBackAction, so that it doesn't throw exception.

这篇关于Android Espresso:使用pressBack正确测试关闭的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆