开发者

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


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"


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜