问题
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;
}
}
回答1:
The issue AFAIK is that AndroidTestCase class has only one constructor - AndroidTestCase()
Specifying test cases using a string is supported only by TestCase class of JUnit but not AndroidTestCase(). That is why you are facing this issue.
回答2:
To run only a subset of test functions make your suite method look like this:
public static Test suite () {
final TestSuite suite = new TestSuite();
suite.addTest(TestSuite.createTest(ArithmeticsTests.class, "testAddNumbers"));
suite.addTest(TestSuite.createTest(ArithmeticsTests.class, "testSubtractNumbers"));
// add some more tests here or comment out one of the lines above
// if you want to skip a particular test
return suite;
}
The createTest method doesn't seem to be well documented, but it works for me: http://developer.android.com/reference/junit/framework/TestSuite.html#createTest(java.lang.Class, java.lang.String)
回答3:
can add different test classes by:
Class[] testClasses = { MathTest.class, AnotherTest.class }
TestSuite suite= new TestSuite(testClasses);
TestSuite suite= new TestSuite();
suite.addTest(new MathTest("testAdd"));
suite.addTest(new MathTest("testDivideByZero"));
TestSuite suite= new TestSuite(MathTest.class);
Link
With Android Unit and Functional tests using the Instrumentation Framework. Its documentation is now clearly explained here, These links can guide you to start testing:
Introduction to the framework and all related classes:
A list of these classes:
A sample of how to test Activities:
回答4:
I found a workaround to achieve what you want.
Check this out: Android TestSuite: Include all TestCases except some explicitly defined
来源:https://stackoverflow.com/questions/14234131/how-do-i-create-android-test-suite-which-only-runs-specified-tests-in-one-or-mor