JUnit parameterized tests in an Android test project

When I create parameterized test cases in JUnit 3.x, I usually create 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 the desktop command line. When I tried this with my Android test project, the tests do not run. How to make my tests run on an emulator? Or is there another way to create parameterized tests for Android?

Other thoughts:

I usually run my tests using 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 choose which tests to run from my test suite. Ideally, I also want to run parameterized tests. The link in the comment from @Appu describes the creation of a separate application that runs JUnit tests. As part of this, this application has a custom TestRunner. I can probably borrow these ideas to create a TestRunner, which I can use instead of android.test.InstrumentationTestRunner . This seems like a lot of work for an extraordinary task. I prefer not to reinvent the wheel if the Android API already has a similar solution. Does anyone know about this? In addition, other alternative solutions will be helpful.

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

+4
source share
3 answers

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

+2
source

Or is there another way to create parameterized tests for Android?

We (Square) created the Burst library for this purpose. If you add enumeration parameters to your test constructor, runter test runst will generate a test for each combination of enum values. For instance:

 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); } } 
+2
source

Shortly after the initial writing of this question, I discovered that I could directly run a test class that contains the 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 does not start when I try to run all the tests in this package.

Of course, that was a long time ago. Now I use Android Studio instead of the command line. I can still run the test class individually, but it still does not start when I select a package or try to run all my tests.

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

+1
source

All Articles