android cts and junit

本文详细介绍了JUnit框架的核心组件,包括Test接口、Assert类和TestCase类的功能与实现方式,并深入探讨了InstrumentationTestCase类及其特有的方法。

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




转载于:https://www.cnblogs.com/elfylin/archive/2012/04/03/2430559.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值