今天的越寫悅快樂之系列文章爲大家帶來Android應用如何接入單元測試。我們都知道軟件測試在軟件的生命週期中有着舉足輕重的位置,不僅僅是因爲測試是軟件質量的保障,更是因爲隨着軟件產品或者服務的不斷演進,傳統的手動測試已經無法滿足大規模的軟件產品和技術迭代,而軟件產品不可避免地會產生問題,因此引入必要的軟件測試構建流程體系對於構建大型軟件產品或服務是一項必不可少的工作。接下來我爲大家分享一下如何在Android應用中接入單元測試。
開發環境
- Window 10.0.17763
- Java 8.0.191
- Android Studio 3.3.2
Gradle 版本
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
Build 版本
dependencies {
classpath "com.android.tools.build:gradle:3.2.1"
}
測試介紹
測試是應用開發過程中不可缺少的一個環節,它可以在應用上架之前儘可能地保證你的應用的正確性、業務功能和可用性。
測試的級別
- Small Tests - 70% - Unit Test
- Medium Tests - 20% - Integration Tests
- Large Tests - 10% - User Interface Tests
測試的優勢
- 快速響應軟件故障
- 在開發週期中儘早地發現軟件缺陷
- 可以快速地優化、構建安全的應用
- 逐步深入的測試可以減少技術債務
接入
添加依賴(app/build.gralde
)
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
// Core library
androidTestImplementation 'androidx.test:core:1.1.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test:rules:1.1.1'
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.ext:truth:1.1.0'
androidTestImplementation 'com.google.truth:truth:0.44'
// Espresso
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.1'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-idling-resource:3.1.1'
// Hamcrest
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
// UIAutomator
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}
爲了統一管理依賴庫的版本,我們可以在項目的
build.gradle
中定義依賴庫的版本,然後在此處使用變量代替依賴庫的版本號。
也可使用Robolectric來進行單元測試
可使用Mockito搭建模擬數據進行單一測試
創建項目
我們從上圖中可以看到源代碼(java
)目錄下生成3個不同用途的目錄:
-
me.weitao.androidtesting
- 源代碼目錄 -
me.weitao.androidtesting(androidTest)
- Instrumented Test 目錄 -
me.weitao.androidtesting(test)
- Unit Test 目錄
我們本文的單元測試中的單元可以是一個類、一個方法、一個模塊、一個組件、亦或一個視圖,但凡可以測試的對象都可以看做是一個單元測試類。在該測試類下構建測試的初始化環境(setUp
),驗證核心功能,觸發數據響應,斷言(assertions
)數據響應的結果,構建有效即時的單元測試,讓你的應用可以單獨驗證某一個功能或者頁面。
編寫單元測試類(me.weitao.androidtesting.ExampleUnitTest.java
)
package me.weitao.androidtesting;
import org.junit.Test;
import static org.junit.Assert.*;
public class ExampleUnitTest {
@Test
public void testNumberPlus() {
assertEquals(4, 2 + 2);
}
}
編寫工具測試類(me.weitao.androidtesting.ExampleInstrumentedTest.java
)
package me.weitao.androidtesting;
import android.content.Context;
import org.junit.Test;
import org.junit.runner.RunWith;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import me.weitao.androidtesting.test.BuildConfig;
import static org.junit.Assert.assertEquals;
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void testPackageName() {
Context appContext = InstrumentationRegistry.getInstrumentation().getContext();
assertEquals(BuildConfig.APPLICATION_ID, appContext.getPackageName());
}
}
運行測試
- 方法一
以@Test
開頭的測試方法可以直接點擊方法之前的三角箭頭,在編輯器的底部就可以看到運行結果
- 方法二
點擊右側Gradle
窗口,然後查看模塊app
下的任務列表,即可看到測試相關的任務名稱,點擊對應的任務名稱即可運行測試
- 方法三
通過終端運行graldlew task test
命令可以達到相同的效果
參考
個人總結
首先,總結一下今天的文章內容,文章從軟件測試的基礎知識出發,闡述了軟件測試的分類,軟件測試的優勢,最後從實戰出發說明了接入單元測試的流程和步驟,那麼基於測試驅動開發的業務功能或者應用可以更好地識別開發過程中的軟件缺陷,我們可以從黑盒和白盒的角度驗證功能的可用性和可訪問性,讓我們的產品或者服務爲客戶創造更大的價值,讓我們的技術得以提升,服務更多的客戶,成就更加美好的未來。若是我的文章對你有所啓發,那將是我莫大的榮幸。