java移动端接口测试_移动端-安卓-接口测试简介

本文介绍了安卓移动端接口测试的概念,它介于白盒和灰盒测试之间,重点关注三方接口和SDK的测试。文章详细阐述了接口测试与单元测试的区别,并提供了详细的接口测试环境搭建步骤,包括配置build.gradle、清单文件以及运行测试用例的方法。此外,还提到了接口测试工程的androidTest目录结构和运行原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

阅读本文之前,建议大家需要提前了解一下,什么是白盒测试,以及白盒测试的范围。

一、接口测试范围

说明一下:本人之前在某绿厂工作过,这边项目的诉求是整机的接口进行测试。

安卓端的接口测试是介于白盒和灰盒测试的范畴,因为三方接口,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已经集成,可忽略

xmlns:android="http://schemas.android.com/apk/res/android"

android:compileSdkVersion="28"

android:compileSdkVersionCodename="9"

package="com.xx.xx.test">

android:minSdkVersion="23"

android:targetSdkVersion="28" />

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" />

android:debuggable="true">

android:name="android.test.runner" />

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是运行在同一进程。

总结

本文简单的说明了一下接口测试的测试范围,目的,以及环境搭建,接口测试如何运行,基本原理以及框架,有任何疑问,可以联系博主,欢迎大家提问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值