浓缩咖啡测试单独通过,在套件中运行时失败 [英] Espresso test passes individually, fails when run in the suite

查看:57
本文介绍了浓缩咖啡测试单独通过,在套件中运行时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下浓缩咖啡测试.如果我自己运行它,它总是会通过,但是当我同时运行该类中的所有测试时,它总是会失败.

I have the following Espresso test. It always passes if I run it by itself, but always fails when I run all the tests in the class together.

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

What's also a bit strange is that it used to work even as part of the suite. I'm not sure why now it stopped working. It must be something I've done but I don't know what.

@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...

该视图是可见的,并且是滚动视图的后代,这可以通过在单独运行时通过而得到证明.

The view is visible and a descendant of a scroll view, as evidenced by it passing when run on it's own.

(在套件中)进行此测试时,它不会滚动.但是当我自己运行它时,它滚动就很好.

When it gets to this test (in the suite) it just doesn't scroll. But when I run it by itself it scrolls just fine.

在另一个堆栈溢出问题中,我最近问过

In another stack overflow question I asked recently Espresso not starting Activity the same for second iteration in parameterised test, I found out that onDestroy from the previous test was getting called after onResume in the current test, causing it to set a value to null and fail the test. Again in that situation, the problem was that the test passed by itself but not in the suite. I now have a similar problem but no obvious way to fix it. (Edit: the workaround for the other question can no longer be applied).

有任何想法吗?会以某种方式读取错误的活动吗?好像它正在查看上一个测试中的那个.这听起来很荒谬,但是在遇到最后一个问题之后,我似乎有可能.

Any ideas anyone? Could it be reading the wrong Activity somehow? Like maybe it's looking at the one from the previous test. That sounds ridiculous but after that last problem I had it seems possible.

编辑:确定,作为套件的一部分运行此测试时,实际上该图像不可见,导致测试失败.我使用调试器并手动滚动视图找到了它.但是为什么呢?

Edit: Ok it turns out that when running this test as part of the suite, the image is in fact not visible causing the test to fail. I found this using the debugger and scrolling the view manually. But why?

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

I think it's a bug and have logged an issue here:

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

推荐答案

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

It can able to solve using Orchestrator android testing utility library, It will executing each test case independently, so there is no chance of breaking in test suite

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

启用build.gradle

Enabling using 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

这篇关于浓缩咖啡测试单独通过,在套件中运行时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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