Espresso 测试单独通过,在套件中运行时失败

2024-04-25

我有以下浓缩咖啡测试。如果我单独运行它,它总是通过,但当我一起运行类中的所有测试时,它总是失败。

还有一点奇怪的是,它曾经作为套件的一部分工作。我不知道为什么现在它停止工作了。这一定是我做过的事,但我不知道是什么。

@Test
public void itemHasImage_ShowsImage() {
    closeSoftKeyboard();
    if (mItem.getImageUrl() != null) {
        onView(withId(R.id.edit_item_image)).perform(scrollTo())
            .check(matches(isDisplayed()));
    }
}

我收到的错误是:

Error performing 'scroll to'...
...
Caused by: java.lang.RuntimeException: Action will not be performed
because the target view does not match one or more of the following
constraints:(view has effective visibility=VISIBLE and is descendant
of a: (is assignable from class: class android.widget.ScrollView...

该视图是可见的,并且是滚动视图的后代,正如它在单独运行时传递所证明的那样。

当进行此测试(在套件中)时,它只是不滚动。但当我单独运行它时,它滚动得很好。

在我最近问的另一个堆栈溢出问题中在参数化测试中,Espresso 未启动与第二次迭代相同的活动 https://stackoverflow.com/questions/42434112/espresso-not-starting-app-the-same-for-second-iteration-in-parameterised-test,我发现上一个测试中的 onDestroy 在当前测试中的 onResume 之后被调用,导致其将值设置为 null 并导致测试失败。同样,在这种情况下,问题是测试本身通过了,但在套件中却没有通过。我现在有类似的问题,但没有明显的方法来解决它。 (Edit:无法再应用其他问题的解决方法)。

有人有什么想法吗?难道它以某种方式读取了错误的 Activity 吗?就像也许它正在查看之前测试中的那个。这听起来很荒谬,但在我遇到最后一个问题之后,这似乎是可能的。

Edit:好吧,事实证明,当作为套件的一部分运行此测试时,图像实际上不可见,导致测试失败。我使用调试器并手动滚动视图发现了这一点。但为什么?


我认为这是一个错误,并在这里记录了一个问题:

https://code.google.com/p/android/issues/detail?id=235247 https://code.google.com/p/android/issues/detail?id=235247


它可以使用Orchestrator android测试实用程序库来解决,它将独立执行每个测试用例,因此没有机会破坏测试套件

  • 使用 AndroidJUnitRunner 版本 1.0 或更高版本时,您可以访问名为 Android Test Orchestrator 的工具,它允许您在自己的 Instrumentation 调用中运行每个应用程序的测试。

启用使用 build.gradle

    android {
  defaultConfig {
   ...
   testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
 }

  testOptions {
    execution 'ANDROID_TEST_ORCHESTRATOR'
  }
}

dependencies {
  androidTestImplementation 'com.android.support.test:runner:1.0.1'
  androidTestUtil 'com.android.support.test:orchestrator:1.0.1'
}

欲了解更多信息:https://developer.android.com/training/testing/junit-runner.html#using-android-test-orchestrator https://developer.android.com/training/testing/junit-runner.html#using-android-test-orchestrator

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Espresso 测试单独通过,在套件中运行时失败 的相关文章

随机推荐