FileDocCategorySizeDatePackage
TestSuiteBuilder.javaAPI DocAndroid 5.1 API10186Thu Mar 12 22:22:42 GMT 2015android.test.suitebuilder

TestSuiteBuilder

public class TestSuiteBuilder extends Object
Build suites based on a combination of included packages, excluded packages, and predicates that must be satisfied.

Fields Summary
private android.content.Context
context
private final TestGrouping
testGrouping
private final Set
predicates
private List
testCases
private TestSuite
rootSuite
private TestSuite
suiteForCurrentClass
private String
currentClassname
private String
suiteName
Constructors Summary
public TestSuiteBuilder(Class clazz)
The given name is automatically prefixed with the package containing the tests to be run. If more than one package is specified, the first is used.

param
clazz Use the class from your .apk. Use the class name for the test suite name. Use the class' classloader in order to load classes for testing. This is needed when running in the emulator.


                                                                                                 
       
        this(clazz.getName(), clazz.getClassLoader());
    
public TestSuiteBuilder(String name, ClassLoader classLoader)

        this.suiteName = name;
        this.testGrouping.setClassLoader(classLoader);
        this.testCases = Lists.newArrayList();
        addRequirements(REJECT_SUPPRESSED);
    
Methods Summary
public final android.test.suitebuilder.TestSuiteBuilderaddRequirements(com.android.internal.util.Predicate predicates)
Exclude tests that fail to satisfy all of the given predicates. If you call this method, you probably also want to call {@link #named(String)} to override the default suite name.

param
predicates Predicates to add to the list of requirements.
return
The builder for method chaining.

        ArrayList<Predicate<TestMethod>> list = new ArrayList<Predicate<TestMethod>>();
        Collections.addAll(list, predicates);
        return addRequirements(list);
    
public android.test.suitebuilder.TestSuiteBuilderaddRequirements(java.util.List predicates)
Exclude tests that fail to satisfy all of the given predicates.

param
predicates Predicates to add to the list of requirements.
return
The builder for method chaining.

        this.predicates.addAll(predicates);
        return this;
    
private voidaddSuiteIfNecessary(java.lang.String parentClassname)

        if (!parentClassname.equals(currentClassname)) {
            currentClassname = parentClassname;
            suiteForCurrentClass = new TestSuite(parentClassname);
            rootSuite.addTest(suiteForCurrentClass);
        }
    
private voidaddTest(TestMethod testMethod)

        addSuiteIfNecessary(testMethod.getEnclosingClassname());
        suiteForCurrentClass.addTest(testMethod.createTest());
    
private voidaddTest(junit.framework.Test test)

        addSuiteIfNecessary(test.getClass().getName());
        suiteForCurrentClass.addTest(test);
    
public android.test.suitebuilder.TestSuiteBuilderaddTestClassByName(java.lang.String testClassName, java.lang.String testMethodName, android.content.Context context)

hide
pending API Council approval


        AndroidTestRunner atr = new AndroidTestRunner();
        atr.setContext(context);
        atr.setTestClassName(testClassName, testMethodName);

        this.testCases.addAll(atr.getTestCases());
        return this;
    
public android.test.suitebuilder.TestSuiteBuilderaddTestSuite(junit.framework.TestSuite testSuite)

hide
pending API Council approval

        for (TestCase testCase : (List<TestCase>) TestCaseUtil.getTests(testSuite, true)) {
            this.testCases.add(testCase);
        }
        return this;
    
public final junit.framework.TestSuitebuild()
Call this method once you've configured your builder as desired.

return
The suite containing the requested tests.

        rootSuite = new TestSuite(getSuiteName());

        // Keep track of current class so we know when to create a new sub-suite.
        currentClassname = null;
        try {
            for (TestMethod test : testGrouping.getTests()) {
                if (satisfiesAllPredicates(test)) {
                    addTest(test);
                }
            }
            if (testCases.size() > 0) {
                for (TestCase testCase : testCases) {
                    if (satisfiesAllPredicates(new TestMethod(testCase))) {
                        addTest(testCase);
                    }
                }
            }
        } catch (Exception exception) {
            Log.i("TestSuiteBuilder", "Failed to create test.", exception);
            TestSuite suite = new TestSuite(getSuiteName());
            suite.addTest(new FailedToCreateTests(exception));
            return suite;
        }
        return rootSuite;
    
public android.test.suitebuilder.TestSuiteBuilderexcludePackages(java.lang.String packageNames)
Exclude all tests in the given packages and all sub-packages, unless otherwise specified.

param
packageNames Names of packages to remove.
return
The builder for method chaining.

        testGrouping.removePackagesRecursive(packageNames);
        return this;
    
protected java.lang.StringgetSuiteName()
Subclasses use this method to determine the name of the suite.

return
The package and suite name combined.

        return suiteName;
    
protected TestGroupinggetTestGrouping()

return
the test package that represents the packages that were included for our test suite. {@hide} Not needed for 1.0 SDK.

        return testGrouping;
    
public final android.test.suitebuilder.TestSuiteBuilderincludeAllPackagesUnderHere()
Include all junit tests that satisfy the requirements in the calling class' package and all sub-packages.

return
The builder for method chaining.

        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();

        String callingClassName = null;
        String thisClassName = TestSuiteBuilder.class.getName();

        // We want to get the package of this method's calling class. This method's calling class
        // should be one level below this class in the stack trace.
        for (int i = 0; i < stackTraceElements.length; i++) {
            StackTraceElement element = stackTraceElements[i];
            if (thisClassName.equals(element.getClassName())
                    && "includeAllPackagesUnderHere".equals(element.getMethodName())) {
                // We've found this class in the call stack. The calling class must be the
                // next class in the stack.
                callingClassName = stackTraceElements[i + 1].getClassName();
                break;
            }
        }

        String packageName = parsePackageNameFromClassName(callingClassName);
        return includePackages(packageName);
    
public android.test.suitebuilder.TestSuiteBuilderincludePackages(java.lang.String packageNames)
Include all tests that satisfy the requirements in the given packages and all sub-packages, unless otherwise specified.

param
packageNames Names of packages to add.
return
The builder for method chaining.

        testGrouping.addPackagesRecursive(packageNames);
        return this;
    
public android.test.suitebuilder.TestSuiteBuildernamed(java.lang.String newSuiteName)
Override the default name for the suite being built. This should generally be called if you call {@link #addRequirements(com.android.internal.util.Predicate[])} to make it clear which tests will be included. The name you specify is automatically prefixed with the package containing the tests to be run. If more than one package is specified, the first is used.

param
newSuiteName Prefix of name to give the suite being built.
return
The builder for method chaining.

        suiteName = newSuiteName;
        return this;
    
private static java.lang.StringparsePackageNameFromClassName(java.lang.String className)

        return className.substring(0, className.lastIndexOf('."));
    
private booleansatisfiesAllPredicates(TestMethod test)

        for (Predicate<TestMethod> predicate : predicates) {
            if (!predicate.apply(test)) {
                return false;
            }
        }
        return true;