简体   繁体   中英

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

Some of these tests have a random tendency to fail, mainly Robotium missing a text field, or timing out, not reading text. I am trying to use the @FlakyTest annotation, so they will run two or three times before throwing out a failed test error. 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

Neither eclipse nor the command line execution of the tests takes into account the flaky test annotation. Does anyone see an error with how I am trying to apply @FlakyTest ?

In general, when writing tests for Android (with or without Robotium) you have to be much more careful. 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.

I can't see any issue with your use of the @FlakyTest annotation.

I put together a quick test case to test @FlakyTest and 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:

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

So the @FlakyTest annotation was definitely being invoked. 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.

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.

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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