工具接口测试web

接口自动化测试(httpclient)

2016-02-15  本文已影响3121人  demil

使用的自动化框架

java + httpclient + log4j + json + mybatis + excel + testng
excel
httpclient ( get / post)
/** * get 形式请求API *
 * @param uri url 和 对应参数
 * @param @key key 
 * @return 返回相应信息 
 * @throws java.net.URISyntaxException 
 * @throws java.io.IOException 
 */
  public static JSON getAPI(URI uri) throws URISyntaxException, IOException {    
          CloseableHttpClient httpclient = HttpClients.createDefault();    
          try {
               httpget = new HttpGet(uri);  
               log.info("执行API请求" + httpget.getRequestLine());
               ResponseHandler<String> responseHandler = new ResponseHandler<String>() { 
            public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {                
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    log.error("请求错误,状态码为:" + response.getStatusLine().getStatusCode());
                    throw new ClientProtocolException("意外的状态返回: " + status);
                } 
           } 
       };
        String responseBody = httpclient.execute(httpget, responseHandler);
        JSONObject dataObject = JSONObject.fromObject(responseBody.trim());
        return dataObject;
    } finally {
        httpclient.close(); 
   }
}
/** * Post 形式请求API *
 * @param uri url 和 对应参数 
 * @param @key key 
 * @return 返回相应信息
 * @throws java.net.URISyntaxException
 * @throws java.io.IOException 
 */
 public static JSON postAPI(URI uri) throws URISyntaxException, IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {
        httpPost = new HttpPost(uri); 
        log.info("执行API接口请求" + httpPost.getRequestLine());
        ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
            public String handleResponse(
                    final HttpResponse response) throws ClientProtocolException, IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    throw new ClientProtocolException("意外的状态返回: " + status);
                }
            } 
       };
        String responseBody = httpclient.execute(httpPost, responseHandler);
        JSONObject dataObject = JSONObject.fromObject(responseBody);
        return dataObject;
    } finally {
        httpclient.close();
    }
}
log4g

###set output encoding###
log4j.appender.logfile.encoding=UTF-8

### set log levels ###
log4j.rootLogger = INFO , ERROR  ,stdout , logfile

### 控制台输出 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %p %t [%c]%M(line:%L)%m%n

### save error log ###
log4j.appender.ERROR = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.File = logs/error.log
log4j.appender.ERROR.Append = true
log4j.appender.ERROR.Threshold = ERROR
log4j.appender.ERROR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern =%-d{yyyy-MM-dd HH:mm:ss} %p %t [%c]%M(line:%L)%m%n

### write in log ###
log4j.appender.logfile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=logs/debug.log
log4j.appender.logfile.Append = true
log4j.appender.logfile.Threshold = DEBUG
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %p %t [%c]%M(line:%L)%m%n

mybatis:跟mysql交互
  public class Accounts_infoTest {
    static Accounts_infoDao accountsDao = new Accounts_InfoDaoImpl();    

/*** 通过account_id查询出 accounts_info表数据
     * @param account_id 用户id
     * @return account_info 表数据 
    */
    public static Accounts_Info byAccount_id(String account_id){
        Accounts_info a = accountsDao.account_id(account_id);
        return a;
    }
 Accounts_info accounts_info=Accounts_infoTest.byAccount_id(Integer.valueOf(account_id));
int coin_num_sql = accounts_info.getCoin();// 拿到数据库中 coin_num 字段
返回json解析
// 拿到response_status对应的值success
 String response_status = jsonObject .getString("response_status");
// 拿到post_enable的值1
    String post_enable = json.getJSONObject("info").getJSONObject("data").getJSONObject("priv_option").getString("post_enable");
// 拿到list下多个组数据
 JSONArray jsonArray = json.getJSONObject("info").getJSONObject("data").getJSONArray("list"); 

// 获取组数据中第一个数据(数组从0开始)并强制转成json格式
 JSONObject jsonObject = (JSONObject) jsonArray.get(0);

String id = jsonObject.getString("id");// 获取强制转成json后的id
创建一个自动化API脚本的步骤
可能遇到的问题
上一篇下一篇

猜你喜欢

热点阅读