1216 接口测试框架实战{JAVA}

课堂PPT

PPT地址

接口测试用例

部门管理接口测试用例.xlsx (9.7 KB)

POM

 <properties>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <java.version>11</java.version>
        <junit.jupiter.version>5.9.1</junit.jupiter.version>
        <maven.compiler.version>3.8.1</maven.compiler.version>
        <maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
        <hamcrest.version>2.2</hamcrest.version>
        <slf4j.version>2.0.5</slf4j.version>
        <logback.version>1.4.5</logback.version>
        <allure.version>2.20.1</allure.version>
        <aspectj.version>1.9.9.1</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>
        <rest-assured.version>5.3.0</rest-assured.version>
        <jackson.version>2.14.1</jackson.version>
        <json-path.version>2.7.0</json-path.version>
        <assertj.version>3.23.1</assertj.version>
        <!-- faker -->
        <javafaker.version>1.0.2</javafaker.version>
        <!-- json-path -->
        <json-path.version>2.7.0</json-path.version>
    </properties>
    <dependencyManagement>
        <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>
        <!-- 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>
        <!-- junit4 -->
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- slf4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- log -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <!-- allure -->
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-junit5</artifactId>
            <version>${allure.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
            <scope>runtime</scope>
        </dependency>
        <!-- rest-assured -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>${rest-assured.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>json-schema-validator</artifactId>
            <version>${rest-assured.version}</version>
        </dependency>
        <!-- fasterxml解析json文件 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
            <scope>compile</scope>
        </dependency>
        <!--  随机测试数据生成库  -->
        <dependency>
            <groupId>com.apifan.common</groupId>
            <artifactId>common-random</artifactId>
            <version>1.0.18</version>
        </dependency>
        <!-- json path 解析json文件 -->
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>${json-path.version}</version>
        </dependency>
        <!-- mustache 解析mustache文件 -->
        <dependency>
            <groupId>com.github.spullara.mustache.java</groupId>
            <artifactId>compiler</artifactId>
            <version>0.9.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.github.biezhi</groupId>
            <artifactId>TinyPinyin</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <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>
                    <includes>
                        <include>**/*Test</include>
                    </includes>
                </configuration>
                <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>
            <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>

随机生成数


import com.apifan.common.random.source.OtherSource;
import com.apifan.common.random.source.PersonInfoSource;
import com.apifan.common.random.util.PinyinUtils;


public class FakerUtil {
    //部门名字
    public static String getDepartName(){
        //随机生成1个公司部门名称
        String department = OtherSource.getInstance().randomCompanyDepartment();
        return department;
    }
    public static String getName(){
        ///生成1个随机中文人名(性别随机)
        String k = PersonInfoSource.getInstance().randomChineseName();
        return k;
    }
    public static String getPinYin(String pinyin){
        return PinyinUtils.toPinyin(pinyin, true);
    }

    public static String getPhone(){
        //生成1个随机中国大陆手机号
        String m = PersonInfoSource.getInstance().randomChineseMobile();
        //返回手机号
        return m;
    }

    public static int getNum(int start,int end) {
        return (int)(Math.random()*(end-start+1)+start);
    }
    public static String getChinese(int num) {
        //生成4个随机汉字
        String j = OtherSource.getInstance().randomChinese(num);
        return j;
    }
}

json Schema

json viewer

http://jsonviewer.stack.hu/

gpath验证

https://onecompiler.com/groovy/3xxb42jsb

import groovy.json.JsonSlurper

def jsonSlurper = new JsonSlurper()
def one = jsonSlurper.parseText('{"errcode":0,"errmsg":"ok","department":[{"id":1,"name":"产研部","parentid":0,"order":100000000,"department_leader":[]},{"id":2,"name":"质量控制部","parentid":1,"order":100000000,"department_leader":[]},{"id":3,"name":"产品部","parentid":1,"order":99999000,"department_leader":[]},{"id":4,"name":"后端研发部","parentid":1,"order":99998000,"department_leader":[]},{"id":5,"name":"前端部门","parentid":1,"order":99997000,"department_leader":[]},{"id":6,"name":"测试一组1","parentid":2,"order":100000000,"department_leader":[]},{"id":7,"name":"测试二组2","parentid":2,"order":99999000,"department_leader":[]},{"id":8,"name":"刘越彬1648352111146","parentid":1,"order":475,"department_leader":[]},{"id":9,"name":"孙雨泽1648352112153","parentid":1,"order":771,"department_leader":[]},{"id":10,"name":"徐煜城1648352112988","parentid":1,"order":321,"department_leader":[]},{"id":11,"name":"戴鹭洋1648352113646","parentid":1,"order":901,"department_leader":[]},{"id":12,"name":"熊明1648352114249","parentid":1,"order":547,"department_leader":[]},{"id":13,"name":"田建辉1648352114899","parentid":1,"order":75,"department_leader":[]},{"id":14,"name":"梁健雄1648352115509","parentid":1,"order":521,"department_leader":[]}]}')


println(one)


def id = one.department.findAll{ it.order < 1000}.id

println(id)

http://groovy-lang.org/processing-xml.html#_gpath