Android学习系列-单元测试与程序调试(5)
1.关键点
1)AndroidManifest.xml中必须配置如下节点
<uses-library android:name="android.test.runner" />
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="cn.nt.test" android:label="Test for MyApp" />
2)AndroidManifest.xml完整代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.nt.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="android.test.runner" />
</application>
<uses-sdk android:minSdkVersion="8" />
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="cn.nt.test" android:label="Test for MyApp" />
</manifest>
3)单元测试类必须继承自AndroidTestCase
例如:public class LogTest extends AndroidTestCase{
public void testSave() throws Exception{
PersonService service = new PersonService();
service.save(null);
}
public void testAdd() throws Exception{
PersonService service = new PersonService();
int actual = service.add(1, 2);
Assert.assertEquals(8, actual);//测试结果会在单元测试窗口中显示
}
}
2.程序调试输出信息
private static final String TAG = "LogTest";
public void testOutLog() throws Throwable{
Log.i(TAG, "www.baidu.cn");
}
public void testOutLog5() throws Throwable{
Log.i(TAG, "123456");
}
public void testOutLog2() throws Throwable{
System.out.println("www.csdn.com");
}
public void testOutLog3() throws Throwable{
System.err.println("www.163.com");
}
输出信息在Logcat窗口中能够看到。
信息分类级别debug<info<warn<error.