您的位置:首页 > 移动开发 > Android开发

android cts and junit

2012-04-03 22:01 106 查看
Classes:from parent to child

1.

junit

package junit.framework;

public interface Test {
public abstract int countTestCases();
public abstract void run(TestResult result);
}


Assert

package junit.framework;
public class Assert {
...
assertTrue()
assertFalse()
fail()
assertEquals()
assertNotNull()
assertNull()
...
}


fail will throw error, other's function will call fail in the end.

2. TestCase

package junit.framework;
public abstract class TestCase extends Assert implements Test {

protected void runTest() throws Throwable {
assertNotNull(fName);
Method runMethod= null;
try {
// use getMethod to get all public inherited
// methods. getDeclaredMethods returns all
// methods of this class but excludes the
// inherited ones.
runMethod= getClass().getMethod(fName, (Class[]) null);
} catch (NoSuchMethodException e) {
fail("Method \""+fName+"\" not found");
}
try {
runMethod.invoke(this, (Object[]) null);
}
     ....
}
public String getName() {
return fName;
}
public void setName(String name) {
fName= name;
}
protected void setUp() throws Exception {
}

protected void tearDown() throws Exception {
}
....


TestCase has a method run(), which use java reflect to get the "Class" and "Method"

then call method.invoke() to start method.

3.InstrumentationTestCase

package android.test;
public class InstrumentationTestCase extends TestCase {
....
public final <T extends Activity> T launchActivity(
String pkg,
Class<T> activityCls,
Bundle extras) {
Intent intent = new Intent(Intent.ACTION_MAIN);
if (extras != null) {
intent.putExtras(extras);
}
return launchActivityWithIntent(pkg, activityCls, intent);
}
...
public void runTestOnUiThread(final Runnable r) throws Throwable {
  ....
}
...
protected void runTest() throws Throwable {
  ...
}

private void runMethod(Method runMethod, int tolerance) throws Throwable {
runMethod(runMethod, tolerance, false);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: