title: How do I create Android test suite which only runs specified tests in one or more classes?
body: <p>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.</p>
  
  <p>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):</p>
  
  <p>The test case class containing the tests:</p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <p>The test suite class including all the tests in the package:</p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Update:</p>
  
  <p>The following two examples work:</p>
  
  <p>Run all the tests in the package:</p>
  
  <pre><code>public static Test suite () {
      return new TestSuiteBuilder(ProjectTestSuite_AllTests.class)
      .includeAllPackagesUnderHere()
      .build();
  }
  </code></pre>
  
  <p>Run all the tests in a specific class of tests (AndroidTestCase class):</p>
  
  <pre><code>public static Test suite () {
      Class testClass = ArithmeticsTests.class;
      TestSuite suite = new TestSuite(testClass);
  
      return suite;
  }
  </code></pre>
  
  <p>However, this does NOT work:</p>
  
  <pre><code>public static Test suite () {
      TestSuite suite= new TestSuite();
      suite.addTest(new ArithmeticsTest("testAddNumbers"));
      suite.addTest(new ArithmeticsTest("testSubtractNumbers"));
  
      return suite;
  }
  </code></pre>
  
  <p>The last (non-working) example is from developer.android.com. The error I get when trying this is:</p>
  
  <blockquote>
    <p>The constructor ArithmeticsTests(String) is undefined</p>
  </blockquote>
  
  <p>Which is strange, since I'm not trying to access a constructor, but name a specific test method in the test case class (testAddNumbers).</p>
  
  <h2>UPDATE Jan. 11:</h2>
  
  <p>Here's the test case class containing a couple of tests:</p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <p>And here's the test suite class in which I try to include (f.ex.) ONE of the tests in the above class:</p>
  
  <pre><code>public class ProjectTestSuite_SomeTests {
  
      public static Test suite () {
          // This does not work:
          TestSuite suite= new TestSuite();
          suite.addTest(new ArithmeticsTests("testAddNumbers"));
          return suite;
      }
  }
  </code></pre>
  
  <p>The addTest line above results in this error:</p>
  
  <blockquote>
    <p>The constructor ArithmeticsTests(String) is undefined</p>
  </blockquote>
  
  <p>Here's the SomeClass file I try to test in this example:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>