FunTester 基于 java 的直线型接口测试框架初探

FunTester · 2020年03月30日 · 727 次阅读

在使用 java 语言作为接口测试的过程中,发现 java 语言的简洁性的确不如脚本语言,如 python,很多功能 python 一行代码几个方法就能搞定,java 需要几行,而且有时候并不利于理解。最近接触到了一个词 “直线型” 代码。看了之后有所感觉,重新写了一个直线型代码风格的接口请求框架。

源码如下:


package com.fun.frame.httpclient

import com.fun.base.bean.RequestInfo
import com.fun.config.RequestType
import net.sf.json.JSONObject
import org.apache.commons.lang3.StringUtils
import org.apache.http.Header
import org.apache.http.client.methods.HttpRequestBase
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
 * 重写FanLibrary,使用面对对象思想
 */
public class FunRequest extends FanLibrary {

    static Logger logger = LoggerFactory.getLogger(FunRequest.class)

    /**
     * 请求类型,truegetfalsepost
     */
    RequestType requestType

    /**
     * 请求对象
     */
    HttpRequestBase request

    /**
     * host地址
     */
    String host

    /**
     * 接口地址
     */
    String apiName

    /**
     * 请求地址,如果为空则由hostapiname拼接
     */
    String uri

    /**
     * header集合
     */
    List<Header> headers = new ArrayList<>()

    /**
     * get参数
     */
    JSONObject args = new JSONObject()

    /**
     * post参数
     */
    JSONObject params = new JSONObject()

    /**
     * json参数
     */
    JSONObject json = new JSONObject()

    /**
     * 构造方法
     *
     * @param requestType
     */
    private FunRequest(RequestType requestType) {
        this.requestType = requestType
    }

    /**
     * 获取get对象
     *
     * @return
     */
    public static FunRequest isGet() {
        new FunRequest(RequestType.GET)
    }

    /**
     * 获取post对象
     *
     * @return
     */
    public static FunRequest isPost() {
        new FunRequest(RequestType.POST)
    }

    /**
     * 设置host
     *
     * @param host
     * @return
     */
    public FunRequest setHost(String host) {
        this.host = host
        this
    }

    /**
     * 设置接口地址
     *
     * @param apiName
     * @return
     */
    public FunRequest setApiName(String apiName) {
        this.apiName = apiName
        this
    }

    /**
     * 设置uri
     *
     * @param uri
     * @return
     */
    public FunRequest setUri(String uri) {
        this.uri = uri
        this
    }

    /**
     * 添加get参数
     *
     * @param key
     * @param value
     * @return
     */
    public FunRequest addArgs(Object key, Object value) {
        args.put(key, value)
        this
    }

    /**
     * 添加post参数
     *
     * @param key
     * @param value
     * @return
     */
    public FunRequest addParam(Object key, Object value) {
        params.put(key, value)
        this
    }

    /**
     * 添加json参数
     *
     * @param key
     * @param value
     * @return
     */
    public FunRequest addJson(Object key, Object value) {
        json.put(key, value)
        this
    }

    /**
     * 添加header
     *
     * @param key
     * @param value
     * @return
     */
    public FunRequest addHeader(Object key, Object value) {
        headers << getHeader(key.toString(), value.toString())
        this
    }

    /**
     * 添加header
     *
     * @param header
     * @return
     */
    public FunRequest addHeader(Header header) {
        headers.add(header)
        this
    }

    /**
     * 批量添加header
     *
     * @param header
     * @return
     */
    public FunRequest addHeader(List<Header> header) {
        header.each { h -> headers << h }
        this
    }

    /**
     * 增加headercookies
     *
     * @param cookies
     * @return
     */
    public FunRequest addCookies(JSONObject cookies) {
        headers << getCookies(cookies)
        this
    }

    /**
     * 获取请求响应,兼容相关参数方法,不包括file
     *
     * @return
     */
    public JSONObject getResponse() {
        if (StringUtils.isEmpty(uri))
            uri = host + apiName
        switch (requestType) {
            case RequestType.GET:
                request = FanLibrary.getHttpGet(uri, args)
                break
            case RequestType.POST:
                request = !params.isEmpty() ? FanLibrary.getHttpPost(uri + changeJsonToArguments(args), params) : !json.isEmpty() ? getHttpPost(uri + changeJsonToArguments(args), json.toString()) : getHttpPost(uri + changeJsonToArguments(args))
                break
        }
        headers.each { x -> request.addHeader(x) }
        return getHttpResponse(request)
    }


    /**
     * 获取请求对象
     *
     * @return
     */
    public HttpRequestBase getRequest() {
        logger.debug("请求信息:{}",new RequestInfo(this.request).toString())
        this.request
    }

    @Override
    public String toString() {
        JSONObject.fromObject(this).toString()
    }
}

使用方法如下:

public static void main(String[] args) {
    JSONObject response = FunRequest.isGet()
            .setHost("www.funtester.cn")
            .setApiName("/test")
            .addArgs("uname", "FunTester")
            .addArgs("passoword", "FunTester")
            .addArgs("type", "FunTester")
            .addHeader("token", "FunTester")
            .addCookies(getJson("login=false"))
            .getResponse();
    output(response);

    FanLibrary.testOver();
}

技术类文章精选

  1. java 一行代码打印心形
  2. Linux 性能监控软件 netdata 中文汉化版
  3. 接口测试代码覆盖率(jacoco)方案分享
  4. 性能测试框架
  5. 如何在 Linux 命令行界面愉快进行性能测试
  6. 图解 HTTP 脑图
  7. 如何测试概率型业务接口
  8. httpclient 处理多用户同时在线
  9. 将 swagger 文档自动变成测试代码
  10. 五行代码构建静态博客
  11. httpclient 如何处理 302 重定向
  12. 基于 java 的直线型接口测试框架初探

非技术文章精选

  1. 为什么选择软件测试作为职业道路?
  2. 成为杰出 Java 开发人员的 10 个步骤
  3. 写给所有人的编程思维
  4. 自动化测试的障碍
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册