Building Instrumented Unit Tests
組件單元測試指的是: 在真實的設備或者模擬器上運行單元測試,這樣可以利用android的framework APIs和supporting APIs,比如說: Android Testing Support Library.當你的測試需要訪問組件信息(比如說目標app的Context)或者他們需要實現真實的Android framework 組件的信息(eg: Parcelable或者SharePreference)時,你應該使用組件的單元測試.
使用組件單元測試也會幫助減少你寫和維護mock 代碼的時間.你仍然可以使用mocking的framwork,如果你任性的話,來模擬任何的依賴關係.
Set Up Your Testing Environment
在android studio的工程中,你需要將組件測試的源文件放置到module-name/src/androidTest/java/中.在你創建工程師,你這個文件夾就已經存在,並且已經包含了了一個組件測試的案例.
在你開始之前,你需要下載Android Testing Support Library Setup,這些APIs中允許你快速的構建和運行組件測試代碼.這個The Testing Support Library包含了JUnit的runner(AndroidJUnitRunner)和用於功能測試(ESpresso和UI Automator)的APIs
同樣,你還需要配置用於由Testing Support Library提供的工程測試的運行和規則的Android 測試的依賴.爲了測試工作的簡化,你同樣可以依賴Hamcrest庫,同歸這個庫的APIs,可以用來創建更多靈活的斷言.
在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的compile級別的庫和androidTestCompile級別依賴的espresso-core的庫,那麼你在build是,可能會因爲依賴衝突,導致build失敗.解決辦法: 更新你的espresso-core的依賴,如下操作:
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' })
要使用Junit4的測試類,確保在app的module級別的build.gradle文件中,指定使用的是AndroidJunitRunenr作爲默認的測試組件.
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
Create an Instrumented Unit Test Class
你的組件測試類應該要寫成Junit 4 測試的類.
爲了創建一個Junit 4測試的類,在你的測試類的註解上添加@RunWith(AndroidJUnit4.class). 並且你需要指定由Android Testing Support Library提供的AndroidJUnitRunner作爲默認的test runner.
接下來的這個案例,告訴你如何測試在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));
}
}
Create a test suite
爲了組織組件單元測試的運行,你可以將幾個測試類放到同一個測試的組件中並且將他們一起運行.這些這是組件可以是交叉的, 你的測試組件可以組織其他的測試組件,並且把他一起運行測試.
一個測試組件包含了一個測試的package中,像主應用的package一樣.慣例上,會將測試組件的包以.suffix來結尾(個eg.com.example.android.testing.mysample.suite)
創建自己的單元測試時,需要引入JUnit的RunWith和Suit類.在你的測試組件中,需要添加的是* @RunWith(Suite.class)和 @Suite.SuitClasses() 註解.在@Suite.SuiteClasses() *註解的括號裏面,列出單獨的測試類或者測試組件
下面的案例說明了如何實現一個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 {}
Run Instrumented Unit Tests
爲了運行組件測試,需要以下的幾個步驟:
- Gradle已經做了Sync Project**
有以下的集中方式來運行你的測試
- 單個測試,打開Project的窗口,右鍵你的測試,並且點擊Run
- 測試類中的所有方法,在文件中右鍵這個類或者方法,並且點擊Run
- 測試一個目錄中的所有測試,右鍵這個目錄,並且選擇Run tests
Android的gradle插件編譯的組件測試代碼位於默認的目錄src/androidTest/java/,構建一個測試的apk和production apk, 同時安裝這個兩個app在連接的設備或者模擬器上,運行測試.Android Studio會在Run window中展示這個組件測試的結果.
爲了運行或者debug組件測試,android studio並沒有給Instant Run額外需要的方法注入,這個特性是關閉的.
Run your tests with Firebase Test Lab
###
Firebase Test Lab 大家自己可以查一下哈