標籤:
原文地址:http://blog.csdn.net/duancanmeng/article/details/7458851
第一步:在AndroidManifest.xml中加入如下兩段代碼:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pccw" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <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> <!—添加代碼1--> <uses-library android:name="android.test.runner"/></application> <!—添加代碼2--> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.pccw" android:label="aaa"/></manifest>
1. <uses-library android:name="android.test.runner"/>代表把單元測試架構中的一些依賴庫引入進來
2. <instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="com.pccw" android:label="aaa"/>代表登錄區測試架構的啟動裝置,啟動裝置有好幾個類,可以選擇,一般情況下我們使用上面這個。
3. targetPackage與上面的package相同,代表單元測試架構和當前應用是處於同一個進程中
第二步:編寫商務邏輯,即需要被測試的模組
public class PersonService { public void save(String name){ String sub = name.substring(6); } public int add(int a, int b){ return a+b; }}
第三步:編寫單元測試代碼
public class PersonServiceTest extends AndroidTestCase { public void testSave() throws Exception { PersonService service = new PersonService(); service.save(null); } public void testAdd() throws Exception { PersonService service = new PersonService(); int result = service.add(1, 2); Assert.assertEquals(3, result); }}
Android學習筆記:對Android應用進行單元測試