1.配置AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ly.excrcise" 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=".ActivityMain" 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="3" /> <!--加下面两句--> <uses-permission android:name="android.permission.RUN_INSTRUMENTATION" />" <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="ly.exercise" android:label="Test for my app"/>" </manifest>
2.编写单元测试代码:必须继承AndroidTestCase
package ly.exercise; import junit.framework.Assert; import android.test.AndroidTestCase; public class AndroidTest extends AndroidTestCase { public void test(){ int i=4+8; Assert.assertEquals(12,i); } }