Android測試(四):Instrumented 單元測試


原文:https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html

Instrumented 單元測試是在真機和模擬器上運行的測試,它可以利用Android框架API和支持的API(如Android測試支持庫)。如果你的測試需要訪問工具信息(例如目標應用程序的Context),或者需要真正實現Android框架組件(如ParcelableSharedPreferences對象),則應該創建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)
@SmallTest
public 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的測試套件,該測試套件將CalculatorInstrumentationTestCalculatorAddParameterizedTest測試類分組並運行在一起。

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM