zoukankan      html  css  js  c++  java
  • 2018-04-22接口自动化测试学习心得(1)

    根据接口文档写接口测试用例-->添加接口自动化测试项目相关依赖(httpclient+testng+poi-ooxml+log4j+mail+mysql-connector-java)-->写接口测试方法-->执行测试

    -- 接口测试
    1.一个接口就是一个函数
    2.我们要保证一个接口能够在url地址栏里面访问到,必须满足一下两个条件
    一是这个接口首先是部署到服务器上的web容器中,
    而是此接口必须实现了http协议
    -- 接口文档
    接口地址
    请求方式
    参数
    变量名
    类型
    说明
    备注

    -- 接口测试工具:
    soapui,jmeter,pastman,fiddler
    都操作一次
    soapui:准备url创建项目,根据接口文档确定提交的方式,准备传参,发送请求,获取接口返回数据

    -- ui层面的功能测试跟接口测试的区别:
    1.ui层面可以通过页面填写数据,传数据到后台,
    但是接口测试没有页面,我们只能借助接口测试工具模拟页面去提交接口数据。
    2.UI层面的功能测试会有后台接口返回的数据,并且数据会通过js以提示信息的方式显示在页面帮助用户或者测试人员去判断功能是否正常。
    但是接口测试就没有页面提示信息,我们能看到的是接口返回的最原始的数据。

    接口自动化实现:
    java有专门的第三方框架可以提供了一套基于http协议的接口请求和处理技术。--httpclient

    -- 环境搭建
    创建maven项目-eclipse集成testng-集成testng框架
    添加依赖
        <dependencies>
            <!-- 管理用例,提供多种测试场景实现 -->
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.8.8</version>
                <scope>test</scope>
            </dependency>
            <!-- 接口提交(get/post) -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.2</version>
            </dependency>
            <!-- 解析excel文件 -->
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.15</version>
            </dependency>
            <!-- 记录日志框架 -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
            <!-- 发送邮件框架 -->
            <dependency>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
                <version>1.5.0-b01</version>
            </dependency>
            <!-- jdbc技术,数据库驱动 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.38</version>
            </dependency>
        </dependencies>

    -- httpPost
    1,以post方式提交请求到服务器
    2,参数封装到请求体当中
    3,数据长度是没有限制的
    4,支持post方式提交的接口往往是吧数据提交到服务器
    关键api函数
    构造函数:
    HttpPost post=new HttpPost(url);
    创建一个post对象,以post方式提交接口请求
    设置参数到请求体
    post.setEntity(new UrlEncodedFormEntity(params));
    通过此方法将接口参数设置到请求中

    -- httpGet
    1,以get方式提交请求到服务器
    2,get参数不是封装在请求体当中的(由没有setEntify方法就可以看出)
    3,数据长度是有限制的
    4,支持get提交的接口一般都是从服务器拿下来数据
    关键api函数
    构造函数:
    HttpGet httpGet=new HttpGet(interfacesurl);
    创建一个get对象,以get方式提交接口请求
    注意:如果以get提交的接口请求有需要传参,蚕食通常是直接拼接在url后面
    例子
    public class RegisterInterface {
        @Test
        public void test1() throws Exception{
            //准备url;
            String url="http://8080/lmcanon_web_auto/mvc/member/api/member";
            //接口提交的方式;
            HttpPost post=new HttpPost(url);
            //准备传参;
            List<NameValuePair>params=new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("mobilephone", "135173"));
            params.add(new BasicNameValuePair("pwd", "e10adc3949ba59abbe56e057f"));
            //将参数设置到post请求中
            post.setEntity(new UrlEncodedFormEntity(params));
            //发送请求;
            CloseableHttpClient httpclient=HttpClients.createDefault();
            CloseableHttpResponse response=httpclient.execute(post);
            //获取接口返回数据.
            String jsonStr=EntityUtils.toString(response.getEntity());
            System.out.println(jsonStr);
        }}
        
        @Test
        public void get() throws Exception{
            //定义接口地址
            String interfacesurl="http://8080/futureload/mvc/api/member/list";
            //决定接口提交方式
            HttpGet httpGet=new HttpGet(interfacesurl);
            //直接提交接口请求,准备客户端
            CloseableHttpClient httpClient=HttpClients.createDefault();
            CloseableHttpResponse response=httpClient.execute(httpGet);
            //获取相应数据
            String jsonString=EntityUtils.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,List<NameValuePair>params){
            //调用函数StringUtils.isEmpty(uri),对uri是否为空的处理
            if(StringUtils.isEmpty(uri)){
                return "";
                }
            //创建httpPost对象
            HttpPost httpPost=new HttpPost(uri);
            String resultString="";
            //设置参数到httpPost对象中
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                //准备httpclient客户端
                CloseableHttpClient httpClient= HttpClients.createDefault();
                //发送请求
                CloseableHttpResponse response=httpClient.execute(httpPost);
                //解析数据
                resultString=EntityUtils.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,List<NameValuePair>params){
            //调用函数StringUtils.isEmpty(uri),对uri是否为空的处理
            if(StringUtils.isEmpty(uri)){
                return "";
                }
            if(params==null){
                return "";
            }
            for (int i=0;i<params.size();i++) {
                NameValuePair nameValuePair=params.get(i);
                if(i==0){
                uri+=("?"+nameValuePair.getName()+"="+nameValuePair.getValue());
                }else{
                    uri+=("&"+nameValuePair.getName()+"="+nameValuePair.getValue());
                }
            }
            //创建httpGet对象
            HttpGet httpGet=new HttpGet(uri);
            String resultString="";
            //设置参数到httpPost对象中
            try {
    //            httpGet.setEntity(new UrlEncodedFormEntity(params));
                //准备httpclient客户端
                CloseableHttpClient httpClient= HttpClients.createDefault();
                //发送请求
                CloseableHttpResponse response=httpClient.execute(httpGet);
                //解析数据
                resultString=EntityUtils.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=(content==null||content!=null&&content.trim().length()==0);
            return isEmpty;
        }
    }

  • 相关阅读:
    struts2 spring3 整合
    SilverLight 银光 基础.net 跨网页,桌面软件体验更好,但是要这个插件
    struts 理解 action
    vb 坐标点击
    错误struts2 json There is no Action mapped for namespace
    struts 与 jquery 整合
    springmvc jquery 界面没回应
    css 相关学习
    spring + ehcache 配置
    jquery json 结合
  • 原文地址:https://www.cnblogs.com/AIME/p/8907207.html
Copyright © 2011-2022 走看看