Espresso Test 8: Test Rule


这是 Espresso UI 测试系列的第八篇文章。这篇主要是讲述 idling resources 在 debug 和 release 中设置,TestRule 的设置 的测试。

代码来源于 youtube 视频:https://www.youtube.com/watch?v=55BsY7mfIhI&list=PLgCYzUzKIBE_ZuZzgts135GuLQNX5eEPk&index=18

1.0 TestWatcher

TestWatcher 是继承 TestRule,我们复写它的几个常用方法有,

  • succeeded,在测试成功的时候调用
  • failed, 在测试失败的时候调用
  • starting,在测试开始之前调用
  • finished,在测试之后调用
public abstract class TestWatcher implements TestRule {

    ...


    /**
     * Invoked when a test succeeds
     */
    protected void succeeded(Description description) {
    }

    /**
     * Invoked when a test fails
     */
    protected void failed(Throwable e, Description description) {
    }


    /**
     * Invoked when a test is about to start
     */
    protected void starting(Description description) {
    }

    /**
     * Invoked when a test method finishes (whether passing or failing)
     */
    protected void finished(Description description) {
    }

}

在我们的例子中是在测试开始前注册 EspressoIdlingResource, 在测试介绍之后反注册 EspressoIdlingResource

// EspressoIdlingResourceRule.kt

class EspressoIdlingResourceRule : TestWatcher() {

    override fun starting(description: Description?) {
        IdlingRegistry.getInstance().register(EspressoIdlingResource.countingIdlingResource)
        super.starting(description)
    }

    override fun finished(description: Description?) {
        IdlingRegistry.getInstance().unregister(EspressoIdlingResource.countingIdlingResource)
        super.finished(description)
    }
}

2.0 EspressoIdlingResource 的 release 和 debug 设置

在这里插入图片描述

Debug 中的 EspressoIdlingResourceRule**

class EspressoIdlingResourceRule : TestWatcher() {

    override fun starting(description: Description?) {
        IdlingRegistry.getInstance().register(EspressoIdlingResource.countingIdlingResource)
        super.starting(description)
    }

    override fun finished(description: Description?) {
        IdlingRegistry.getInstance().unregister(EspressoIdlingResource.countingIdlingResource)
        super.finished(description)
    }

}

Release 中的 EspressoIdlingResourceRule

object EspressoIdlingResource {

    fun increment() {

    }

    fun decrement() {

    }
}

根据编译的类型,自己可以选 Active Build Variant 的版本为 debug 或者 release.
如果选择的是 debug, 则编译进去的是 Debug 中的 EspressoIdlingResourceRule, 这样 EspressoIdlingResource 能起到作用。
如果选择的是 release, 则编译进去的是 Release 中 EspressoIdlingResourceRule。 这时 increment 和 decrement 里面都是空方法,这样对 release 的逻辑没有影响。

在这里插入图片描述


相关代码已经放置到 Github: https://github.com/yxhuangCH/EspressoDemo/tree/Espresso/IdlingResource2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个错误是因为在你的项目中没有找到 com.android.support.test.espresso:espresso-core:2.2.2 这个依赖库。你需要在你的 build.gradle 文件中添加这个依赖库,或者更新它的版本。具体操作可以参考以下步骤: 1. 打开你的项目中的 build.gradle 文件。 2. 在 dependencies 中添加以下代码: ``` androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2' ``` 3. 点击 Sync Now 按钮,等待 Gradle 同步完成。 4. 如果还是出现错误,可以尝试更新依赖库的版本,例如: ``` androidTestImplementation 'com.android.support.test.espresso:espresso-core:3..2' ``` 然后再次点击 Sync Now 按钮,等待 Gradle 同步完成。 希望这些步骤可以帮助你解决这个问题。 ### 回答2: 这是一个涉及到安卓支持库的错误。在开发安卓应用时,我们经常使用安卓支持库,因为这些库提供了许多有用的特性和功能。然而,有时候这些库可能无法正确地导入或编译。导致这种情况的原因可能有很多,我们需要一步步地分析。 首先,我们需要确定是否正确地在build.gradle文件中声明了依赖关系。在这个特定的错误消息中,我们可以看到指示我们所需的库的名称和版本号。因此,我们需要检查我们的build.gradle文件,确保我们正确地声明了依赖关系。我们应该能够找到类似这样的代码: ``` dependencies { ... androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2' ... } ``` 如果没有找到这样的代码,我们需要手动添加它。如果找到这样的代码,确保它没有被注释掉或者有任何语法错误。 如果我们已经正确地声明了依赖关系,我们需要检查我们的构建环境。有时候我们的Android Studio环境可能会出现问题,导致无法正确编译库。在这种情况下,我们可以尝试清除Gradle缓存并重新编译。为此,我们可以在Android Studio的菜单中找到“Build” -> “Clean Project”和“Build” -> “Rebuild Project”选项。尝试使用这些选项可能会解决问题。 最后,如果以上方法都没有起作用,可能需要升级或降级所需的库的版本。在这种情况下,我们可以转到Maven仓库网站,查找版本与我们的构建环境和其他库之间的兼容性。我们也可以查看安卓支持库的文档,找到适合我们应用程序的最新版本。 总之,出现这种错误,我们需要逐步检查所有可能出现问题的地方,并使用适当的资源和工具来解决问题。 ### 回答3: 这个错误提示是在使用Android Studio构建应用时出现的。它表明在Gradle构建脚本中引入的Espresso测试框架的核心库无法被解析,即可能没有找到它或者版本不正确。 解决这个问题的方法如下: 1.检查Gradle构建脚本:确保在dependencies部分正确地引入了Espresso测试框架的核心库。例如: dependencies { ... androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' } 2.检查仓库配置:确认项目的仓库配置是否正确,可以在项目根目录的build.gradle文件中添加Google Maven仓库: allprojects { repositories { maven { url 'https://maven.google.com' } jcenter() } } 3.检查版本号:如果上述两个问题都已排除,则可能是版本号不正确。检查Espresso核心库版本是否正确。您可以在以下网址中查找最新版本: https://developer.android.com/training/testing/espresso/setup 4.同步Gradle:如果仍然无法解决问题,则尝试同步Gradle。选择Android Studio菜单中的File > Sync Project with Gradle Files。 总之,要解决这个错误,需要仔细检查Gradle脚本、仓库配置和版本号,并确保正确同步Gradle。这些方法通常可以解决大多数与Espresso测试框架的构建错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值