接口框架的基础功能已经讲完了,接下来会分享一些接口测试实践。今天分享了接口一般的业务验证,注册用户和用户登录。借助开源社区提供的接口来做演示。写了一些测试脚本,针对独立性较高的接口测试项目如何快速验证。没有使用所谓数据驱动,也没有采用断言(使用预警代替),总的来讲,更像 BDD 一些。中间也用到了多接口相互验证的功能。

相信一万行代码的理论!

视频专题:

接口测试中业务验证


gitee 地址:https://gitee.com/fanapi/tester

代码

package com.fun;


import com.alibaba.fastjson.JSONObject;
import com.fun.config.HttpClientConstant;
import com.fun.frame.httpclient.FanLibrary;
import com.fun.utils.RString;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpPost;

public class AR extends FanLibrary {

    public static String APIKEY;

    public static void main(String[] args) {
        testDemo001();
        testDemo002();

        testOver();
    }

    public static void testDemo001() {
        JSONObject funTester = registerUser("FunTester", RString.getString(10), RString.getString(10));
        int code = funTester.getIntValue("code");
        if (code != 400) fail();
    }

    public static void testDemo002() {
        String name = "Fun" + getMark();
        String pwd = RString.getString(10);
        String remark = RString.getString(10);
        JSONObject funTester = registerUser(name, pwd, remark);
        int code = funTester.getIntValue("code");
        if (code != 200) fail();
        JSONObject result = funTester.getJSONObject("result");
        String name1 = result.getString("name");
        if (!name.equals(name1)) fail();
        String remark1 = result.getString("remarks");
        if (!remark.equals(remark1)) fail();
        userLogin(name, pwd);
    }


    public static JSONObject registerUser(String name, String passwd, String remark) {
        if (StringUtils.isEmpty(APIKEY)) developerLogin();
        String url = "https://api.apiopen.top/registerUser";
        JSONObject param = new JSONObject();
        param.put("apikey", APIKEY);
        param.put("name", name);
        param.put("passwd", passwd);
        param.put("nikeName", "FunTester");
        param.put("headerImg", "http://pic.automancloud.com/sick-jvm-heap-1.png");
        param.put("phone", "13100001111");
        param.put("email", "Fhaohaizi@163.com");
        param.put("vipGrade", "3");
        param.put("autograph", "abc");
        param.put("remarks", remark);
        HttpPost httpPost = getHttpPost(url, param);
        JSONObject response = getHttpResponse(httpPost);
        output(response);
        return response;
    }

    public static void register() {
        String url = "https://api.apiopen.top/developerRegister";
        JSONObject param = new JSONObject();
        param.put("name", "FunTester");
        param.put("passwd", "FunTester");
        param.put("email", "Fhaohaizi@163.com");
        HttpPost httpPost = getHttpPost(url, param);
        JSONObject response = getHttpResponse(httpPost);
        output(response);
    }

    public static void userLogin(String name, String pwd) {
        if (StringUtils.isEmpty(APIKEY)) developerLogin();
        String url = "https://api.apiopen.top/loginUser";
        JSONObject param = new JSONObject();
        param.put("name", name);
        param.put("passwd", pwd);
        param.put("apikey", APIKEY);
        HttpPost httpPost = getHttpPost(url, param);
        JSONObject response = getHttpResponse(httpPost);
        output(response);
        if (!(response.getIntValue("code") == 200)) fail();
    }

    public static void developerLogin() {
        String url = "https://api.apiopen.top/developerLogin";
        JSONObject params = new JSONObject();
        params.put("name", "funtester");
        params.put("passwd", "funtester");
        HttpPost httpPost = getHttpPost(url, params);
        httpPost.addHeader(HttpClientConstant.X_Requested_KWith);
        httpPost.addHeader(getHeader("name", "FunTester"));
        JSONObject response = getHttpResponse(httpPost);
        output(response);
        if (response.getIntValue("code") == 200) {
            APIKEY = response.getJSONObject("result").getString("apikey");
        } else {
            fail();
        }
    }


}


无代码文章精选


↙↙↙阅读原文可查看相关链接,并与作者交流