自己怎么设置会员网站,做暖暖免费网站,适合工作室做的项目,极客 wordpress根据接口文档写接口测试用例--添加接口自动化测试项目相关依赖(httpclienttestngpoi-ooxmllog4jmailmysql-connector-java)--写接口测试方法--执行测试 -- 接口测试1.一个接口就是一个函数2.我们要保证一个接口能够在url地址栏里面访问到#xff0c;必须满足一下两…根据接口文档写接口测试用例--添加接口自动化测试项目相关依赖(httpclienttestngpoi-ooxmllog4jmailmysql-connector-java)--写接口测试方法--执行测试 -- 接口测试1.一个接口就是一个函数2.我们要保证一个接口能够在url地址栏里面访问到必须满足一下两个条件一是这个接口首先是部署到服务器上的web容器中而是此接口必须实现了http协议-- 接口文档接口地址请求方式参数变量名类型说明备注-- 接口测试工具soapuijmeterpastmanfiddler都操作一次soapui准备url创建项目根据接口文档确定提交的方式准备传参发送请求获取接口返回数据-- ui层面的功能测试跟接口测试的区别1.ui层面可以通过页面填写数据传数据到后台但是接口测试没有页面我们只能借助接口测试工具模拟页面去提交接口数据。2.UI层面的功能测试会有后台接口返回的数据并且数据会通过js以提示信息的方式显示在页面帮助用户或者测试人员去判断功能是否正常。但是接口测试就没有页面提示信息我们能看到的是接口返回的最原始的数据。接口自动化实现java有专门的第三方框架可以提供了一套基于http协议的接口请求和处理技术。--httpclient-- 环境搭建创建maven项目-eclipse集成testng-集成testng框架添加依赖 dependencies !-- 管理用例提供多种测试场景实现 -- dependency groupIdorg.testng/groupId artifactIdtestng/artifactId version6.8.8/version scopetest/scope /dependency !-- 接口提交(get/post) -- dependency groupIdorg.apache.httpcomponents/groupId artifactIdhttpclient/artifactId version4.5.2/version /dependency !-- 解析excel文件 -- dependency groupIdorg.apache.poi/groupId artifactIdpoi-ooxml/artifactId version3.15/version /dependency !-- 记录日志框架 -- dependency groupIdlog4j/groupId artifactIdlog4j/artifactId version1.2.17/version /dependency !-- 发送邮件框架 -- dependency groupIdjavax.mail/groupId artifactIdmail/artifactId version1.5.0-b01/version /dependency !-- jdbc技术数据库驱动 -- dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version5.1.38/version /dependency /dependencies-- httpPost1,以post方式提交请求到服务器2,参数封装到请求体当中3,数据长度是没有限制的4,支持post方式提交的接口往往是吧数据提交到服务器关键api函数构造函数HttpPost postnew HttpPost(url);创建一个post对象以post方式提交接口请求设置参数到请求体post.setEntity(new UrlEncodedFormEntity(params));通过此方法将接口参数设置到请求中-- httpGet1,以get方式提交请求到服务器2,get参数不是封装在请求体当中的(由没有setEntify方法就可以看出)3,数据长度是有限制的4,支持get提交的接口一般都是从服务器拿下来数据关键api函数构造函数HttpGet httpGetnew HttpGet(interfacesurl);创建一个get对象以get方式提交接口请求注意如果以get提交的接口请求有需要传参蚕食通常是直接拼接在url后面例子public class RegisterInterface { Test public void test1() throws Exception{ //准备url; String urlhttp://8080/lmcanon_web_auto/mvc/member/api/member/register; //接口提交的方式; HttpPost postnew HttpPost(url); //准备传参; ListNameValuePairparamsnew ArrayListNameValuePair(); params.add(new BasicNameValuePair(mobilephone, 13517315121)); params.add(new BasicNameValuePair(pwd, e10adc3949ba59abbe56e057f20f883e)); //将参数设置到post请求中 post.setEntity(new UrlEncodedFormEntity(params)); //发送请求; CloseableHttpClient httpclientHttpClients.createDefault(); CloseableHttpResponse responsehttpclient.execute(post); //获取接口返回数据. String jsonStrEntityUtils.toString(response.getEntity()); System.out.println(jsonStr); }} Test public void get() throws Exception{ //定义接口地址 String interfacesurlhttp://8080/futureload/mvc/api/member/list; //决定接口提交方式 HttpGet httpGetnew HttpGet(interfacesurl); //直接提交接口请求准备客户端 CloseableHttpClient httpClientHttpClients.createDefault(); CloseableHttpResponse responsehttpClient.execute(httpGet); //获取相应数据 String jsonStringEntityUtils.toString(response.getEntity()); System.out.println(jsonString); }} --httpRequeste接口类型表示是从客户端发送服务端请求HttpGet和httpPost都是httpRequest实现类属于子类对象-- httpResponse 接口类型表示是从服务端到客户端的响应从响应对象中获取返回数据getEntity()从响应对象中获取响应状态getStatusLine()从响应对象中获取响应头信息getAllHeaders()从响应对象中设置响应头信息setHeader-- 93框架实现步骤1,testng管理用例2,实现httpUtil类提供公共的方法处理post和get请求3,设计测试用例4,实现数据驱动获取url接口类型传参封装成方法5,接口测试数据回写6,日志集成7,报表集成8,jenkins集成--get和post公共方法public static String getResultStringByPost(String uri,ListNameValuePairparams){ //调用函数StringUtils.isEmpty(uri)对uri是否为空的处理 if(StringUtils.isEmpty(uri)){ return ; } //创建httpPost对象 HttpPost httpPostnew HttpPost(uri); String resultString; //设置参数到httpPost对象中 try { httpPost.setEntity(new UrlEncodedFormEntity(params)); //准备httpclient客户端 CloseableHttpClient httpClient HttpClients.createDefault(); //发送请求 CloseableHttpResponse responsehttpClient.execute(httpPost); //解析数据 resultStringEntityUtils.toString(response.getEntity()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return resultString; } /**get请求通用处理函数 * param uri接口地址 * param params提交参数 * return接口响应数据 */ public static String getResultStringByGet(String uri,ListNameValuePairparams){ //调用函数StringUtils.isEmpty(uri)对uri是否为空的处理 if(StringUtils.isEmpty(uri)){ return ; } if(paramsnull){ return ; } for (int i0;iparams.size();i) { NameValuePair nameValuePairparams.get(i); if(i0){ uri(?nameValuePair.getName()nameValuePair.getValue()); }else{ uri(nameValuePair.getName()nameValuePair.getValue()); } } //创建httpGet对象 HttpGet httpGetnew HttpGet(uri); String resultString; //设置参数到httpPost对象中 try {// httpGet.setEntity(new UrlEncodedFormEntity(params)); //准备httpclient客户端 CloseableHttpClient httpClient HttpClients.createDefault(); //发送请求 CloseableHttpResponse responsehttpClient.execute(httpGet); //解析数据 resultStringEntityUtils.toString(response.getEntity()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return resultString; }//StringUtils判空方法public class StringUtils { public static boolean isEmpty(String content){ boolean isEmpty(contentnull||content!nullcontent.trim().length()0); return isEmpty; }}转载于:https://www.cnblogs.com/AIME/p/8907207.html