android studio 2.0建立一個空android moudle後,html
會出現兩個test目錄,其中一個是Instrument Test,android
另外一個則是Unit Testgit
Unit Test 和 Instrument Test 的區別:github
在 Android Developer 給出的 Instrument Test 解釋以下:api
Instrumented unit tests are unit tests that run on physical devices and emulators, instead of the Java Virtual Machine (JVM) on your local machine. You should create instrumented unit tests if your tests need access to instrumentation information (such as the target app’s Context) or if they require the real implementation of an Android framework component (such as a Parcelable or SharedPreferences object). Using instrumented unit tests also helps to reduce the effort required to write and maintain mock code. You are still free to use a mocking framework, if you choose, to simulate any dependency relationships. Instrumented unit tests can take advantage of the Android framework APIs and supporting APIs, such as the Android Testing Support Library。app
簡單的翻譯: Instrumented Unit test 是容許在真機或者模擬器上的,而不是運行在本地環境下的虛擬機中。若是在測試時須要使用 instrumentation information(例如 app Context),或者你須要獲取 Android 框架的組件(例如 SharedPrederences),這時候你就能夠建立一個 instrumented unit test。使用 Instrumented unit test 能夠幫助減小編寫模擬環境的代碼。固然你也可使用一些 mock 框架。使用 Instrumented unit test 能夠很好的利用 Android framework api 和 supporting API。框架
在 Android Developer 給出的 Local Unit Tests(Unit Test)解釋以下:單元測試
If your unit test has no dependencies or only has simple dependencies on Android, you should run your test on a local development machine. This testing approach is efficient because it helps you avoid the overhead of loading the target app and unit test code onto a physical device or emulator every time your test is run. Consequently, the execution time for running your unit test is greatly reduced. With this approach, you normally use a mocking framework, like Mockito, to fulfill any dependency relationships.學習
簡單的翻譯:若是你的單元測試不依賴Android環境,那麼你須要使用一個本地的單元測試,運行在本地的JVM上,這樣的話,就有利於你的測試速度,由於加載物理設備是很耗費時間的。你可使用相似於 Mockito 這種 Java 測試框架。測試
簡單的總結一下是,這些狀況適合用 Instrumented unit test :
其餘狀況優先考慮使用 Unit Test ,由於它的速度要快不少,效率也要高不少。其次,Instrumented unit test 是基於 Unit Test 的。
Unit Test 環境配置:
dependencies {
testCompile ‘junit:junit:4.12’
testCompile 「org.mockito:mockito-core:1.9.5」
}
編寫測試代碼:
配置運行:
運行結果:
tip:須要在測試方法前須要添加 @Test 註解
兩個知識點學習:
關於 Junit 4 中註解的使用:
Assert 系列方法的使用:
Junit 提供了一系列的 Assert 重載方法,提供你把預期結果(Object,long,Arrays 等類型)和實際結果進行比較。同時還有一個 AssertThat()方法,提供了一個失敗 Message 的輸出
Instrumented unit 環境配置:
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
dependencies {
androidTestCompile 'com.android.support:support-annotations:23.0.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
// Optional -- Hamcrest library
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
// Optional -- UI testing with Espresso
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
// Optional -- UI testing with UI Automator
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
}
運行配置:
運行結果:
官方連接:
http://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html#setup
Demo地址:
https://github.com/googlesamples/android-testing/tree/master/unit/BasicUnitAndroidTest