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

5
  • 1
    This might be of help. It's one of my favorite blogs. Commented Feb 22, 2013 at 6:11
  • @Appu Thanks. Feel free to post that as an answer. You deserve at least an upvote for it ;-) Commented Feb 22, 2013 at 6:13
  • So do you for your question +1. But I can post this as an answer if it really helped you solve your issue. Commented Feb 22, 2013 at 6:16
  • @Appu I'll let you know when I take the time to read it more thoroughly ;-) Commented Feb 22, 2013 at 6: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. Commented Feb 22, 2013 at 6:34

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 ;-(
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.
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);
  }
}

3 Comments

Is the enum required to be nested inside the test class or can it be a stand-alone class or a nested enum from another class?
@Code-Apprentice it's not required, any enum would work.
Awesome! I'll add this to my list of Square libraries to learn. You guys never cease to amaze me. Thanks for all of your contributions to the Android development community.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.