Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized JUnit tests in Android test project

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

like image 452
Code-Apprentice Avatar asked Feb 22 '13 05:02

Code-Apprentice


People also ask

How does JUnit parameterized test work?

JUnit 4 has introduced a new feature called parameterized tests. Parameterized tests allow a developer to run the same test over and over again using different values. There are five steps that you need to follow to create a parameterized test. Annotate test class with @RunWith(Parameterized.

Does JUnit support parameterized tests?

JUnit 5, the next generation of JUnit, facilitates writing developer tests with shiny new features. One such feature is parameterized tests. This feature enables us to execute a single test method multiple times with different parameters.


2 Answers

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

like image 194
Diego Torres Milano Avatar answered Sep 19 '22 15:09

Diego Torres Milano


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);
  }
}
like image 26
Daniel Lubarov Avatar answered Sep 23 '22 15:09

Daniel Lubarov