1

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

Marco10
  • 87
  • 2
  • 8

4 Answers4

1

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.

Thomas Bouron
  • 613
  • 3
  • 11
  • Let me see if I get it: - With both AndroidTestRunner I can run/simulate an execution of my app; - With Roboeletric I can run the tests in my IDE, without simulating the entirely execution of an app; So, if I want to run a set of **N** simulations of my app, I could use both solutions? And how do both approaches know when a test ended? – Marco10 Mar 27 '14 at 13:02
  • 1
    The test runner, no matter which one, will execute all test classes (your set of tests) that are within the test directory (under /src ant based projects, under /src/androidTest for gradle based). It finishes when the execution of each test methods are executed, nothing more. – Thomas Bouron Mar 27 '14 at 14:11
1

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

Takahiko Kawasaki
  • 18,118
  • 9
  • 62
  • 105
  • Yes, thank you!! That's a lot like my scratch of how to do it... My only concern is how to make the tests vary in input (that is, how to have different values for the variables) – Marco10 Mar 27 '14 at 16:51
  • As written in javadoc, BaseRootActivity#onCreate(Bundle) is the 'logical' starting point of an application. This means that onCreate() is called every time your application (re)starts. So, override onCreate(Bundle) and prepare a different variable set there. Just FYI: The 'physical' starting point of an application is BaseApplication#onCreate(). See https://github.com/TakahikoKawasaki/nv-android-base/blob/master/src/main/java/com/neovisionaries/android/app/BaseApplication.java – Takahiko Kawasaki Mar 27 '14 at 17:09
0

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

Community
  • 1
  • 1
Dima
  • 158
  • 6
  • Yes, I already though of a script, but how? A _bash script_ that starts and kills the app? And how do I know when to kill the app? – Marco10 Mar 27 '14 at 13:08
  • 1
    its possible by using monkeyrunner, but is that what you need? – Dima Mar 27 '14 at 13:12
  • I'm not that much familiar with the monkeyrunner tool... From what I read in the last 10 min, I can use it to install and start an app or test package, that would be interesting... But i don't know if I can stop the app, or even if the tools controls when to stop it. Can you tell me more about the tool, while I read more about it? – Marco10 Mar 27 '14 at 13:23
0

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.

eleven
  • 6,779
  • 2
  • 32
  • 52
  • It's a little more complex than running an app with parameters... You see, the behaviour of an app depends on the values that the variables take, right? I want to simulate the execution of my app with different values for them, but one test should start automatically after the last one. – Marco10 Mar 27 '14 at 13:06
  • @Marco10 You're talking about test framework. They are already developed. And I'm sure that there is no any reason to create your own one. – eleven Mar 27 '14 at 13:37