在实际开发中,开发android软件的过程需要不断地进行测试。而使用Junit测试框架,侧是正规Android开发的必用技
术,在Junit中可以得到组件,可以模拟发送事件和检测程序处
理的正确性。
第一步:首先在AndroidManifest.xml中加入下面红色代码:
<span style="font-size:18px;"><manifest xmlns:android="http://schemas.android.com/apk/res/android"</span>
<span style="font-size:18px;">
package="cn.itcast.action“ android:versionCode="1“ android:versionName="1.0"></span>
<span style="font-size:18px;">
<application android:icon="@drawable/icon" android:label="@string/app_name"></span>
<span style="font-size:18px;">
<span style="color:#ff0000;"><uses-library android:name="android.test.runner" /></span>
....
</application></span>
<span style="font-size:18px;">
<uses-sdk android:minSdkVersion="6" /></span>
<span style="font-size:18px;">
<span style="color:#ff0000;"><instrumentation android:name="android.test.InstrumentationTestRunner"</span></span>
<span style="font-size:18px;"><span style="color:#ff0000;">
android:targetPackage="cn.itcast.action" android:label="Tests for My App" /></span></span>
<span style="font-size:18px;">
</manifest></span>
上面targetPackage指定的包要和应用的package相同。
第二步:编写单元测试代码(选择要测试的方法,右键点击“RunAs”--“AndroidJunitTest”):
<pre name="code" class="java">import android.test.AndroidTestCase;
import android.util.Log;
public class XMLTest <span style="color:#ff0000;">extends AndroidTestCase</span> {
public void testSomething() throws Throwable {
Assert.assertTrue(1 + 1 == 3);
}
}