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

前言

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

一、接口测试范围

在这里插入图片描述
说明一下:本人之前在某绿厂工作过,这边项目的诉求是整机的接口进行测试。
安卓端的接口测试是介于白盒和灰盒测试的范畴,因为三方接口,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是运行在同一进程。
在这里插入图片描述

总结

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

猜你喜欢

转载自blog.csdn.net/weixin_42233460/article/details/108484343