在實際開發中,開發android軟件的過程需要不斷地進行測試。而使用Junit測試框架,側是正規Android開發的必用技術,在Junit中可以得到組件,可以模擬發送事件和檢測程序處理的正確性..........
第一步:首先在AndroidManifest.xml中加入下面代碼:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package="hb.learn.junit" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <!-- 在本應用中導入需要使用的包,放在application里面activity外面 --> <uses-library android:name="android.test.runner" /> <activity android:name=".JunitTestActivity" 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> <!-- 記住這個一要放在application外面,不然會出現配置錯誤 信息 --> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="hb.learn.junit" android:label="Tests for My App" /> </manifest> |
上面targetPackage指定的包要和應用的package相同。就是這個測試類所在的包名;
第二步:編寫單元測試代碼(選擇要測試的方法,右鍵點擊“Run As”--“Android Junit Test” ):
import android.test.AndroidTestCase; import android.util.Log; public class XMLTest extends AndroidTestCase { public void testSomething() throws Throwable { Assert.assertTrue(1 + 1 == 3); } }
在實際開發中,開發android軟件的過程需要不斷地進行測試。而使用Junit測試框架,側是正規Android開發的必用技術,在Junit中可以得到組件,可以模擬發送事件和檢測程序處理的正確性.......... 第一步:首先在AndroidManifest.xml中加入下面代碼: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package="hb.learn.junit" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <!-- 在本應用中導入需要使用的包,放在application里面activity外面 --> <uses-library android:name="android.test.runner" /> <activity android:name=".JunitTestActivity" 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> <!-- 記住這個一要放在application外面,不然會出現配置錯誤 信息 --> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="hb.learn.junit" android:label="Tests for My App" /> </manifest> |
上面targetPackage指定的包要和應用的package相同。就是這個測試類所在的包名; 第二步:編寫單元測試代碼(選擇要測試的方法,右鍵點擊“Run As”--“Android Junit Test” ): import android.test.AndroidTestCase; import android.util.Log; public class XMLTest extends AndroidTestCase { public void testSomething() throws Throwable { Assert.assertTrue(1 + 1 == 3); } } |
|