Rc N
Rc N

Reputation: 103

Robotium - how to run selective test cases?

My test class has 3APIs that run three test cases (test***()). When I run the project as JUnit test case, how stop one or more of these 3 test cases to execute? Basically, how to selectively run test cases present in the same file? {Putting them in a separate class is not really the solution!! :)}

Rc

Upvotes: 0

Views: 3351

Answers (4)

Balazs Banyai
Balazs Banyai

Reputation: 696

Since there is no @Ignore annotation in JUnit 3, I had to figure out a workaround to ignore the long-running activity/instrumentation test cases from my test suite to be able to run the unit tests:

public class FastTestSuite extends TestSuite {

    public static Test suite() {

        // get the list of all the tests using the default testSuiteBuilder
        TestSuiteBuilder b = new TestSuiteBuilder(FastTestSuite.class);
        b.includePackages("com.your.package.name");
        TestSuite allTest = b.build();


        // select the tests that are NOT subclassing InstrumentationTestCase
        TestSuite selectedTests = new TestSuite();

        for (Test test : Collections.list(allTest.tests())) {

            if (test instanceof TestSuite) {
                TestSuite suite = (TestSuite) test;
                String classname = suite.getName();

                try {
                    Class<?> clazz = Class.forName(classname);
                    if (!InstrumentationTestCase.class.isAssignableFrom(clazz)) {
                        selectedTests.addTest(test);
                    }
                } catch (Exception e) {
                    continue;
                }   
            }   
        }   
        return selectedTests;
    }   
}

Simply run this test suite as an Android JUnit test.

Upvotes: 0

anshumans
anshumans

Reputation: 4095

You can also do this from the commandline:

adb shell am instrument -e class com.android.demo.app.tests.FunctionTests#testCamera com.android.demo.app.tests/android.test.InstrumentationTestRunner

In this example, you're running only test method 'testCamera' in class FunctionTests. You can add multiple values via the -e argument.

Upvotes: 2

Eric Nordvik
Eric Nordvik

Reputation: 14746

If you are using Eclipse and just want to run a single test method instead of the whole suite or the whole test class, you just right click the method-name and choose "Run as.." -> "Android JUnit Test"

Upvotes: 4

goto10
goto10

Reputation: 4370

To selective skip test cases with JUnit you can add an @Ignore annotation above the test method(s) you don't want to run.

Upvotes: 2

Related Questions