5

When I create parameterized test cases in JUnit 3.x, I usually create a TestSuite with something like

public static Test suite() {
    TestSuite s = new TestSuite();

    for (int i = MIN; i < MAX; ++i) {
        s.addTest(new MyTest(i));
    }
}

This suite() method is called correctly when running JUnit from a desktop command-line. When I tried this with my Android test project, the tests don't run. How do I get my tests to run on the emulator? Or is there a different way to create parameterized tests for Android?

More thoughts:

Typically I run my tests with the command line:

adb shell am instrument -w [-e class <fully qualified test class name>[#<test method name>()]] <Android package name>/android.test.InstrumentationTestRunner

This allows me to select which tests to run from my test suite. Ideally, I want to run the the parameterized tests in this way as well. The link in the comment from @Appu describes building a separate app that runs JUnit tests. As part of that, this app has a custom TestRunner. I can very likely borrow these ideas to create a TestRunner which I can use in place of android.test.InstrumentationTestRunner. This seems like a lot of work for a not uncommon task. I prefer not to reinvent the wheel if there is already a similar solution in the Android API. Does anyone know of such a thing? Also, other alternative solutions will be helpful.

Nevermind, it looks like @dtmilano already posted this as an answer...

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 1
    [This](http://mylifewithandroid.blogspot.in/2008/11/junit-in-android.html) might be of help. It's one of my favorite blogs. – Kanth Feb 22 '13 at 06:11
  • @Appu Thanks. Feel free to post that as an answer. You deserve at least an upvote for it ;-) – Code-Apprentice Feb 22 '13 at 06:13
  • So do you for your question +1. But I can post this as an answer if it really helped you solve your issue. – Kanth Feb 22 '13 at 06:16
  • @Appu I'll let you know when I take the time to read it more thoroughly ;-) – Code-Apprentice Feb 22 '13 at 06:19
  • Okay. Fine.This was the question you forgot to ask ;) Hope others would answer that solves it in the exact way. That is why I didn't post it as an answer. – Kanth Feb 22 '13 at 06:34

3 Answers3

2

You can implement a test runner to be able to pass parameters to Android tests. See the example at how to pass an argument to a android junit test (Parameterized tests).

Community
  • 1
  • 1
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • After I edited my question, I noticed you basically posted something similar to where my thoughts had lead me. I don't know how I missed this reply. I'm usually quite thorough about checking my notifications ;-( – Code-Apprentice Feb 26 '13 at 00:40
  • Now that I looked at the `InstrumentationTestRunner` javadocs, I see that I can override the `getTestSuite()` and/or `getAllTests()` methods to insert tests into the test suite. This way I can use a constructor or other solution to pass the parameters to my test case, rather than adding a getter to my customized InstrumentationTestRunner. – Code-Apprentice Feb 26 '13 at 00:50
2

Or is there a different way to create parameterized tests for Android?

We (Square) wrote a library called Burst for this purpose. If you add enum parameters in your test constructor, Burst's test runner will generate a test for each combination of enum values. For example:

public class ParameterizedTest extends TestCase {
  enum Drink { COKE, PEPSI, RC_COLA }

  private final Drink drink;

  // Nullary constructor required by Android test framework
  public ConstructorTest() {
    this(null);
  }

  public ConstructorTest(Drink drink) {
    this.drink = drink;
  }

  public void testSomething() {
    assertNotNull(drink);
  }
}
Daniel Lubarov
  • 7,796
  • 1
  • 37
  • 56
1

Quite a while after originally writing this question, I discovered that I can directly run a test class which contains a static suite() method:

adb shell am instrument -w -e class <fully qualified test class name> <Android package name>/android.test.InstrumentationTestRunner

However, the test suite doesn't run when I try to run all the tests in a given package.

Of course, this has been a while. Now I am using Android Studio instead of the command-line. I can still run the test class individually, but it still doesn't run when I select a package or try to run all of my tests.

A potential alternative is to write a master test class with a suite() method which adds all the tests to the returned TestCase. Unfortunately, this requires some manually editing every time I add a new test class to my suite.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268