开发者

Android TestSuite: Include all TestCases except some explicitly defined

Problem: I need to adapt the code from the Android Developer TestSuite example so that it runs all TestCases in the package except a few explicitly defined ones. Currently it just runs them all:

public class AllTests extends TestSuite {
    public static Test suite() {
        return new TestSuiteBuilder(AllTests.class)
                .includeAllPackagesUnderHere()
                .build();
    }
}

Looking at the Doc开发者_JS百科s for TestSuiteBuilder, perhaps I could adapt the above code by adding a call to TestSuiteBuilder's addRequirements() method, but I can't make heads or tails of if this will do it, or should be used to do it.

If addRequirements will and should be used to exclude AndroidTestCases, how do I call it? I don't understand what argument I'd pass, the documentation says:

addRequirements(Predicate...<TestMethod> predicates)
//Exclude tests that fail to satisfy all of the given predicates. 

But I can't find anything about the existence of a class Predicate or how it should be populated to achieve my goal.

Thanks


I wanted to exclude the InstrumentationTestCases when running unit tests during development, to be able to run the test suite without the functional tests in no time.

Here's how I did it:

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;
    }   
}


I've decided just to include all the ones I want explicitly instead.

http://developer.android.com/reference/junit/framework/TestSuite.html

TestSuite doesn't seem to have a removeTestSuite method or similar, so I can't un-include any tests that the TestSuiteBuilder would add to a test it builds. I'd be grateful if anyone could explain how to exclude/include tests using the addRequirements(...) method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜