1.准备数据:数据格式为{json},放在json文件内,例如这样:
2.工具类:读取.json文件,保存在Map<testCaseName,JSONObject>内
public static Map<String, JSONObject> jsonToSearchFilter(String fileName) throws IOException { Map<String, JSONObject> result = new HashMap<>(); StringBuffer buffer = new StringBuffer(); String dataPath = System.getProperty("user.dir") + "/src/main/resources/data/" + fileName; BufferedReader br = new BufferedReader(new FileReader(new File(dataPath))); String line = ""; while ((line = br.readLine()) != null) { buffer.append(line); } String jsonData = buffer.toString(); try { JSONArray array = JSONObject.parseArray(jsonData);//将json格式的字符串转换成JSONObject 对象 for (int i = 0; i < array.size(); i++) { JSONObject jsonObj = (JSONObject) array.get(i); //将array中的数据进行逐条转换 String testCaseName = jsonObj.getString("testCaseName"); result.put(testCaseName, jsonObj); } } catch (Exception e) { e.printStackTrace(); } return result; }
3.
3.1在init类里初始化数据:
public Map<String, JSONObject> testData = new HashMap<>(); testData = DataUtils.jsonToSearchFilter("fileName.json");
3.2 测试用例类继承init类,在具体测试方法里找到具体的数据:content
data = testData.get("key"); // HashMap.get(key),返回该key(testCaseName)对应的jsonObject content = data.getJSONObject("data");
标签:Map,String,框架,json,testCaseName,自动化,new,array,读取 From: https://www.cnblogs.com/jinziguang/p/18164705