1、測試需求描述java
對服務後臺一系列的http接口功能測試。web
輸入:根據接口描述構造不一樣的參數輸入值(Json格式)apache
輸出:字符串(傳入的方式+傳入的字符串)json
http://localhost:8090/lctest/TestServer微信
2、程序設計app
一、Client程序設計post
讀取Excel配置的測試用例數據測試
發送參數Json格式給Server,接收Server的返回數據網站
進行數據比對校驗,返回測試結果,將數據插入到Excel中ui
二、Server端程序設計
接受Client的參數,進行解析
發送返回數據給client
3、實現方法
一、選用Java腳原本驅動測試
二、採用Excel表格管理測試數據,包括用例的管理、測試數據錄入、測試結果顯示等等,這個須要封裝一個Excel的類。
三、調用http接口採用java自帶的的API
四、測試須要的將參數轉化成字符串
五、經過預期結果和實際結果的對比,將實際結果和對比結果寫入Excel用例中,這裏封裝了一個類
六、首次執行測試採用人工檢查輸出的是否正確,一旦正確寫入Excel的指望結果中,若是發現錯誤手工修正爲預期文件。
4、Excel表格設計
5、代碼結構
6、實現代碼
一、ExcelUtil.java
package client; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class ExcelUtil { //讀取Excel中數據 public static List<Param> read() throws Exception{ HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow row = s.createRow(0); HSSFCell cell = row.createCell((int)0,0); //------------從xls讀出數據 wb = new HSSFWorkbook(new FileInputStream("D:\\learn\\test.xls")); s = wb.getSheetAt(0); //得到EXCEL行數 int rowNums=s.getLastRowNum(); //得到Excell列數 //int columnNum=r.getPhysicalNumberOfCells(); List<Param> params=new ArrayList<Param>(); for(int i=1;i<=rowNums;i++){ HSSFRow r = s.getRow(i); cell=r.getCell(0); Param param= new Param(); param.setNo(r.getCell(0).getStringCellValue()); param.setName(r.getCell(1).getStringCellValue()); param.setAge(r.getCell(2).getStringCellValue()); param.setSex(r.getCell(3).getStringCellValue()); param.setExpResu(r.getCell(4).getStringCellValue()); // System.out.println(cell.getRichStringCellValue()); params.add(param); } return params; } /** * 寫入Excel,在任意座標處寫入數據。 * String value:你要輸入的內容 * int x :行座標,Excel從 0 算起 * int y :列座標,Excel從 0 算起 */ public static void writeCell(String filePath,int x,int y,String value) { try { // 建立Excel的工做書冊 Workbook,對應到一個excel文檔 HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath)); HSSFSheet sheet=wb.getSheetAt(0); HSSFRow row=sheet.getRow(x); HSSFCell cell=row.getCell((short) y); cell.setCellValue(value); FileOutputStream os; os = new FileOutputStream(filePath); wb.write(os); os.close(); } catch (Exception e) { e.printStackTrace(); } } }
二、JsonsUtil.java
package client; import java.text.ParseException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /** * 使用json-lib構造和解析Json數據 */ public class JsonsUtil { /**將Bean轉換成Map * 將Map轉換Json數據 */public static String BuildJson(Param param) throws JSONException { Map<String, String> map1 = new HashMap<String, String>(); map1.put("no", param.getNo()); map1.put("name", param.getName()); map1.put("age", param.getAge()); map1.put("sex", param.getSex()); // map1.put("expResu", param.getExpResu()); // JSON格式數據解析對象 JSONObject jo = new JSONObject(); // 將Map轉換爲JSONArray數據 JSONArray ja = new JSONArray(); ja.put(map1); // System.out.println("JSONArray對象數據格式:"+ja.toString()); jo.put("map", ja); // System.out.println("最終構造的JSON數據格式:"+jo.toString()); return jo.toString(); } /** * 解析Json數據 * */public static JSONArray ParseJson(String jsonString) throws JSONException, ParseException { JSONObject jo = new JSONObject(jsonString); JSONArray ja = jo.getJSONArray("map"); // System.out.println(ja.getJSONObject(0).getString("name")); return ja; } }
三、Param.java
package client; public class Param { String no;//編號 String name;//姓名 String age;//年齡 String sex;//性別 String expResu;//指望結果 String actResu;//實際結果 String pass;//是否經過 String desc;//描述 public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getExpResu() { return expResu; } public void setExpResu(String expResu) { this.expResu = expResu; } public String getActResu() { return actResu; } public void setActResu(String actResu) { this.actResu = actResu; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } }
四、CompareRes.java
package client; public class CompareRes { int actResuNo=5;//測試結果在第五例 int passNo=6;//對比結果在第六列 public void compare(String filePath,Param param,String actResu){ ExcelUtil.writeCell(filePath,Integer.parseInt(param.getNo()),actResuNo,actResu); if(param.getExpResu().trim().equals(actResu)){ ExcelUtil.writeCell(filePath,Integer.parseInt(param.getNo()),passNo,"Y"); }else{ ExcelUtil.writeCell(filePath,Integer.parseInt(param.getNo()),passNo,"N"); } } }
五、TestClient .java
package client; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.List; public class TestClient { public static void main(String[]agrs){ TestClient a=new TestClient(); try { a.client(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void client() throws Exception{ List<Param> params = ExcelUtil.read(); for(Param pa:params){ try { // 接報文的地址 String filePath="D:\\learn\\test.xls"; String param= new JsonsUtil().BuildJson(pa); URL serverUrl= new URL("http://localhost:8090/lctest/TestServer"); URLConnection uct= serverUrl.openConnection(); HttpURLConnection hutc=(HttpURLConnection)uct; // 設置報文參數 hutc.setRequestMethod("POST"); // 設置是否向httpUrlConnection輸出,由於這個是post請求,參數要放在 http正文內,所以須要設爲true, 默認狀況下是false; hutc.setDoOutput(true); // 設置是否從httpUrlConnection讀入,默認狀況下是true hutc.setDoInput(true); // hutc.setAllowUserInteraction(true); // 開啓流,寫入數據data OutputStream out=hutc.getOutputStream(); out.write(param.getBytes("UTF-8")); out.flush(); out.close(); // 獲取返回的數據 StringBuffer buffer=new StringBuffer(); BufferedReader reader = null; InputStream ins=hutc.getInputStream(); reader = new BufferedReader(new InputStreamReader(ins,"UTF-8")); String sg=reader.readLine(); if (sg!= null){ buffer.append(sg); } System.out.println("接收返回值:" + buffer); new CompareRes().compare(filePath, pa, buffer.toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
六、TestServer
public class TestServer extends HttpServlet { private static final long serialVersionUID = 1L; private static JSONArray ja; /** * @see HttpServlet#HttpServlet() */ public TestServer() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub try { this.excute(request, response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub try { this.excute(request, response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void excute(HttpServletRequest request,HttpServletResponse response) throws Exception{ request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/xml"); String method=request.getMethod(); String url=request.getRequestURI(); String param; // 獲取收到的報文 BufferedReader reader = request.getReader(); String line = ""; line = reader.readLine(); ja=new JsonsUtil().ParseJson(line); StringBuffer resultBuffer=new StringBuffer(); resultBuffer.append("訪問方式"+method+"訪問成功"); resultBuffer.append("接收到的數據:"+line); PrintWriter out =response.getWriter(); out.println(resultBuffer.toString()); out.flush(); out.close(); } }
七、web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>TestServer</servlet-name> <servlet-class>com.servlet.TestServer</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServer</servlet-name> <url-pattern>/lctest/TestServer</url-pattern> </servlet-mapping> </web-app>
歡迎你們關注微信公衆號與QQ羣進行交流
我的網站地址:http://www.hongmenliyu.com