use of junit.framework.Test in project junit4 by junit-team.
the class RepeatedTestTest method testRepeatedMoreThanOnce.
public void testRepeatedMoreThanOnce() {
Test test = new RepeatedTest(fSuite, 3);
assertEquals(6, test.countTestCases());
TestResult result = new TestResult();
test.run(result);
assertEquals(6, result.runCount());
}
use of junit.framework.Test in project junit4 by junit-team.
the class BaseTestRunner method getTest.
/**
* Returns the Test corresponding to the given suite. This is
* a template method, subclasses override runFailed(), clearStatus().
*/
public Test getTest(String suiteClassName) {
if (suiteClassName.length() <= 0) {
clearStatus();
return null;
}
Class<?> testClass = null;
try {
testClass = loadSuiteClass(suiteClassName);
} catch (ClassNotFoundException e) {
String clazz = e.getMessage();
if (clazz == null) {
clazz = suiteClassName;
}
runFailed("Class not found \"" + clazz + "\"");
return null;
} catch (Exception e) {
runFailed("Error: " + e.toString());
return null;
}
Method suiteMethod = null;
try {
suiteMethod = testClass.getMethod(SUITE_METHODNAME);
} catch (Exception e) {
// try to extract a test suite automatically
clearStatus();
return new TestSuite(testClass);
}
if (!Modifier.isStatic(suiteMethod.getModifiers())) {
runFailed("Suite() method must be static");
return null;
}
Test test = null;
try {
// static method
test = (Test) suiteMethod.invoke(null);
if (test == null) {
return test;
}
} catch (InvocationTargetException e) {
runFailed("Failed to invoke suite():" + e.getTargetException().toString());
return null;
} catch (IllegalAccessException e) {
runFailed("Failed to invoke suite():" + e.toString());
return null;
}
clearStatus();
return test;
}
use of junit.framework.Test in project platform_frameworks_base by android.
the class UiAutomatorInstrumentationTestRunner method getAndroidTestRunner.
@Override
protected AndroidTestRunner getAndroidTestRunner() {
AndroidTestRunner testRunner = super.getAndroidTestRunner();
testRunner.addTestListener(new TestListener() {
@Override
public void startTest(Test test) {
if (test instanceof UiAutomatorTestCase) {
((UiAutomatorTestCase) test).initialize(getArguments());
}
}
@Override
public void endTest(Test test) {
}
@Override
public void addFailure(Test test, AssertionFailedError e) {
}
@Override
public void addError(Test test, Throwable t) {
}
});
return testRunner;
}
use of junit.framework.Test in project platform_frameworks_base by android.
the class InstrumentationCoreTestRunner method getAndroidTestRunner.
@Override
protected AndroidTestRunner getAndroidTestRunner() {
AndroidTestRunner runner = super.getAndroidTestRunner();
runner.addTestListener(new TestListener() {
/**
* The last test class we executed code from.
*/
private Class<?> lastClass;
/**
* The minimum time we expect a test to take.
*/
private static final int MINIMUM_TIME = 100;
/**
* The start time of our current test in System.currentTimeMillis().
*/
private long startTime;
public void startTest(Test test) {
if (test.getClass() != lastClass) {
lastClass = test.getClass();
printMemory(test.getClass());
}
Thread.currentThread().setContextClassLoader(test.getClass().getClassLoader());
startTime = System.currentTimeMillis();
}
public void endTest(Test test) {
if (test instanceof TestCase) {
cleanup((TestCase) test);
/*
* Make sure all tests take at least MINIMUM_TIME to
* complete. If they don't, we wait a bit. The Cupcake
* Binder can't handle too many operations in a very
* short time, which causes headache for the CTS.
*/
long timeTaken = System.currentTimeMillis() - startTime;
if (timeTaken < MINIMUM_TIME) {
try {
Thread.sleep(MINIMUM_TIME - timeTaken);
} catch (InterruptedException ignored) {
// We don't care.
}
}
}
}
public void addError(Test test, Throwable t) {
// This space intentionally left blank.
}
public void addFailure(Test test, AssertionFailedError t) {
// This space intentionally left blank.
}
/**
* Dumps some memory info.
*/
private void printMemory(Class<? extends Test> testClass) {
Runtime runtime = Runtime.getRuntime();
long total = runtime.totalMemory();
long free = runtime.freeMemory();
long used = total - free;
Log.d(TAG, "Total memory : " + total);
Log.d(TAG, "Used memory : " + used);
Log.d(TAG, "Free memory : " + free);
Log.d(TAG, "Now executing : " + testClass.getName());
}
/**
* Nulls all non-static reference fields in the given test class.
* This method helps us with those test classes that don't have an
* explicit tearDown() method. Normally the garbage collector should
* take care of everything, but since JUnit keeps references to all
* test cases, a little help might be a good idea.
*/
private void cleanup(TestCase test) {
Class<?> clazz = test.getClass();
while (clazz != TestCase.class) {
Field[] fields = clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
if (!f.getType().isPrimitive() && !Modifier.isStatic(f.getModifiers())) {
try {
f.setAccessible(true);
f.set(test, null);
} catch (Exception ignored) {
// Nothing we can do about it.
}
}
}
clazz = clazz.getSuperclass();
}
}
});
return runner;
}
use of junit.framework.Test in project platform_frameworks_base by android.
the class TestCaseUtil method getTestAtIndex.
public static Test getTestAtIndex(TestSuite testSuite, int position) {
int index = 0;
Enumeration enumeration = testSuite.tests();
while (enumeration.hasMoreElements()) {
Test test = (Test) enumeration.nextElement();
if (index == position) {
return test;
}
index++;
}
return null;
}
Aggregations