接口测试 我现在的这接口测试代码离所谓的框架还缺少了什么东西?

围城 · 2017年02月28日 · 最后由 ting 回复于 2017年06月22日 · 1194 次阅读

用的 okhttp3 框架,首先封装四种请求方法

import net.sf.json.JSONObject;  
import okhttp3.*;  
import java.io.IOException;  
import java.util.HashMap;  
import java.util.Iterator;  

class TestMethod {  
    private static String baseUrl = "http://192.168.3.149:8080";  
    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");  
    private static OkHttpClient client = new OkHttpClient();  
    //post请求  
    static Object doPost(String url, String json, String cookie) throws IOException {  
        RequestBody body = RequestBody.create(JSON, json);  
        Request request = new Request.Builder()  
                .url(baseUrl + url)  
                .addHeader("Cookie", cookie)  
                .post(body)  
                .build();  
        try (Response response = client.newCall(request).execute()) {  
            return JSONObject.fromObject(response.body().string());  
        }  
    }  
    //get请求  
    static Object doGet(String url, HashMap<String, Object> param, String cookie) throws IOException {  
    //拼接url字符串  
        if (param != null) {  
            Iterator<String> it = param.keySet().iterator();  
            StringBuffer sb = null;  
            while (it.hasNext()) {  
                String key = it.next();  
                Object value = param.get(key);  
                if (sb == null) {  
                    sb = new StringBuffer();  
                    sb.append("?");  
                } else {  
                    sb.append("&");  
                }  
                sb.append(key);  
                sb.append("=");  
                sb.append(value);  
            }  
            url += sb.toString();  
        }  
        Request request = new Request.Builder()  
                .url(baseUrl + url)  
                .addHeader("Cookie", cookie)  
                .get()  
                .build();  
        try (Response response = client.newCall(request).execute()) {  
            return JSONObject.fromObject(response.body().string());  
        }  
    }  
    //put请求  
    static Object doPut(String url, String json) throws IOException {  
        RequestBody body = RequestBody.create(JSON, json);  
        Request request = new Request.Builder()  
                .url(baseUrl + url)  
                .put(body)  
                .build();  
        try (Response response = client.newCall(request).execute()) {  
            return JSONObject.fromObject(response.body().string());  
        }  
    }  
    //delete请求  
    static Object doDelete(String url, String json) throws IOException {  
        RequestBody body = RequestBody.create(JSON, json);  
        Request request = new Request.Builder()  
                .url(baseUrl + url)  
                .delete(body)  
                .build();  
        try (Response response = client.newCall(request).execute()) {  
            return JSONObject.fromObject(response.body().string());  
        }  
    }  
}  

接下来是做 cookie 获取,把 cookie 保存到本地,在需要传递 cookie 的方法里调用

import okhttp3.*;  
import java.io.IOException;  

class Cookie {  
    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");  
    private static OkHttpClient client = new OkHttpClient();  
    static String cookieManage() throws IOException {  
        String json = "{\"username\":\"huangshayang@supeq.com\",\"password\":\"123456\"}";  
        RequestBody body = RequestBody.create(JSON, json);  
        String cookieUrl = "http://192.168.3.149:8080/api/v1/login";  
        Request request = new Request.Builder()  
                .url(cookieUrl)  
                .post(body)  
                .build();  
        try (Response response = client.newCall(request).execute()) {  
            return response.header("Set-Cookie");  
        }  
    }  
}  

实际例子:登录请求和退出请求
1.登录请求不需要 cookie,所以不传

import net.sf.json.JSONObject;  
import java.io.IOException;  

public class LoginTest {  
    private static String loginUrl = "/api/v1/login";  
    //成功登录  
    public static JSONObject loginSuccess() throws IOException {  
        String json = "{\"username\":\"huangshayang@supeq.com\",\"password\":\"123456\"}";  
        return TestMethod.doPost(loginUrl, json, null);  
    }  
}  

2.退出请求需要 cookie

import net.sf.json.JSONObject;  
import java.io.IOException;  

public class LogoutTest {  
    private static String logoutUrl = "/api/v1/logout";  
    //成功退出  
    public static JSONObject logoutSuccess() throws IOException {  
        return TestMethod.doGet(logoutUrl, null, Cookie.cookieManage());  
    }  
}  

最后 junit 里做断言结果判断

import com.company.LoginTest;  
import com.company.LogoutTest;  
import net.sf.json.JSONObject;  
import org.junit.Test;  
import java.io.IOException;  
import static org.junit.Assert.assertEquals;  

public class ApiTest {  
    @Test  
    public void testloginSuccess() throws IOException {  
        JSONObject result = LoginTest.loginSuccess();  
        assertEquals(200, Integer.parseInt(result.getString("status")));  
        assertEquals("Operation is successful.", result.getString("message"));  
    }  
    @Test  
    public void testlogoutSuccess() throws IOException {  
        JSONObject result = LogoutTest.logoutSuccess();  
        assertEquals(200, Integer.parseInt(result.getString("status")));  
        assertEquals("Operation is successful.", result.getString("message"));  
    }  
}  
共收到 10 条回复 时间 点赞

缺了测试数据和测试业务逻辑的分离。写在一起难以维护。

ting 回复

你说的写在一起是哪里写在一起了??

难道要写成 Excel 管理那种??

对啊,我也好奇

可以不用 excel,excel 只是一种工具

cucumber 写比较好

测试框架包括测试服务器的维护 + 持续集成 + 用例管理 + 用例执行 + 报告生成。你这个只是做了用例的自动执行而已。

围城 回复

就是说你的测试数据写在 java 代码里。改数据的时候要改代码。像你只传一个用户名密码的例子里这样用是可以的,但在测试用例数量上去之后,或者你的数据变得复杂起来的时候,这个维护有点麻烦。

举个例子,你现在只测一个用户的登录,如果需要测的用户不止一个,有 10 个用户都想测一下登陆,其中有几个预期结果是登录失败,失败时返回的信息各有不同,你这个框架下怎么实现。

如果框架成熟,测一个用户和一组用户的区别只是定义不同的用户名和密码。测成功登录和各种失败登陆的区别只是预期结果不同。也就是我理想情况下要创建这些用例,只需要输入这些数据,而不需要再写任何其他的代码。

ting 回复

为什么 github 找了好久,没找到类似的好用的框架。请我有资源分享吗?

开鑫 回复

类似于 robot framework、testng、Xunit 之类的成熟框架都提供数据驱动功能,但你得自己用起来。
不会用的话请看用户手册或找教程,再搭配上一些调用接口的库就是你想找的所谓接口测试框架。

举例来说,楼主的这个就是 okhttp3(接口调用的库)+junit(成熟框架)。
你要的例子就是把他的代码重构一下,把数据和业务分离开来,
让测试人员写 case 和改 case 时需要写的代码量减少。

然后再加上各种你实际上需要的功能(比如你要分布式执行测试,比如你要 mock 一些接口,比如你要特定格式的报告或者把报告中的数据存到什么地方去做后续分析),就是比较好的框架了。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册