Can someone shed some light on how to organize tests in test suites, using JUnit in Android? I find almost all examples to be non-working, and I'm wondering what it is that I'm not getting.
I've made a small example with an AndroidTestCase class containing a couple of tests, and a test suite which includes all the tests in the package. This works (apparently):
The test case class containing the tests:
public class ArithmeticsTest extends AndroidTestCase {
SomeClass sctest;
protected void setUp () throws Exception {
sctest = new SomeClass();
super.setUp();
}
/* Test the SomeClass.addNumbers (int, int) method: */
public void testAddNumbers () {
assertEquals(9, sctest.addNumbers(3, 6));
}
/* Test the SomeClass.addNumbers (int, int) method: */
public void testSubtractNumbers () {
assertEquals(2, sctest.subtractNumbers(6, 4));
}
protected void tearDown () throws Exception {
super.tearDown();
}
}
The test suite class including all the tests in the package:
import junit.framework.Test;
import android.test.suitebuilder.TestSuiteBuilder;
public class ProjectTestSuite_AllTests {
public static Test suite () {
return new TestSuiteBuilder(ProjectTestSuite_AllTests.class)
.includeAllPackagesUnderHere()
.build();
}
}
As mentioned, this works. However, if I have many test cases, both classes with several tests, and other classes with other tests (it makes sense to organize different types of tests, imo), then I'd like to create test suites that include specific tests for specific classes etc, so they can be run exclusively.
I've seen examples on this, but I've not gotten anyone to work. Does anyone have a good example? That is, a new test suite which includes specific test cases from one or more of the test case classes.
Update:
The following two examples work:
Run all the tests in the package:
public static Test suite () {
return new TestSuiteBuilder(ProjectTestSuite_AllTests.class)
.includeAllPackagesUnderHere()
.build();
}
Run all the tests in a specific class of tests (AndroidTestCase class):
public static Test suite () {
Class testClass = ArithmeticsTests.class;
TestSuite suite = new TestSuite(testClass);
return suite;
}
However, this does NOT work:
public static Test suite () {
TestSuite suite= new TestSuite();
suite.addTest(new ArithmeticsTest("testAddNumbers"));
suite.addTest(new ArithmeticsTest("testSubtractNumbers"));
return suite;
}
The last (non-working) example is from developer.android.com. The error I get when trying this is:
The constructor ArithmeticsTests(String) is undefined
Which is strange, since I'm not trying to access a constructor, but name a specific test method in the test case class (testAddNumbers).
UPDATE Jan. 11:
Here's the test case class containing a couple of tests:
public class ArithmeticsTests extends AndroidTestCase {
SomeClass sctest;
protected void setUp () throws Exception {
sctest = new SomeClass();
super.setUp();
}
public void testAddNumbers () {
assertEquals(9, sctest.addNumbers(3, 6));
}
public void testSubtractNumbers () {
assertEquals(2, sctest.subtractNumbers(6, 4));
}
protected void tearDown () throws Exception {
super.tearDown();
}
}
And here's the test suite class in which I try to include (f.ex.) ONE of the tests in the above class:
public class ProjectTestSuite_SomeTests {
public static Test suite () {
// This does not work:
TestSuite suite= new TestSuite();
suite.addTest(new ArithmeticsTests("testAddNumbers"));
return suite;
}
}
The addTest line above results in this error:
The constructor ArithmeticsTests(String) is undefined
Here's the SomeClass file I try to test in this example:
public class SomeClass {
int classInt;
String classString;
Item item;
public SomeClass () {}
public SomeClass (int ci, String cs) {
this.classInt = ci;
this.classString = cs;
}
public int addNumbers (int num1, int num2) {
return num1+num2;
}
public int subtractNumbers (int num1, int num2) {
return num1-num2;
}
}