How unified interface testing functionality, automation and performance test cases

Server test, most of the content are deployed around the interface. For interface testing, is nothing more than a function, automation, performance testing based, occasionally think of a problem, if you can write a use case, re-use feature, automated performance of the three test scenario, certainly it can save a lot of time.

The general idea before with interfaces and performance frameworks, by total test project to interface functions packaged good, stay out of the test parameters. Functional testing on human flesh and then write a method call to check, if the automation interface to come up with a response, and then verify that all kinds of data, the performance of the direct use of performance framework which can be called directly.

It took some time to do a Demo, to others.

This method is a simple request for an interface, which is a method wherein the main functional test execution code, is a document in the form of use cases, there will not write:


public class Headgear extends NajmBase {


    public Headgear(NajmBase najmBase) {
        this.loginKey = najmBase.loginKey;
        this.args = najmBase.args;
        this.user_id = najmBase.user_id;
    }

    private static NajmBase base = NajmBase.getBase(0);

    public static Headgear drive = new Headgear(base);


    /**
     * 当前正在使用的头套
     */

    public int usingHeadgearId;

    //  public JSONObject headgearInfo = new JSONObject();
    public Map<Integer, Long> headgearInfo = new HashMap<>();

    public static void main(String[] args) {
//      NajmBase.getUserBalance(base.user_id);
//      int type = 1, id = 36, packageId = 60, num = 1, price = 1;
//      NajmBase base1 = new NajmBase(V580User.getUserName(0));
//      Headgear headgear = new Headgear();
//      headgear.switchHeadgear(34);
//      output(headgear.getHeadgearInfo());
//      output(headgear.getUsingHeadgearId());
//      output(base1.loginResponse);
//      drive.getAllHeadgear();
//      new MallBase(base).buy(type, id, packageId, num, price);
//      drive.getUserHeadgearInfo();
//      NajmBase.getUserBalance(base.user_id);
//      drive.getUserHeadgearInfo();
//      drive.getOnsaleHeadgear();
        int times = 0;
        while (true) {
            times++;
            int type = 1, id = getRandomInt(2) == 1 ? 34 : 36, packageId = id == 34 ? 56 : 60, num = 1, price = 1;
            long deadtime1 = drive.getHeadgearInfo().get(id);
            Verify verify = new Verify(new MallBase(base).buy(type, id, packageId, num, price));
            drive.getUserHeadgearInfo();
            long deadtime2 = drive.getHeadgearInfo().get(id);
            if (deadtime2 - deadtime1 != DAY) break;
        }
        output("一共进行了:" + times);
//      output(drive.getHeadgearInfo());
//      output(drive.usingHeadgear);
//      output(drive.loginKey);
//      output(drive.args);
//      output(base.loginResponse.getJSONObject(DATAINFO).getJSONObject("headGear").getInt("id"));
        testOver();
    }

    /**
     * 获取所有头套信息,包括下架的
     *
     * @return
     */
    public JSONObject getAllHeadgear() {
        String url = HOST + HeadgearApiPath.GET_ALL_HEADGEAR;
        HttpGet httpGet = getHttpGet(url);
        JSONObject response = getHttpResponseEntityByJson(httpGet);
        output(response);
        return response;
    }

    /**
     * 用户切换头套接口
     *
     * @param hid
     * @return
     */
    public JSONObject switchHeadgear(int hid) {
        String url = HOST + HeadgearApiPath.SWITCH_HEADGEAR + hid + changeJsonToArguments(args);
        HttpPost httpPost = getHttpPost(url);
        JSONObject response = getHttpResponseEntityByJson(httpPost);
//      output(response);
        return response;
    }

    /**
     * 获取用户头套信息
     *
     * @return
     */
    public JSONObject getUserHeadgearInfo() {
        sleep(1);
        String url = HOST + HeadgearApiPath.GET_USER_HEADGEAR;
        JSONObject response = getHttpResponseEntityByJson(getHttpGet(url, args));
        output(response);
        if (isRightResponse(response)) {
            headgearInfo.clear();
            JSONArray jsonArray = response.getJSONArray(DATAINFO);
            jsonArray.forEach(json -> {
                    JSONObject jsonObject = JSONObject.fromObject(json.toString()) ;
                    String name = jsonObject.getString("name") ;
                    long deadTime = jsonObject.getLong("deadlineTime") ;
                    int headgearId = jsonObject.getInt("goodId") ;
                    int use = jsonObject.getInt("isUse") ;
            if (use == 1) usingHeadgearId = headgearId;
            headgearInfo.put(headgearId, deadTime);
            output(name, headgearId, getTimeByTimestamp(deadTime));
        } );
        }
        return response;
    }

    /**
     * 获取在售的头套的列表
     *
     * @return
     */
    public JSONObject getOnsaleHeadgear() {
        String url = HOST + HeadgearApiPath.GET_ONSALE_HEADGEAR;
        JSONObject response = getHttpResponseEntityByJson(getHttpGet(url, args));
        output(response);
        return response;
    }

    public int getUsingHeadgearId() {
        getUserHeadgearInfo();
        return usingHeadgearId;
    }

    public Map<Integer, Long> getHeadgearInfo() {
        getUserHeadgearInfo();
        return headgearInfo;
    }

}

The following is based on automated test this feature, main method which is the process of debugging use case, the method of execution cases in the previous article wrote, using the reflection to record an information and execute test cases and save the test results, the output test reports, abnormal warning and so on:

/**
 * 用户0-10
 */
public class HeadgearCase extends SourceCode {
    static HeadgearCase headgearCase = new HeadgearCase();
    static NajmBase base = new NajmBase(V580User.getUserName(0));
    static Headgear drive = new Headgear(base);

    public static void main(String[] args) {
//      headgearCase.testDemo001();
//      headgearCase.testDemo002();
//      headgearCase.testDemo003();
//      headgearCase.testDemo004();
        headgearCase.testDemo005();
//      headgearCase.testDemo006();
        ApiLibrary.testOver();
    }

    /**
     * 获取所有头套信息用例
     */
    public void testDemo001() {
        String label = "获取所有头套信息用例" + TAB + Thread.currentThread().getStackTrace()[1];
        Verify verify = new Verify(drive.getAllHeadgear());
        JSONObject result = new JSONObject();
        result.put("状态码为0", verify.isRight());
        result.put("包含数组", verify.isArray("heads"));
        result.put("包含已下架的头套", verify.isContains("自动化专用3"));
        result.put("包含正在出售的头套", verify.isContains("自动化专用1"));
        MySqlTest.saveTestResult(label, result);
    }

    /**
     * 获取在售的头套用例
     */
    public void testDemo002() {
        String label = "获取在售的头套用例" + TAB + Thread.currentThread().getStackTrace()[1];
        Verify verify = new Verify(drive.getOnsaleHeadgear());
        JSONObject result = new JSONObject();
        result.put("状态码为0", verify.isRight());
        result.put("包含数组", verify.isArray("dataInfo"));
        result.put("不包含已下架的头套", !verify.isContains("自动化专用3"));
        result.put("包含正在出售的头套", verify.isContains("自动化专用1"));
        result.put("包含描述信息", verify.isContains("测试10天"));
        MySqlTest.saveTestResult(label, result);
    }

    /**
     * 获取用户头套信息用例
     */
    public void testDemo003() {
        String label = "获取用户头套信息用例" + TAB + Thread.currentThread().getStackTrace()[1];
        Verify verify = new Verify(drive.getUserHeadgearInfo());
        JSONObject result = new JSONObject();
        result.put("状态码为0", verify.isRight());
        result.put("用户头套正常", verify.isContains("自动化专用1"));
        result.put("用户佩戴正常", verify.isContains("\"isUse\":1"));
        result.put("头套套餐正常", verify.isContains("测试1天"));
        MySqlTest.saveTestResult(label, result);
    }

    /**
     * 余额不足购买用例
     */
    public void testDemo004() {
        String label = "余额不足购买用例" + TAB + Thread.currentThread().getStackTrace()[1];
        NajmBase base = new NajmBase(V580User.getUserName(1));
        int type = 1, id = 36, packageId = 60, num = 1, price = 1;
        Verify verify = new Verify(new MallBase(base).buy(type, id, packageId, num, price));
        JSONObject result = new JSONObject();
        result.put("状态码为35", 35 == verify.getCode());
        MySqlTest.saveTestResult(label, result);
    }

    /**
     * 正常购买用例
     */
    public void testDemo005() {
        String label = "正常购买用例" + TAB + Thread.currentThread().getStackTrace()[1];
        int type = 1, id = getRandomInt(2) == 1 ? 34 : 36, packageId = id == 34 ? 56 : 60, num = 1, price = 1;
        int balance = NajmBase.getUserBalance(drive.user_id);
        long deadtime1 = drive.getHeadgearInfo().get(id);
        Verify verify = new Verify(new MallBase(base).buy(type, id, packageId, num, price));
        drive.getUserHeadgearInfo();
        long deadtime2 = drive.getHeadgearInfo().get(id);
        int balance1 = NajmBase.getUserBalance(drive.user_id);
        JSONObject result = new JSONObject();
        result.put("状态码为0", verify.isRight());
        result.put("截止日期正确", (deadtime2 + EMPTY).equals(verify.getValue("deadlineTime")));
        result.put("头套日期增加正常", deadtime2 - deadtime1 == DAY);
        result.put("用户余额减少正常", balance - balance1 == 1);
        MySqlTest.saveTestResult(label, result);
    }

    /**
     * 用户切换头套用例
     */
    public void testDemo006() {
        String label = "用户切换头套用例" + TAB + Thread.currentThread().getStackTrace()[1];
        drive.getAllHeadgear();
        int id1 = drive.getUsingHeadgearId() == 34 ? 36 : 34;
        Verify verify = new Verify(drive.switchHeadgear(id1));
        int id2 = drive.getUsingHeadgearId();
        JSONObject result = new JSONObject();
        result.put("状态码为0", verify.isRight());
        result.put("头套切换成功", id1 == id2);
        MySqlTest.saveTestResult(label, result);
    }
}

Here is a test case against which the performance of a function (test two types, one is a single HTTP request, I spent the HttpRequestBase target acquisition request and then to re-send concurrent requests, one is multi-interface or non-HTTP requests, such as dubbo, mysql, redis, message queues, and so, a direct call method of pressure-test):

Simple HTTP requests:

class CancelReason extends OkayBase{

    public static void main(String[] args) {
        def argsUtil = new ArgsUtil(args)
        def thread = argsUtil.getIntOrdefault(0, 2)
        def times = argsUtil.getIntOrdefault(1, 5)

        def base = getBase()
        
        Headgear drive = new Headgear(base);
        drive.getAllHeadgear()
        def request = FanLibrary.getLastRequest()

        def timesthread = new RequestThreadTimes(request, times)
        new Concurrent(timesthread, thread,"获取所有头套,内容流转二期压测接口").start()

        allOver()
    }
}

Performance testing framework written before Demo refer to non-simple HTTP request: performance testing framework Second Edition . Such methods are relatively simple to write, use a high range, but the need to address security issues multithreading data and objects according to different business needs.

The entire project on git, functional testing locally, automation projects and project performance on the server, using the Groovy script, you can also debug locally. Automation projects take a fixed time interval or timing of automatic operation, the performance of the project receive input commands groovy filename.groovyto run.


  • Solemnly declare : The article first appeared in public No. "FunTester", prohibit third parties (except Tencent cloud) reproduce, publish.

Technology Featured articles

Non-technical Selected Articles

Guess you like

Origin www.cnblogs.com/FunTester/p/12151542.html