一、修改配置文件AndroidManifest.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "cn.ycmoon.test.activity" android:versionCode = "1" android:versionName = "1.0" > < uses-sdk android:minSdkVersion = "8" /> < 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 > < instrumentation android:name = "android.test.InstrumentationTestRunner" android:targetPackage = "cn.ycmoon.test.activity" android:label = "Testing" /> </ manifest > |
說明:1、在Application節點中Activity前添加 <uses-library android:name="android.test.runner" />
2、在Application節點的添加 <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="cn.ycmoon.test.activity" android:label="Testing" />
二、在相應包中編寫要完方法,
三、在src同級級目錄中添加一個test文件夾[可自定義],并在下建立和項目一致的包名: cn.ycmoon.test.activity
四、在要進行單元測試的方法上右鍵->NEW->JUNIT Test Case。彈出下界面:
選擇 SuperClass為:
點擊下一步,選擇要測試的方法:
點擊完成。
將新產生的單元測試的類放入test/項目包名下。
在單元測試方法中添加實現:
展開test/cn.ycmoon.test.activity下單元測試類,選擇要測試的方法,點右鍵,選擇Run As->Android JUnit Test,就會打開單元測試結果界面。
右該方法無錯誤,則結果會顯示綠色狀態條: