提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
上篇文章介绍完接口测试的一些基本概念之后,相信大家知道我们需要测什么,目的以及意义。
所以现在需要知道如何搭建测试环境,以及为什么?
同样的这里需要大家先了解一下安卓的测试框架Instrumentation,他是用来干什么的?
一、Instrumentation是什么?
可以先初步的看一下这篇文章
Android instrumentation原理
两个重点
1.这个框架提供了将测试apk和被测apk运行在同一进程,这样就实现了资源共享
2.可以调用本该由系统调用的方法,如四大组件的生命周期。
有了以上两点,可以便捷我们进行接口测试,而且接口测试页依赖以上两点。
为什么提及这个呢,因为我们最终运行测试用例的运行器AndroidJUnitRunner继承自Instrumentation
二、AndroidJUnitRunner是什么?
大家可以先了解一下这边文章,简单说来,就是可以访问安卓资源的,且支持Junit框架的,这样我们就可以用它来进行运行在真机上的单元测试。
androidJUnitRunner以及JUnit的学习以及使用
三、接口测试环境搭建
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是运行在同一进程。