標籤:樣本 writing rest 存在 架構 oid 選擇 單擊 應用
Android測試(五):Instrumented 單元測試
發布時間 2017年12月20日 蟲師
原文:https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html
Instrumented 單元測試是在真機和模擬器上啟動並執行測試,它可以利用Android架構API和支援的API(如Android測試支援庫)。如果你的測試需要訪問工具資訊(例如目標應用程式的Context
),或者需要真正實現Android架構組件(如Parcelable
或SharedPreferences
對象),則應該建立Instrumented 單元測試。
使用Instrumented單元測試還有助於減少編寫和維護mock代碼所需的工作量。 如果你願意,仍然可以自由地使用一個mock架構類比任何依賴關係。
設定測試環境
在你的Android Studio項目中,你必須將mock測試的源檔案儲存體在module-name/src/androidTest/java/ 中。 建立新項目時該目錄已經存在,並包含範例程式碼。
在開始之前,你應該下載Android測試支援庫安裝程式,該安裝程式提供的API可讓你快速構建和運行應用程式的檢測代碼。測試支援庫包括用於功能性UI測試(Espresso和UI Automator)的JUnit 4測試回合器(AndroidJUnitRunner)和API。
還需要為項目配置Android測試依賴項,以使用測試回合程式和測試支援庫提供的規則API。 為了簡化測試開發,還應該包含Hamcrest庫,它可以讓你使用Hamcrest匹配器API建立更靈活的斷言。
在你的App的頂級build.gradle檔案中將這些庫指定為依賴項:
dependencies { androidTestCompile ‘com.android.support:support-annotations:24.0.0‘ androidTestCompile ‘com.android.support.test:runner:0.5‘ androidTestCompile ‘com.android.support.test:rules:0.5‘ // 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.2‘ // Optional -- UI testing with UI Automator androidTestCompile ‘com.android.support.test.uiautomator:uiautomator-v18:2.1.2‘}
警告: 如果構建配置包含support-annotations庫的編譯依賴項和espresso-core庫的androidTestCompile依賴項,則由於依賴衝突,構建可能會失敗。 請按照下面步驟更新對espresso-core的依賴關係:
androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2‘, { exclude group: ‘com.android.support‘, module: ‘support-annotations‘})
要使用JUnit 4測試類別,請確保將AndroidJUnitRunner指定為項目中的預設測試載入器運行器,方法是在應用程式的模組層級build.gradle檔案中包含以下設定:
android { defaultConfig { testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" }}
建立一個Instrumented的單元測試類
你的Instrumented單元測試類應該寫成JUnit 4測試類別。要瞭解有關建立JUnit 4測試類別和使用JUnit 4斷言和注釋的更多資訊,請參閱建立本地單元測試類。
要建立一個Instrumented的JUnit 4測試類別,在測試類別定義的開頭添加@RunWith(AndroidJUnit4.class)注釋。 還需要將Android測試支援庫中提供的AndroidJUnitRunner類指定為預設測試回合器。測試入門中對此步驟進行了更詳細的介紹。
以下樣本顯示如何編寫一個Instrumented單元測試,以確保LogHistory類正確實現了Parcelable介面:
import android.os.Parcel;import android.support.test.runner.AndroidJUnit4;import android.util.Pair;import org.junit.Test;import org.junit.runner.RunWith;import java.util.List;import static org.hamcrest.Matchers.is;import static org.junit.Assert.assertThat;@RunWith(AndroidJUnit4.class)@SmallTestpublic class LogHistoryAndroidUnitTest { public static final String TEST_STRING = "This is a string"; public static final long TEST_LONG = 12345678L; private LogHistory mLogHistory; @Before public void createLogHistory() { mLogHistory = new LogHistory(); } @Test public void logHistory_ParcelableWriteRead() { // Set up the Parcelable object to send and receive. mLogHistory.addEntry(TEST_STRING, TEST_LONG); // Write the data. Parcel parcel = Parcel.obtain(); mLogHistory.writeToParcel(parcel, mLogHistory.describeContents()); // After you‘re done with writing, you need to reset the parcel for reading. parcel.setDataPosition(0); // Read the data. LogHistory createdFromParcel = LogHistory.CREATOR.createFromParcel(parcel); List<Pair<String, Long>> createdFromParcelData = createdFromParcel.getData(); // Verify that the received data is correct. assertThat(createdFromParcelData.size(), is(1)); assertThat(createdFromParcelData.get(0).first, is(TEST_STRING)); assertThat(createdFromParcelData.get(0).second, is(TEST_LONG)); }}
建立一個測試套件
要組織測試單元測試的執行,可以將一組測試集合在一個測試套件類中,並將這些測試一起運行。測試套件可以嵌套; 測試套件可以將其他測試套件分組,並將所有組件測試類別一起運行。
測試套件包含在測試包中,類似於主應用程式套件組合。按照慣例,測試套件包名通常以.suite
尾碼結尾(例如,com.example.android.testing.mysample.suite
)。
以下樣本顯示了如何?名為UnitTestSuite
的測試套件,該測試套件將CalculatorInstrumentationTest
和CalculatorAddParameterizedTest
測試類別分組並運行在一起。
import com.example.android.testing.mysample.CalculatorAddParameterizedTest;import com.example.android.testing.mysample.CalculatorInstrumentationTest;import org.junit.runner.RunWith;import org.junit.runners.Suite;// Runs all unit tests.@RunWith(Suite.class)@Suite.SuiteClasses({CalculatorInstrumentationTest.class, CalculatorAddParameterizedTest.class})public class UnitTestSuite {}
運行Instrumented單元測試
要運行Instrumented測試,請遵循以下步驟:
1、通過單擊工具列中的“Sync Project”,確保您的項目與Gradle同步。。
2、以下列其中一種方式運行測試:
- 要運行單個測試請開啟Project視窗,然後單擊“Run”。
- 要測試類別中的所有方法,請按右鍵測試檔案中的類或方法,然後單擊“Run”。
- 要在目錄中運行所有測試,請按右鍵該目錄並選擇“Run Tests”。
Gradle的Android外掛程式編譯位於預設目錄(src/androidTest/java/)中的測試代碼,構建測試APK和生產APK,在串連的真機或模擬器上安裝兩個APK,並運行測試。Android Studio然後在“Run”視窗中顯示測試執行結果。
注意:在運行或調試測試載入器時,Android Studio不會為即時運行注入所需的額外方法,並關閉該特性。
使用Firebase測試實驗室運行測試
略….
(請查看原文)
額外的範例程式碼
要下載到應用程式範例,請參閱Android ActivityInstrumentation Sample。
Android測試(五):Instrumented 單元測試