Question

I would like to run a bunch of tests in my Android App. The thing is: I want to define first a number of tests N, and make my App run that number of times, one after another.

It's a little tricky in Android, because of the Activities lifecycle, but the goal is to start a test (execute the app again) right after when the last one stoped.

Is that possible? I tried reading the Android Developers Testing section, but I'm having some doubts about if what I want is possible with that technique.

Plus, I want to make each test execute with different values for the variables (different inputs), but that's probably even more tricky, so... let's focus in the first problem :)

Any help?? Thanks

Était-ce utile?

La solution

It sounds to me what you want is a mechanism to restart your Android application programatically (and gracefully). Many people may say it is impossible, but you can implement the mechanism.

The basic flow is:

(1) finish() your root activity.

(2) In onDestroy() of your root activity, call startActivity(createMainLauncherIntent()).

(3) And the implementation of createMainLauncherIntent() should look like the following.

private Intent createMainLauncherIntent()
{
    Intent intent = new Intent();

    // To launch this activity as if it started from the launcher.
    intent.setClass(this, getClass());
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    return intent;
}

(4) Of course, the onDestroy() should have a mechanism to avoid infinite loop of 'restart'.


A sample of base root Activity class:
https://github.com/TakahikoKawasaki/nv-android-base/blob/master/src/main/java/com/neovisionaries/android/app/BaseRootActivity.java

A sample Android application that implements 'restart' mechanism:
https://github.com/TakahikoKawasaki/nv-android-base-sample

Autres conseils

You can run any number of test for your app, you just need to specify the valid test runner. By default, the SDK provides a AndroidTestRunner that allows you to run tests for your app inside an emulator.

After that, you can also use another test runner like Robolectric that allows to run tests directly from your IDE.

You can run as may tests as you want with both solutions, there is no need to kill and restart the app between each test. Even though, it would very inefficient and time consuming to do so.

im not sure, but what about a script witch kills your app and start it again with new inputs, which could be stored in preinitialized db? here a link to how to kill your aplication: Android ADB stop application command like "force-stop" for non rooted device

You can launch app with params using shell command start and parameter -e:

$ adb shell am start -n com.some.package/com.some.package.MainActivity -e key param

Params will come to the onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Bundle extras = this.getIntent().getExtras();
    if(extras != null){
        String key = extras.getString("key");
    }
}

To kill app on android you need to have rooted device. Next command will kill app:

$ adb shell ps | grep com.some.package | awk '{print $2}' | xargs adb shell kill

But I'm sure that standard testing methods will works better. So I recommend you carefully read all Android documentation about testing.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top