How do I create Android test suite which only runs specified tests in one or more classes?
Asked Answered
C

3

10

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;
    }
}
Coffeecolored answered 9/1, 2013 at 11:22 Comment(4)
Also, the main topic of this question seems drifting here and there with each intermediate edit. It is, I would say, unfair to start along the lines "please help, I cannot run Android tests and also cannot find any tutorial" and after ten minutes to start talking about something completely different. Also experienced developers try first to explain that is being asked.Quell
Edit? I haven't edited the question at all. However, I'll try editing it to make it more clear. But when looking at it, I don't see immediately where the question is unclear? I'm asking the following: How do I create test suites which does NOT run all tests in a package, but specify exactly which tests in one or more classes to run?Coffeecolored
I have simply changed the header, assuming the good faith.Quell
Ah, so the header was the problem. Thanks :-)Coffeecolored
F
0

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.

Falsecard answered 29/10, 2013 at 11:3 Comment(0)
I
1

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)

Incautious answered 23/5, 2013 at 10:3 Comment(0)
F
0

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.

Falsecard answered 29/10, 2013 at 11:3 Comment(0)
R
0

I found a workaround to achieve what you want.

Check this out: Android TestSuite: Include all TestCases except some explicitly defined

Rossi answered 25/7, 2014 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.