转载

如何对修改密码接口进行压测

做接口测试中,对于一般性的单业务接口测试很多工具可供选择,但是对于一些相关业务相关性的关联接口测试就比较麻烦,使用工具比如jmeter、postman、soapui等等就比较麻烦。我比较偏重脚本化执行测试用例,所以选择了groovy作为主要语言来进行接口测试,但是脚本依赖的库还是基于之前所在的java为主的测试框架,有兴趣的可以翻翻以前的文章。项目的架构思路是以模块为基础把接口分类,然后对于接口的请求单独进行实现。通过一个user作为一个用户,携带各种属性,如:uname,pwd,token,userinfobean等信息。来作为各个模块类之间的信息传递。

回到修改密码接口,简单说一下我们接口的逻辑,先登录,获取token作为用户身份的唯一校验值,修改密码接口参数,newpwd,oldpwd,token。成功之后会返回新的token,作为用户继续其他操作的校验值。

分享一下自己的测试脚本代码:

class T8 extends OkayBase {
    public static void main(String[] args) {
        int thread =changeStringToInt(args[0])
        int times =changeStringToInt(args[1])
        List<ThreadBase> threads = new ArrayList<>()
        for (int i = 0; i < thread; i++) {
            OkayBase base = getBase(i)
            UserCenter userCenter = new UserCenter(base)
            userCenter.modifyPwd()
            ThreadBase threadBase = new ThreadBase() {
                @Override
                protected void before() {

                }

                @Override
                protected void doing() throws Exception {
                    userCenter.modifyPwd()
                }

                @Override
                protected void after() {

                }
            }
            threadBase.setTimes(times)
            threads.add(threadBase)
        }
        new Concurrent(threads).start()
        allOver()
    }
}

main方法留了两个参数表示线程数和单线程请求数,下面是usercenter的主要代码。

public class UserCenter extends OkayBase {

    private static Logger logger = LoggerFactory.getLogger(UserCenter.class);

    public UserCenter(OkayBase okayBase) {
        super(okayBase);
    }


    public JSONObject modifyPwd() {
        String url = UserApi.MODIFY_PWD;
        JSONObject params = getParams();
        params.put("newpwd", getPassword(this.getUname()));
        params.put("oldpwd", getPassword(this.getPwd()));
        JSONObject response = getPostResponse(url, params);
        output(response);
        if (isRight(response)) {
            String string = response.getJSONObject("data").getString("token");
            this.setToken(string);
        }
        return response;
    }
}

下面是okaybase的主要代码:

public class OkayBase extends SourceCode implements IBase {

    private static Logger logger = LoggerFactory.getLogger(OkayBase.class);

    int uid;

    String token;

    String uname;

    String pwd;
    
    public OkayBase(String uname, String pwd) {
        this.uname = uname;
        this.pwd = pwd;
        login();
    }
    
    public String getPassword() {
        String s = uname.substring(uname.length() - 6);
        return getPassword(s);
    }

    public String getPassword(String pwd) {
        return RSAUtils.getPassword(pwd);
    }
       
    public JSONObject getParams() {
        JSONObject json = getJson("uid=" + uid, "token=" + token);
        json.put("imei", "isFake");
        json.put("serial", "W170500652");
        json.put("ua", "f_an_4..0");
        return json;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public int getUid() {
        return uid;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getUname() {
        return uname;
    }

    @Override
    public boolean isRight(JSONObject jsonObject) {
        int code = TEST_ERROR_CODE;
        try {
            code = jsonObject.getJSONObject("meta").getInt("ecode");
            JSONObject data = jsonObject.getJSONObject("data");
            return code == 0 && !data.isEmpty();
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 测试结束,资源释放
     */
    public static void allOver() {
        FanLibrary.testOver();
    }
}

欢迎有兴趣的童鞋一起交流

原文  https://segmentfault.com/a/1190000019802415
正文到此结束
Loading...