在实际开发中,开发android软件的过程须要不断进行测试,使用Junit测试框架,是正规Android开发的必用技术,在Junit中能够获得组件,能够模拟发送事件和检测程序处理的正确性。 android
第一步:首先在AndroidManifest.xml中加入下面红色代码 app
<manifest xmlns:android=http://schemas.android.com/apk/res/android 框架
package="org.test.action" android:versionCode="1" android:versionName="1.0"> 单元测试
<application android:icon="@drawable/icon" android:label="@String/app_name"> 测试
<uses-library android:name="android:test.runner"/> spa
...... xml
</application> 事件
<uses-sdk android:minSdkVersion="6"/> 开发
<instrumentation android:name="android:test.InstrumentationTestRunner" get
android:targetPackage="cn.itcastaction" android:label="Test for My App" />
</manifest>
上面targetPackage指定的包要和应用的package相同。
第二步:编写单元测试代码(选择要测试的方法,右键点击"Run As"-"Android Junit Test"):
import android.test.AndroidTestCase;
import android.util.Log;
public class XML Test extends AndroidTestCase
{
public void testSomenthing() throws Throwable
{
Assert.asserTrue(1+1==3);
}
}