简体   繁体   中英

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

如果您使用的是Eclipse,并且只想运行单个测试方法而不是整个套件或整个测试类,则只需右键单击方法名称,然后选择“运行方式”。->“ Android JUnit Test”

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.

要使用JUnit选择性跳过测试用例,可以在您不想运行的测试方法上方添加@Ignore批注。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM