基于Java+RestAssured+TestNG+Maven+Allure的接口自动化框架


一般自动化框架需要提供三个层次的能力,核心层(提供用例执行管理能力,比如 testng )、工具层(提供触发关键操作的能力,比如接口测试中的 rest-assured,UI 自动化中的 appium、selenium)、适配层(【可选】提供用例编写的模板或规范,减少重复提高编写效率。比如提供 excel 或者 yml 写用例的方式)。

常见组合:

  • 核心层:testng + reportNG/Allure report/extentreports,或直接 junit(比较少见)
  • 工具层:rest-assured(调用接口 + 断言一条龙提供)/基于 httpclient 自行封装/基于其他 http 框架自行封装
  • 适配层:单接口常见用 excel/yml(一般结合 testng 本身提供的 dataProvider 使用),多接口多见直接写代码(代码里区分单接口调用层、多接口业务操作层,用例是通过调用这两层的方法来完成操作)

当前的接口自动化框架基本上分两种:

  • 采用excel文件编写接口用例。这是一种数据驱动思想,通过poi来读取excel文件的接口测试用例。
  • 将测试用例编写在TestNG测试类中

以下介绍的是第一种框架,通过excel来管理测试用例。

一、框架介绍

1. 分析

在框架组装之前,需要把一些前提条件给想好。这里主要的前提条件是围绕测试用例执行和管理。所以,需要用到测试框架,包括单元测试框架和其他功能测试框架。例如常见的测试框架,有TestNG, Junit, Cucumber, Robot 等, 这里我们选择TestNG,主要的原因,就是利用extent report得到更美观的测试报告。

2. 框架选型

常见的框架有,页面对象模型,关键字驱动,数据驱动,行为驱动,以及前面两个或者多个的混合框架。一般来说,现在采用混合框架居多。

这里说的混合框架,其实呢Rest Assured具备一些行为驱动的特点。然后如果你API测试用例写在excel文件中,java中借助POI这个jar包获取excel数据,然后使用TestNG的data provider这个注解来实现数据驱动。这个数据驱动,在这个框架,我暂时不引入进来。所有的API测试用例都写在TestNG管理的测试类中。

二、框架设计

1.涉及到的技术

  • Rest Assured
  • TestNG
  • Maven
  • Jenkins(CI)
  • Git&Github

2. 框架步骤

基于上面的框架选型,我大概分以下步骤来逐渐介绍框架的实现步骤。

  • 创建一个Maven 工程
  • 更新 pom.xml文件
  • 创建文件夹结构
  • 日志模块(log4j)
  • 继承思想,测试基类BaseTest.java的设计
  • 扩充实际接口测试用例和调试测试
  • 其他工具类补充
  • 报告获取
  • Jenkins持续集成搭建

三、框架前期准备

1.创建Maven工程

根据自己使用的代码工具,创建一个maven项目

2.添加相关依赖包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.cyj</groupId>
    <artifactId>RestAssuredAPI</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <aspectj.version>1.9.7</aspectj.version>
    </properties>

    <dependencies>
        <!-- REST Assured  -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>5.2.0</version>
        </dependency>

        <!-- json-path  -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>json-path</artifactId>
            <version>5.2.0</version>
        </dependency>

        <!-- xml-path  -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>xml-path</artifactId>
            <version>5.2.0</version>
            <scope>test</scope>
        </dependency>

        <!-- json-schema-validator  -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>json-schema-validator</artifactId>
            <version>5.2.0</version>
            <scope>test</scope>
        </dependency>

        <!--Spring Mock Mvc-->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>spring-mock-mvc</artifactId>
            <version>5.2.0</version>
            <scope>test</scope>
        </dependency>

        <!--testng-->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!--hamcrest-->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>java-hamcrest</artifactId>
            <version>2.0.0.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.70</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-json</artifactId>
            <version>5.7.21</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>

        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>3.1.5</version>
        </dependency>

        <!-- allure报表依赖 -->
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-testng</artifactId>
            <version>2.19.0</version>
            <scope>test</scope>
        </dependency>

        <!--easyPoi依赖,有两个坐标-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-java-commons</artifactId>
            <version>2.19.0</version>
            <scope>compile</scope>
        </dependency>


    </dependencies>

    <!-- allure报告 -->
    <build>
        <plugins>
            <plugin>
                <!-- maven-surefire-plugin 配合testng执行测试用例的maven插件 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <!-- 测试失败后,是否忽略并继续测试 -->
                    <testFailureIgnore>true</testFailureIgnore>
                    <suiteXmlFiles>
                        <!-- testng配置文件名称 -->
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <!--设置参数命令行 -->
<!--                    <argLine>-->
<!--                        &lt;!&ndash; UTF-8编码 &ndash;&gt;-->
<!--                        -Dfile.encoding=UTF-8-->
<!--                        &lt;!&ndash; 配置拦截器 &ndash;&gt;-->
<!--                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"-->
<!--                    </argLine>-->
                    <systemProperties>
                        <property>
                            <!-- 配置 allure 结果存储路径 -->
                            <name>allure.results.directory</name>
                            <value>${project.build.directory}\allure-results</value>
                        </property>
                    </systemProperties>
                </configuration>
                <dependencies>
                    <!-- aspectjweaver maven坐标 -->
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

<!--  extend reporter报告      -->
<!--    <build>-->
<!--        <plugins>-->
<!--            <plugin>-->
<!--                <groupId>org.apache.maven.plugins</groupId>-->
<!--                <artifactId>maven-compiler-plugin</artifactId>-->
<!--                <configuration>-->
<!--                    <source>8</source>-->
<!--                    <target>8</target>-->
<!--                </configuration>-->
<!--            </plugin>-->

<!--            <plugin>-->
<!--                <groupId>org.apache.maven.plugins</groupId>-->
<!--                <artifactId>maven-surefire-plugin</artifactId>-->
<!--                <version>2.18.1</version>-->
<!--                <configuration>-->
<!--                    <suiteXmlFiles>-->
<!--                        &lt;!&ndash; TestNG suite XML files &ndash;&gt;-->
<!--                        <suiteXmlFile>testng.xml</suiteXmlFile>-->
<!--                    </suiteXmlFiles>-->
<!--                </configuration>-->
<!--            </plugin>-->

<!--        </plugins>-->
<!--    </build>-->




</project>

3.工程目录结构

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值