Instrumented 單元測試運(yùn)行在物理設(shè)備或者模擬器上。他能使用 Android api 和一些 support api,例如 Android Testing Support Library。如果需要獲取 instrumentation 信息(例如需要 app 的 context)或者需要 Android 框架里的一些組件(比如 Parcelable 或 SharedPreferences 對象),可以使用Instrumented 單測
使用Instrumented單元測試也有助于減少編寫和維護(hù)mock 代碼的工作量。如果你選擇mock 的話,可以使用一個(gè)模仿的框架來mock 任何依賴關(guān)系。
設(shè)置編譯環(huán)境
在 Android stuido 中 instrumented 測試代碼在目錄module-name/src/androidTest/java/中,當(dāng)你創(chuàng)建一個(gè)新工程時(shí),這個(gè)目錄就已經(jīng)有了,并包含了一個(gè)測試?yán)?/p>
在開始之前需要下載 e Android Testing Support Library ,它提供了快速測試的 api,包含了JUnit 4 和一些 UI 測試 (Espresso and UI Automator)
在 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'
}
注:如果同時(shí)加入了 support-annotations library 、 an androidTestCompile dependency和 the espresso-core ,會有依賴沖突,為了解決這個(gè)
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' })
使用JUnit 4測試類,確保工程指定AndroidJUnitRunner作為默認(rèn)的測試工具
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
構(gòu)建一個(gè) Instrumented 單測
在測試類前加上 @RunWith(AndroidJUnit4.class) 注解。
下面的示例演示如何編寫一個(gè)Instrumented單元測試,用來測試loghistory類(實(shí)現(xiàn)了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));
}
}
創(chuàng)建一個(gè)測試套件(test suite)
要組織執(zhí)行instrumented單測,你可以在一個(gè)測試套件類中組一個(gè)測試類的集合,并一起運(yùn)行這些測試。測試套件可以嵌套,您的測試套件可以組其他測試套件,并一起運(yùn)行所有的組件測試類。
一個(gè)測試套件包含在一個(gè)測試包中,類似于主要應(yīng)用程序包。按照慣例,測試套件包的名稱通常以.suite 后綴(例如,com.example.android.testing.mysample.suite)。
單測的一個(gè)測試套件需要導(dǎo)入 JUnit 和 suite 類,在測試套件中加入@RunWith(Suite.class) and the @Suite.SuitClasses() 注解。在@Suite.SuitClasses() 注解中列出單獨(dú)的測試類或測試套件作為參數(shù)
以下的例子創(chuàng)建了一個(gè)名叫 UnitTestSuite的測試套件。他包含了CalculatorInstrumentationTest and CalculatorAddParameterizedTest 兩個(gè)測試類
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 {}
運(yùn)行
略
測試系列博客目錄
- Android 測試 (一)--測試總覽 http://xuyushi.github.io/2016/11/05/Android%20%E6%B5%8B%E8%AF%95%20%EF%BC%88%E4%B8%80%EF%BC%89--%E6%B5%8B%E8%AF%95%E6%80%BB%E8%A7%88/
- Android 測試 (二)--Instrumented Unit Tests http://xuyushi.github.io/2016/11/05/Android%20%E6%B5%8B%E8%AF%95%20%EF%BC%88%E4%BA%8C%EF%BC%89--Instrumented%20Unit%20Tests/
- Android 測試 (三)--Local Unit Tests http://xuyushi.github.io/2016/11/06/Android%20%E6%B5%8B%E8%AF%95%20%EF%BC%88%E4%B8%89%EF%BC%89--Local%20Unit%20Tests/
- Android 測試 (四)-- 實(shí)戰(zhàn)分析 http://xuyushi.github.io/2016/11/13/Android%20%E6%B5%8B%E8%AF%95%20%EF%BC%88%E5%9B%9B%EF%BC%89--%20%E5%AE%9E%E6%88%98%E5%88%86%E6%9E%90/