1. 程式人生 > >android自動化測試之單元測試例項 .

android自動化測試之單元測試例項 .

這樣,在啟動程式的時候就會先啟動一個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程式碼