Android系統單元測試方法(15)

發表于:2011-06-30來源:未知作者:領測軟件測試網采編點擊數: 標簽:
applicationandroid:icon= @drawable/icon android:label= @string/app_name activityandroid:name= .TestApp android:label= @string/app_name /activity /application application android:icon=@drawable/icon an
  1. <application android:icon="@drawable/icon" android:label="@string/app_name">   
  2.   
  3. <activity android:name=".TestApp" android:label="@string/app_name">   
  4.   
  5. ……   
  6.   
  7. </activity>   
  8.   
  9. </application>  


這樣,在啟動程序的時候就會先啟動一個Application,然后在此Application運行過程中根據情況加載相應的 Activity,而Activity是需要一個界面的。但是Instrumentation并不是這樣的。你可以將Instrumentation理解為一種沒有圖形界面的,具有啟動能力的,用于監控其他類(用Target Package聲明)的工具類。任何想成為Instrumentation的類必須繼承android.app.Instrumentation。下面是這個類的解釋:

Base class for implementing application instrumentation code. When running with instrumentation turned on, this class will be instantiated for you before any of the application code, allowing you to monitor all of the interaction the system has with the application. An Instrumentation implementation is described to the system through an AndroidManifest.xml's <instrumentation> tag.

對于單元測試,我們需要認真了解的就是android.test.InstrumentationTestRunner類。這是Android單元測試的主入口。它相當于JUnit當中TestRunner的作用。

那么如何加載它呢,首先要在manifest文件中加入一行關于Instrumentation的聲明。比如Android Api Demos中的測試里的manifest是這么寫的(我濾掉了所有的注釋):

Java代碼 復制代碼 收藏代碼
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.   
  3. package="com.example.android.apis.tests">   
  4.   
  5. <application>   
  6.   
  7. <uses-library android:name="android.test.runner" />   
  8.   
  9. </application>   
  10.   
  11. <instrumentation android:name="android.test.InstrumentationTestRunner"  
  12.   
  13. android:targetPackage="com.example.android.apis"  
  14.   
  15. android:label="Tests for Api Demos."/>   
  16.   
  17. </manifest>  
...

熱門標簽

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97