Search in sources :

Example 21 with Test

use of junit.framework.Test in project platform_frameworks_base by android.

the class TestRunner method run.

/*
    This class determines if more suites are added to this class then adds all individual
    test classes to a test suite for run
     */
public void run(String className) {
    try {
        mClassName = className;
        Class clazz = mContext.getClassLoader().loadClass(className);
        Method method = getChildrenMethod(clazz);
        if (method != null) {
            String[] children = getChildren(method);
            run(children);
        } else if (mRunnableClass.isAssignableFrom(clazz)) {
            Runnable test = (Runnable) clazz.newInstance();
            TestCase testcase = null;
            if (test instanceof TestCase) {
                testcase = (TestCase) test;
            }
            Throwable e = null;
            boolean didSetup = false;
            started(className);
            try {
                if (testcase != null) {
                    testcase.setUp(mContext);
                    didSetup = true;
                }
                if (mMode == PERFORMANCE) {
                    runInPerformanceMode(test, className, false, className);
                } else if (mMode == PROFILING) {
                    //Need a way to mark a test to be run in profiling mode or not.
                    startProfiling();
                    test.run();
                    finishProfiling();
                } else {
                    test.run();
                }
            } catch (Throwable ex) {
                e = ex;
            }
            if (testcase != null && didSetup) {
                try {
                    testcase.tearDown();
                } catch (Throwable ex) {
                    e = ex;
                }
            }
            finished(className);
            if (e == null) {
                passed(className);
            } else {
                failed(className, e);
            }
        } else if (mJUnitClass.isAssignableFrom(clazz)) {
            Throwable e = null;
            //Create a Junit Suite.
            JunitTestSuite suite = new JunitTestSuite();
            Method[] methods = getAllTestMethods(clazz);
            for (Method m : methods) {
                junit.framework.TestCase test = (junit.framework.TestCase) clazz.newInstance();
                test.setName(m.getName());
                if (test instanceof AndroidTestCase) {
                    AndroidTestCase testcase = (AndroidTestCase) test;
                    try {
                        testcase.setContext(mContext);
                        testcase.setTestContext(mContext);
                    } catch (Exception ex) {
                        Log.i("TestHarness", ex.toString());
                    }
                }
                suite.addTest(test);
            }
            if (mMode == PERFORMANCE) {
                final int testCount = suite.testCount();
                for (int j = 0; j < testCount; j++) {
                    Test test = suite.testAt(j);
                    started(test.toString());
                    try {
                        runInPerformanceMode(test, className, true, test.toString());
                    } catch (Throwable ex) {
                        e = ex;
                    }
                    finished(test.toString());
                    if (e == null) {
                        passed(test.toString());
                    } else {
                        failed(test.toString(), e);
                    }
                }
            } else if (mMode == PROFILING) {
                //Need a way to mark a test to be run in profiling mode or not.
                startProfiling();
                junit.textui.TestRunner.run(suite);
                finishProfiling();
            } else {
                junit.textui.TestRunner.run(suite);
            }
        } else {
            System.out.println("Test wasn't Runnable and didn't have a" + " children method: " + className);
        }
    } catch (ClassNotFoundException e) {
        Log.e("ClassNotFoundException for " + className, e.toString());
        if (isJunitTest(className)) {
            runSingleJunitTest(className);
        } else {
            missingTest(className, e);
        }
    } catch (InstantiationException e) {
        System.out.println("InstantiationException for " + className);
        missingTest(className, e);
    } catch (IllegalAccessException e) {
        System.out.println("IllegalAccessException for " + className);
        missingTest(className, e);
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(junit.framework.Test)

Example 22 with Test

use of junit.framework.Test in project platform_frameworks_base by android.

the class TestRunner method start.

/**
	 * Starts a test run. Analyzes the command line arguments and runs the given
	 * test suite.
	 */
public TestResult start(String[] args) throws Exception {
    String testCase = "";
    String method = "";
    boolean wait = false;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("-wait"))
            wait = true;
        else if (args[i].equals("-c"))
            testCase = extractClassName(args[++i]);
        else if (args[i].equals("-m")) {
            String arg = args[++i];
            int lastIndex = arg.lastIndexOf('.');
            testCase = arg.substring(0, lastIndex);
            method = arg.substring(lastIndex + 1);
        } else if (args[i].equals("-v"))
            System.err.println("JUnit " + Version.id() + " by Kent Beck and Erich Gamma");
        else
            testCase = args[i];
    }
    if (testCase.equals(""))
        throw new Exception("Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class");
    try {
        if (!method.equals(""))
            return runSingleMethod(testCase, method, wait);
        Test suite = getTest(testCase);
        return doRun(suite, wait);
    } catch (Exception e) {
        throw new Exception("Could not create and run test suite: " + e);
    }
}
Also used : Test(junit.framework.Test)

Example 23 with Test

use of junit.framework.Test in project platform_frameworks_base by android.

the class ListTestCaseNames method getTestNames.

/** 
     * Returns a list of test class and method names for each TestCase in suite.  
     */
public static List<TestDescriptor> getTestNames(TestSuite suite) {
    List<Test> tests = Collections.<Test>list(suite.tests());
    ArrayList<TestDescriptor> testNames = new ArrayList<TestDescriptor>();
    for (Test test : tests) {
        if (test instanceof TestCase) {
            String className = test.getClass().getName();
            String testName = ((TestCase) test).getName();
            testNames.add(new TestDescriptor(className, testName));
        } else if (test instanceof TestSuite) {
            testNames.addAll(getTestNames((TestSuite) test));
        }
    }
    return testNames;
}
Also used : TestSuite(junit.framework.TestSuite) Test(junit.framework.Test) TestCase(junit.framework.TestCase) ArrayList(java.util.ArrayList)

Example 24 with Test

use of junit.framework.Test in project platform_frameworks_base by android.

the class InstrumentationTestRunnerTest method testMultipleTestClass.

public void testMultipleTestClass() throws Exception {
    String classArg = PlaceHolderTest.class.getName() + "," + PlaceHolderTest2.class.getName();
    mInstrumentationTestRunner.onCreate(createBundle(InstrumentationTestRunner.ARGUMENT_TEST_CLASS, classArg));
    Test test = mStubAndroidTestRunner.getTest();
    assertContentsInOrder(ListTestCaseNames.getTestNames((TestSuite) test), new TestDescriptor(PlaceHolderTest.class.getName(), "testPlaceHolder"), new TestDescriptor(PlaceHolderTest2.class.getName(), "testPlaceHolder2"));
}
Also used : TestSuite(junit.framework.TestSuite) Test(junit.framework.Test) SmallTest(android.test.suitebuilder.annotation.SmallTest) TestDescriptor(android.test.suitebuilder.ListTestCaseNames.TestDescriptor)

Example 25 with Test

use of junit.framework.Test in project platform_frameworks_base by android.

the class TestCaseUtilTest method testCreateTestForTestSuiteWithSuiteMethod.

public void testCreateTestForTestSuiteWithSuiteMethod() throws Exception {
    Test test = TestCaseUtil.createTestSuite(TwoTestsInTestSuite.class);
    assertEquals(2, test.countTestCases());
}
Also used : Test(junit.framework.Test)

Aggregations

Test (junit.framework.Test)112 TestSuite (junit.framework.TestSuite)44 TestCase (junit.framework.TestCase)22 ArrayList (java.util.ArrayList)13 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 Method (java.lang.reflect.Method)12 AssertionFailedError (junit.framework.AssertionFailedError)12 TestListener (junit.framework.TestListener)11 TestResult (junit.framework.TestResult)11 TestDescriptor (android.test.suitebuilder.ListTestCaseNames.TestDescriptor)10 SmallTest (android.test.suitebuilder.annotation.SmallTest)10 Enumeration (java.util.Enumeration)10 RepeatedTest (junit.extensions.RepeatedTest)7 AndroidTestRunner (android.test.AndroidTestRunner)5 Field (java.lang.reflect.Field)5 List (java.util.List)5 IOException (java.io.IOException)3 PrintWriter (java.io.PrintWriter)2 JUnit4TestAdapter (junit.framework.JUnit4TestAdapter)2 GridAbstractTest (org.apache.ignite.testframework.junits.GridAbstractTest)2