移動端-安卓-介面測驗簡介
- 前言
- 一、介面測驗范圍
- 二、介面測驗和單元測驗的區別
- 三、介面測驗環境搭建
- 總結
前言
閱讀本文之前,建議大家需要提前了解一下,什么是白盒測驗,以及白盒測驗的范圍,
一、介面測驗范圍

說明一下:本人之前在某綠廠作業過,這邊專案的訴求是整機的介面進行測驗,
安卓端的介面測驗是介于白盒和灰盒測驗的范疇,因為三方介面,sdk的介面存在原始碼不可見的情況,這種就是灰盒的測驗范圍,原始碼可見的介面采用白盒測驗思維進行測驗,
總的來說,介面測驗屬于單元測驗的其中一個分支,
二、介面測驗和單元測驗的區別
1.單元測驗是和源工程一個專案下,因為原始碼必須可見才能進行單元測驗,而介面如果不可見,比如三方介面,則不需要再同一個工程下
2.單元測驗用例是運行在jvm上,而介面測驗是需要在真機環境,也就是安卓環境下才能運行
因為介面通常都是跨模塊的,jvm無法提供環境,比如context,sim卡,網路等
(如果有別的差異也歡迎大家補充,此處只強調重點的區別)

androidTest目錄下就是介面測驗用例(運行在android環境(真機))
test目錄就是單元測驗用例(運行在java環境(jvm))
三、介面測驗環境搭建
1.配置app build.gradle
根據官方檔案添加依賴
AndroidJUnitRunner配置
https://developer.android.google.cn/training/testing/junit-runner
android {
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// The following argument makes the Android Test Orchestrator run its
// "pm clear" command after each test invocation. This command ensures
// that the app's state is completely cleared between tests.
testInstrumentationRunnerArguments clearPackageData: 'true'
}
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
}
dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestUtil 'androidx.test:orchestrator:1.1.0'
}
2.配置清單檔案
此步驟工具gradle已經集成,可忽略
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
android:compileSdkVersion="28"
android:compileSdkVersionCodename="9"
package="com.xx.xx.test">
<uses-sdk
android:minSdkVersion="23"
android:targetSdkVersion="28" />
<instrumentation
android:label="Tests for com.xx.xx"
android:name="android.support.test.runner.AndroidJUnitRunner"
android:targetPackage="com.xx.xx"
android:handleProfiling="false"
android:functionalTest="false" />
<application
android:debuggable="true">
<uses-library
android:name="android.test.runner" />
</application>
</manifest>
3.介面測驗工程androidTest

我們就可以在androidTest目錄下撰寫運行在真機上的介面測驗用例了
4.運行介面測驗用例
前提是插入手機
可以點擊@Test

也可以通過adb 命令運行
adb shell am instrument -w -r -e debug false -e class '類名的絕對路徑#方法名' 包名/android.support.test.runner.AndroidJUnitRunner
詳情使用可以參考官方檔案:
https://developer.android.google.cn/training/testing/junit-runner
# Install the test orchestrator.
adb install -r path/to/m2repository/androidx/test/orchestrator/1.1.0/orchestrator-1.1.0.apk
# Install test services.
adb install -r path/to/m2repository/androidx/test/services/test-services/1.1.0/test-services-1.1.0.apk
# Replace "com.example.test" with the name of the package containing your tests.
# Add "-e clearPackageData true" to clear your app's data in between runs.
adb shell 'CLASSPATH=$(pm path androidx.test.services) app_process / \
androidx.test.services.shellexecutor.ShellMain am instrument -w -e \
targetInstrumentation com.example.test/androidx.test.runner.AndroidJUnitRunner \
androidx.test.orchestrator/.AndroidTestOrchestrator'
如果您不知道目標插樁測驗,可以運行以下命令來查詢:
adb shell pm list instrumentation
4.androidTest架構
重點說明一下,測驗apk和debug.apk是運行在同一行程,

總結
本文簡單的說明了一下介面測驗的測驗范圍,目的,以及環境搭建,介面測驗如何運行,基本原理以及框架,有任何疑問,可以聯系博主,歡迎大家提問,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/25582.html
標籤:其他
