在项目的AndroidManifest.xml功能清单文件中加入如下类库代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner" />
<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>
</application>
<uses-sdk android:minSdkVersion="8" />
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="cn.itcast.test" android:label="Tests for My App" />
</manifest>
被测试方法:
public class PersonService {
public int save() {
String in = "78";
int b = new Integer(in);
return b;
}
}
单元测试类:必须继承AndroidTestCase
public class PersonServiceTest extends AndroidTestCase {
private static final String TAG = "PersonServiceTest";
public void testSave() throws Throwable{
PersonService service = new PersonService();
int b = service.save();//检验save()方法运行是否正常
//Log.i(TAG, "result="+ b);
//System.out.println();
//System.err.println("result="+ b);
Assert.assertEquals(78, b);
}
}
转载于:https://blog.51cto.com/donv082303/761163