软件测试学习笔记丨接口自动化测试框架介绍

93 阅读5分钟

本文转自测试人社区,原文链接:ceshiren.com/t/topic/283…

接口测试场景

title 功能测试与接口测试对比
scale 2
actor 测试工程师 as tester
participant 前端页面 as fronted
participant 后端服务 as backend

group 功能测试
tester -> fronted: 测试工程师在系统页面做点击输入等操作
fronted-> backend: 通过接口发起请求信息
backend -> fronted: 返回响应,前端渲染页面
fronted -> tester: 通过页面查看对比响应内容与预期是否一致
end
group 接口测试
tester -> backend: 通过接口发起请求信息
backend -> tester: 返回响应,对比响应内容与预期是否一致
end

自动化测试场景

title UI自动化测试与接口自动化测试对比
scale 2
actor 测试工程师 as tester
participant 代码 as code
participant 前端页面 as fronted
participant 后端服务 as backend
group UI自动化测试
tester → code: 测试工程师编写【UI自动化测试】脚本
code → fronted: 脚本在页面上模拟点击、输入等操作
fronted-> backend: 通过接口发起请求信息
backend → fronted: 返回响应,前端渲染页面
fronted → code: 通过页面查看对比响应内容与预期是否一致
end
group 接口自动化测试
tester → code: 测试工程师编写【接口自动化】测试脚本
code → backend: 通过接口发起请求信息
backend → code: 返回响应,对比响应内容与预期是否一致
end

接口测试在分层测试中的位置

接口自动化测试与 Web/App 自动化测试对比

接口自动化测试与 Web/App 自动化测试对比

看起来接口自动化测试什么都比 Web/App 自动化测试要好,为什么还要做 Web/App 自动化测试?

  • 接口关注数据无法触达用户体验。

接口自动化测试场景

接口自动化测试在企业中的实践

接口测试的市场需求

接口测试工具类型

为什么推荐 REST-assured

  • 是由 Java 实现的 REST API 测试框架。
  • 支持发起 POST, GET, PUT, DELETE 等请求。
  • 可以用来验证和校对响应信息。

REST-assured 优势

  • 支持多种数据格式:支持 xml/json 的结构化解析。
  • 内置断言库:支持 xpath/jsonpath/gpath 解析方式。
  • 可与多种测试框架集成:支持与 JUnit、TestNG 等测试框架集成。

REST-assured 环境准备

  • 创建 maven 项目。
  • 基于 JDK 11、JUnit5。
  • pom.xml 添加 rest-assured 的依赖。
<!-- rest-assured 相关依赖 -->
<properties>
    <rest-assured.version>5.3.0</rest-assured.version>
</properties>
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>${rest-assured.version}</version>
</dependency>

附录:完整依赖配置

<!-- 其他使用到的依赖配置  -->
<!--   版本配置-->
<properties>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    <java.version>11</java.version>
    <junit.jupiter.version>5.9.2</junit.jupiter.version>
    <maven.compiler.version>3.11.0</maven.compiler.version>
    <maven-surefire-plugin.version>3.0.0</maven-surefire-plugin.version>
    <rest-assured.version>5.3.0</rest-assured.version>
    <json-path.version>2.8.0</json-path.version>
    <!-- allure报告 -->
    <allure.version>2.21.0</allure.version>
    <aspectj.version>1.9.19</aspectj.version>
    <allure.maven.version>2.12.0</allure.maven.version>
    <allure.cmd.download.url>
        https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline
    </allure.cmd.download.url>
</properties>
<dependencyManagement>
    <!--        junit5 版本管理, 找到对应依赖关系的 pom 文件,为了解决依赖冲突问题-->
    <dependencies>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>${junit.jupiter.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>

    <!--        junit 相关依赖下载-->
    <!-- junit5 -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- junit5-suite -->
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- 用做兼容老版本 -->
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- rest-assured -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>${rest-assured.version}</version>
        <scope>compile</scope>
    </dependency>
    <!-- json path 解析json文件 -->
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <version>${json-path.version}</version>
    </dependency>
    <!--        allure报告-->
    <dependency>
        <groupId>io.qameta.allure</groupId>
        <artifactId>allure-junit5</artifactId>
        <version>${allure.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${aspectj.version}</version>
    </dependency>

</dependencies>
<build>
    <plugins>
        <!--            maven 命令行执行插件-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire-plugin.version}</version>
            <configuration>
                <argLine>
                    -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                </argLine>
            </configuration>
            <!--                防止maven与junit5使用依赖冲突的问题-->
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit.jupiter.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                    <version>${junit.jupiter.version}</version>
                </dependency>
            </dependencies>
        </plugin>
        <!--            maven 编译使用插件-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.version}</version>
            <configuration>
                <parameters>true</parameters>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>${maven.compiler.encoding}</encoding>
            </configuration>
        </plugin>

        <plugin>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-maven</artifactId>
            <version>${allure.maven.version}</version>
            <configuration>
                <reportVersion>${allure.version}</reportVersion>
                <allureDownloadUrl>${allure.cmd.download.url}/${allure.version}/allure-commandline-${allure.version}.zip</allureDownloadUrl>
            </configuration>
        </plugin>
    </plugins>
</build>
package com.restassured;

import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems;

public class FirstTest {

    @Test
    public void demo(){
        when().
                get("https://ceshiren.com/").
                then().
                statusCode(200);
    }
}

推荐学习

【霍格沃兹测试开发】7天软件测试快速入门带你从零基础/转行/小白/就业/测试用例设计实战

【霍格沃兹测试开发】最新版!Web 自动化测试从入门到精通/ 电子商务产品实战/Selenium (上集)

【霍格沃兹测试开发】最新版!Web 自动化测试从入门到精通/ 电子商务产品实战/Selenium (下集)

【霍格沃兹测试开发】明星讲师精心打造最新Python 教程软件测试开发从业者必学(上集)

【霍格沃兹测试开发】明星讲师精心打造最新Python 教程软件测试开发从业者必学(下集)

【霍格沃兹测试开发】精品课合集/ 自动化测试/ 性能测试/ 精准测试/ 测试左移/ 测试右移/ 人工智能测试

【霍格沃兹测试开发】腾讯/ 百度/ 阿里/ 字节测试专家技术沙龙分享合集/ 精准化测试/ 流量回放/Diff

【霍格沃兹测试开发】Pytest 用例结构/ 编写规范 / 免费分享

【霍格沃兹测试开发】JMeter 实时性能监控平台/ 数据分析展示系统Grafana/Docker 安装

【霍格沃兹测试开发】接口自动化测试的场景有哪些?为什么要做接口自动化测试?如何一键生成测试报告?

【霍格沃兹测试开发】面试技巧指导/ 测试开发能力评级/1V1 模拟面试实战/ 冲刺年薪百万!

【霍格沃兹测试开发】腾讯软件测试能力评级标准/ 要评级表格的联系我

【霍格沃兹测试开发】Pytest 与Allure2 一键生成测试报告/ 测试用例断言/ 数据驱动/ 参数化

【霍格沃兹测试开发】App 功能测试实战快速入门/adb 常用命令/adb 压力测试

【霍格沃兹测试开发】阿里/ 百度/ 腾讯/ 滴滴/ 字节/ 一线大厂面试真题讲解,卷完拿高薪Offer !

【霍格沃兹测试开发】App自动化测试零基础快速入门/Appium/自动化用例录制/参数配置

【霍格沃兹测试开发】如何用Postman 做接口测试,从入门到实战/ 接口抓包(最新最全教程)

【霍格沃兹测试开发】6 小时轻松上手功能测试/ 软件测试工作流程/ 测试用例设计/Bug 管理