testng用来组织测试用例,控制用例运行顺序以及断言,httpclient用来模拟客户端发送请求,获取响应结果。
通过testng里面的dataProvider数据提供者来实现参数化。
代码如下:
package com.mg.api.auto_v1; import java.util.HashMap; import java.util.Map; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class LoginCase { @Test(dataProvider = "loginData") // 测试登录 public void testLogin(String username, String password) { String url = "http://49.235.92.12:9000/api/v1/login"; Map<String, String> params = new HashMap<String, String>(); params.put("username", username); params.put("password", password); System.out.println(HttpUtil.sendPost(url, params)); } @DataProvider(name = "loginData") public Object[][] datas() { Object[][] datas = { { "test", "123456" }, // 正常登录 { "test", "" }, // 不输入密码登录 { "", "123456" }, // 不输入用户名登录 { "", "" }, // 输入为空登录 { "t", "123456" }, // 错误用户名登录 { "test", "1" }, // 错误密码登录 { "test$", "1" }, // 用户名包含特殊字符 { "test$", "123456#" }, // 密码包含特殊字符 }; return datas; } }
HttpUtil.sendPost(url, params)调用了工具类里面的sendPost发送请求,并返回结果:
package com.mg.api.auto_v1; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; /** * 接口调用工具类,post和get * * @author admin * */ public class HttpUtil { /** * post请求处理接口 * * @param url:请求url地址 * @param params:请求参数map形式添加 * @return */ public static String sendPost(String url, Map<String, String> params) { // 接口请求方式:post HttpPost post = new HttpPost(url); List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>(); // 取出map中所有的参数名 Set<String> keys = params.keySet(); // 通过循环将参数保存到list集合里面 for (String key : keys) { String value = params.get(key); parameters.add(new BasicNameValuePair(key, value)); } // 定义全局变量,便于返回结果 String result = ""; try { post.setEntity(new UrlEncodedFormEntity(parameters, "utf-8")); // 发送请求,获取响应 HttpClient client = HttpClients.createDefault(); // 创建默认客户端,相当于按钮 HttpResponse response = client.execute(post); // 状态码 int code = response.getStatusLine().getStatusCode(); System.out.println("状态码:" + code); // 响应报文 result = EntityUtils.toString(response.getEntity()); } catch (Exception e) { e.printStackTrace(); } return "响应报文:" + result; } /** * get请求处理接口 * * @param url * @param params * @return */ public static String sendGet(String url, Map<String, String> params) { Set<String> keys = params.keySet(); // 标识位 int mark = 1; for (String key : keys) { if (mark == 1) { url += ("?" + key + "=" + params.get(key)); } else { url += ("&" + key + "=" + params.get(key)); } mark++; } System.out.println("url=" + url); // 请求方式:get HttpGet get = new HttpGet(url); // 发送请求,获取响应 HttpClient httpClient = HttpClients.createDefault(); HttpResponse response; String result = ""; try { response = httpClient.execute(get); // 获取响应状态码 int code = response.getStatusLine().getStatusCode(); System.out.println("响应状态码:" + code); // 获取响应内容 result = EntityUtils.toString(response.getEntity()); } catch (Exception e) { e.printStackTrace(); } return "响应报文:" + result; } }
代码运行结果:通过数据提供者运行了8条用例