码迷,mamicode.com
首页 > Web开发 > 详细

Http接口测试—参数化

时间:2015-08-31 17:44:14      阅读:596      评论:0      收藏:0      [点我收藏+]

标签:

1、传入参数对象化

public class Param {
	
	String no;//编号
	String name;//姓名
	String age;//年龄
	String sex;//性别
	String expResu;//期望结果
	String actResu;//实际结果
	String pass;//是否通过
	String desc;//描述
	.......
}

2、使用Excel储存数据,表格如下

技术分享

3、用poi读取和写入数据,这里我们封装一个ExcelUtil类,代码如下

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 newContent:你要输入的内容
	  * int beginRow :行坐标,Excel从 0 算起
	  * int beginCol   :列坐标,Excel从 0 算起
	  */
	 public void writeCell( String newContent, int beginRow, int beginCell){ 		 	
	 
	 }
}

4、客户端代码如下:

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 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);
	
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	
	}
}

5、服务端代码如下:

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 JsonsTest().ParseJson(line);		
		StringBuffer resultBuffer=new StringBuffer();
		resultBuffer.append("访问方式"+method+"访问成功");
		resultBuffer.append("接收到的数据:"+line+"name:     "+ja.getJSONObject(0).getString("name"));
		PrintWriter out =response.getWriter();
		out.println(resultBuffer.toString());
		out.flush();
		out.close();
		
	}
}

6、客户端调取服务端,运行结果如下:

技术分享

下一步我们就要做接口的自动化测试设计

Http接口测试—参数化

标签:

原文地址:http://my.oschina.net/hellotest/blog/499527

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!