简体   繁体   English

使用Robotium的Android测试注释

[英]Android test annotations with Robotium

I'm currently building an app in Android, and using Robotium to do functional tests (By the way, don't use Robotium on anything less that Android 1.6, it is way too buggy). 我目前正在Android上构建一个应用程序,并使用Robotium进行功能测试(顺便说一句,不要在Android 1.6或更低版本的机器上使用Robotium,这太容易出错了)。

Some of these tests have a random tendency to fail, mainly Robotium missing a text field, or timing out, not reading text. 这些测试中的某些测试有随机失败的趋势,主要是Robotium缺少文本字段,或者超时而不读取文本。 I am trying to use the @FlakyTest annotation, so they will run two or three times before throwing out a failed test error. 我正在尝试使用@FlakyTest批注,因此它们将运行两次或三次,然后抛出失败的测试错误。 However, the annotation is not working, the tests do not re-run after a failure. 但是,注释不起作用,失败后不会重新运行测试。

Here is how I am using the annotation: 这是我使用注释的方式:

public class ClassName extends ActivityInstrumentationTestCase2<HomeActivity>{

        @LargeTest
        @FlakyTest(tolerance=3)
        public void testMethod(){

        //Here I run my roboitium scripts.

        }
}

Then I run it from the command line: 然后我从命令行运行它:

adb shell am instrument -w com.jayway.test/android.test.InstrumentationTestRunner 亚行工具外壳-w com.jayway.test / android.test.InstrumentationTestRunner

Neither eclipse nor the command line execution of the tests takes into account the flaky test annotation. eclipse或测试的命令行执行都不会考虑不稳定的测试注释。 Does anyone see an error with how I am trying to apply @FlakyTest ? 有人看到我尝试应用@FlakyTest吗?

In general, when writing tests for Android (with or without Robotium) you have to be much more careful. 通常,在为Android(带有或不带有Robotium)编写测试时,您必须更加小心。 You can't just say "is this visible". 您不能只说“这是可见的”。 You need to wrap everything in a "wait for" cycle, so would say "wait for this to be visible". 您需要将所有内容包装在“等待”周期中,因此会说“等待此可见”。 This is particularly a problem when running in the emulators, because sometimes things take long without any good reason. 当在仿真器中运行时,这尤其是个问题,因为有时事情会花很长时间而没有任何正当的理由。 Without the waiting cycles, you will never have a consistent run. 没有等待周期,您将永远不会获得一致的运行。 We have a few hundred tests and we have never needed to use the FlakyTest annotation. 我们有数百个测试,而且我们从来不需要使用FlakyTest批注。

I can't see any issue with your use of the @FlakyTest annotation. 我看不到您使用@FlakyTest批注的任何问题。

I put together a quick test case to test @FlakyTest and Robotium (v2.2): 我整理了一个快速测试案例来测试@FlakyTest和Robotium(v2.2):

public class FlakyTestCase extends ActivityInstrumentationTestCase2<Main> {

private static int count = 0;
private Solo solo;

public FlakyTestCase() {
    super("com.stackoverflow.example", Main.class);
}

@Override
public void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
}

@LargeTest
@FlakyTest(tolerance=3)
public void testFlaky(){
    Log.e("FlakeyTestCase", "Execution Count:" + ++count);

    solo.assertCurrentActivity(null,Main.class);
    solo.clickOnText("Doesn't Exist");

    Log.e("FlakeyTestCase", "Shouldn't make it here");
}
}

LogCat showed the following messages: LogCat显示以下消息:

Execution Count: 1
Execution Count: 2
Execution Count: 3

So the @FlakyTest annotation was definitely being invoked. 因此,肯定会调用@FlakyTest批注。 The (final) failure of the test was shown as: 测试的(最终)失败显示为:

junit.framework.AssertionFailedError: The text: Doesn't Exist is not found!

And the message "Shouldn't make it here" was never logged. 消息"Shouldn't make it here"不做"Shouldn't make it here"从未被记录过。

So as far as I can see, there is no issue with how you've declared your annotation or any problems with @FlakyTest and Robotium, v2.2 anyway. 据我所知,您如何声明注释或@FlakyTest和Robotium v​​2.2都没有问题。

Perhaps there is an issue with another part of your test code? 也许测试代码的另一部分有问题?

Robotium missing a text field, or timing out, not reading text means We have to check clearly if the text or any existed on the screen then only need to perform the actions like Robotium缺少文本字段或超时,无法阅读文本,这意味着我们必须清楚地检查屏幕上是否存在文本或任何文本,然后只需执行以下操作

if(solo.searchText("Doesn't Exist", true){
solo.clickOnText("Doesn't Exist");
}

Similar if any components like button or others we can achieve this by above logic. 如果有任何类似按钮或其他组件的组件,我们也可以通过上述逻辑来实现。

将此添加到您的代码:

import android.util.Log;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM