接口测试 基于 Excel 数据驱动的单接口自动化接口框架 2-Swagger 提数及白名单设计实现

CC · 2016年08月22日 · 最后由 kenny 回复于 2016年08月28日 · 1929 次阅读

目前所在公司服务端采用 swagger 注解方式自动生成 API 接口文档,这给自动生成 excel 模版产生相当大的便利


一,生成 excel 模版基础数据

1.(源数据) 从 swagger UI 提数,写入到本地 txt 文件

/**
     * 请求swagger JSON文本
     * @throws Exception
     */
    public static String GetSwaggerJSON() throws Exception{
        String url = "https://xxxx/v2/api-docs";
        return httpClientMethod.getSSLMethod(url,new HashMap<String, Object>(),new HashMap<String, Object>()).toString();

    }

    /**
     * 请求swagger JSON文本,并把特殊字符进行处理后再写入本地文件
     * @throws Exception
     */
    public static void WriteSwaggerJSON() throws Exception{

        String swaggerJSON = GetSwaggerJSON();
        String[] fbsArr = {"$"};
        for(String key:fbsArr){
            if(swaggerJSON.contains(key)){
                swaggerJSON = swaggerJSON.replace(key,"");
            }
        }

        System.out.println(swaggerJSON);
        fileMethod.writeTxtFile(swaggerJSON,sysConfig.SwaggerFilePath);
    }

2.对源数据 (JSON) 进行解析,生成 excel 初始数据 (不要用 fastjson 解析,会出现大坑)

/**
     * 读取本地SwaggerJSON文件
     * @throws Exception
     */
    public static JSONObject ReadSwaggerJSON() throws Exception{
        String sJSON = fileMethod.readTxtFile(sysConfig.SwaggerFilePath);
        JSONObject swaggerJSON = JSONObject.fromObject(sJSON);
        return swaggerJSON;
    }

    /**
     *处理SwaggerJSON数据,生成excel所需数据
     *主要使用Map+JSON进行处理
     * 2016-06-22 修改优化
     */

    public static  JSONObject basicModelMAPJSON() {
    //由于处理过程代码逻辑比较复杂,只能抱歉的写了下思路,代码量挺大,总的一个思路: 递归方式处理
    //获取swagger JSON源数据
    //迭代器方式循环遍历数据
    //数据存储格式例:{"URI":{"method":"HTTP方法","summary":"描述","tags":"归类","parameters":{参数信息}}}
    //把数据写入到本地txt文件中
}


3.白名单
出现白名单的原因,整个产品的接口超过 600 多个接口,我们测试阶段可能只关心某一个模块的接口,因此出现了白名单
设计实现:
a. 在本地 txt 中输入需要测试的 URI 地址
b. 读取本地 txt 文件专为 list

    /**
     * 读TXT文件内容
     * @param filepath 文件路径
     * @return
     * @throws IOException
     */
    public static List<String> readTxtFileAsList(String filepath) throws IOException{
        String result = "";
        File directory  = new File(".");
        String path = null;
        path = directory .getCanonicalPath();
        path = path + filepath;
        File file = new File(path);
        if (!file.exists()){
            return null;
        }
        List<String> resultList = new ArrayList<String>();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法,一次读一行
//              result = result + "\n" +s;
                resultList.add(s);
            }
            br.close();

        }catch(Exception e){
            e.printStackTrace();
        }
//      result = result.replaceAll("\r|\n", "");
        return resultList;
    }

c.遍历 list,并比对生成到本地的 excel 初始数据,生成白名单数据,写入到本地

/**
 * 读取本地本地自定义接口文本
 * 生成excel JSON数据源,并写入本地
 * @throws Exception
 */
public static JSONObject CustomModelJSON() throws Exception{

    List<String> customAPIList = new ArrayList<String>();
    JSONObject basicExcelJSONData = new JSONObject();
    JSONObject customModelJSONData = new JSONObject();

    //读取本地文本数据
    customAPIList = fileMethod.readTxtFileAsList(sysConfig.CustomAPIFilePath);

    //获取SwaggerExcelJSON基础数据源
    basicExcelJSONData = ReadBasicSwaggerJSONData();

    for(String keyAPI : customAPIList){
        if(basicExcelJSONData.containsKey(keyAPI)){
            customModelJSONData.put(keyAPI,basicExcelJSONData.getJSONObject(keyAPI));
        }
    }
    fileMethod.writeTxtFile(customModelJSONData.toString(),sysConfig.CustomExcelBasicJSONData);//excel数据源写入到本地文件中
    return customModelJSONData;
}

至此,基于 swagger UI 的数据提数及白名单提数完成
之后我这边继续更新
1.excel 数据模版生成
2.参数强度测试策略
3.HTTP Header 处理
4...

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 3 条回复 时间 点赞

直接用 jsonObject 解析的 swagger json? 可以用现成的 swagger parser, 好用。

CC #2 · 2016年08月25日 Author

#1 楼 @qi_ling2005 嗯,我直接用 jsonObject + map 裸解,你说的方法我去试试看😁

用的 excel 转数组

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