Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 914
andreweskeclarke Avatar asked Oct 22 '10 20:10

andreweskeclarke


People also ask

How do you use Robotium?

Test an App with RobotiumStep 1 − Create a test Project in the Android Studio named as “RobotiumTest”. Choose all the default options until you reach to the main page. Step 2 − Copy the Robotium jar file into the Lib folder of the project. Step 3 − Add the dependency in build.

Where can we execute Robotium test cases?

Robotium Test cases can be executed in the Android emulator as well as the Android real device.

Is Robotium an automation?

Robotium is an Android test automation framework that has full support for native and hybrid applications.


1 Answers

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?

like image 143
Louth Avatar answered Oct 16 '22 21:10

Louth