接口测试总结

1.后端那边更改数据之后,必须重新请求一次接口,以获取最新的请求结果

2.单独运行一个sql语句的时候,选择起始位置,右键运行这一行数据

3.当sql脚本的某个字段是变量,必须写上注释,以便更换

4.jdbc连接不上的时候,可能是因为mysql太新了。所以一般用5.1的mysql来工作

5.访问url时候,一般要把项目名写在前面,不然无法访问成功

6.如果遇到总是无法访问的,是因为项目没有部署到tomcat的webapp里面去,需要重新创建项目

7.jdbc实现代码

接口介绍

8.啥是接口:接口是后端给前端开发人员数据的一种方式,我们测试接口是为了给前端开发人员提前检验该接口是否是通过的,改变接口报文的参数,
该接口返回的数据是否发生相应的变化并且是正确的。
接口的形式:<?xml version="1.0" encoding="utf-8"?>
<fwpackage>
  <jsnr>
    <fwdm>1045</fwdm>
    <nsrsbh>500100201711203</nsrsbh>
    <jrdm>123456</jrdm>
  </jsnr>
  <ywnr><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<request>
<nsrsbh>500100201711203</nsrsbh>
<sblx>0</sblx>
<kpddm>456789</kpddm>
<kpdmc>2655478793</kpdmc>
<fplxdm>004</fplxdm>
<jqbh>499098896920</jqbh>
<xhdwdz>销货单位地址</xhdwdz>
<xhdwdh>15268360666</xhdwdh>
<xhdwkhyh>销货银行开户银行</xhdwkhyh>
<xhdwyhzh>33050163724100000218</xhdwyhzh>
<wlljfs>TCP</wlljfs>
<jsxxlx>1</jsxxlx>
<sfdd>1</sfdd>
</request>]]></ywnr>
</fwpackage>
这一类的数据我们是以全局变量来初始化的,在excel表格中如果需要另外给参数复制,就在局部变量赋值,这样报文拿到的就是局部变量的值
如何判断:我们可以通过接口返回的部分数据来判断接口的正确错误:
如:rtncode=0000
    rtnmsg=调用[开票终端添加]接口成功,终端成功添加!
接口的运行流程:如添加数据,运行该接口之后,真的就会在前端添加一条记录,编辑该记录其实报文和添加是一致的,我们只要拿到添加数据的唯一
标志放到编辑的发送报文中进行。
具体的使用:如我们的添加的终端的名称为小于20个字符的汉字,我们在报文中其他的参数不变,唯独改变终端名字符的大小,为空,大于0小于20,大于20,
全是字母,全是汉字,包含特殊字符,这些接口除了大于0小于20的汉字用例,其他的全是失败的,如果接口返回的数据和预期的一直,那么这个接口就是测试
通过的。
响应报文的格式:
响应报文:
<?xml version="1.0" encoding="utf-8"?>
<result> 
  <requestId>928688097d7d4a39ad52a59add0b58c8</requestId>  
  <rtncode>0000</rtncode>  
  <rtndata/>  
  <rtnmsg>调用[开票终端维护]接口成功,终端维护成功!</rtnmsg> 
</result>
其他验证接口的方式;跑完一条记录之后查看页面是否发生对应接口的变化,如果发生,既是成功

接口测试代码实现

 

![在这里插入图片描述](https://img-blog.csdnimg.cn/20181128150047371.png) public static void appadd() {
            try {
                URL url = new URL(ADD_URL);
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setRequestMethod("POST");
                connection.setUseCaches(false);
                connection.setInstanceFollowRedirects(true);
                connection.setRequestProperty("connection", "Keep-Alive");
                //connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
                connection.connect();
                //POST请求
                DataOutputStream out = new DataOutputStream(
                        connection.getOutputStream());
                JSONObject obj = new JSONObject();
                String message = java.net.URLEncoder.encode("哈哈哈","utf-8");
                obj.element("username", "bwgf8");
                obj.element("password", "a1234561");
                

                out.writeBytes("data="+obj.toString());
                System.out.println("data="+obj.toString());
                out.flush();
                out.close();
                //读取响应
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                String lines;
                StringBuffer sb = new StringBuffer("");
                while ((lines = reader.readLine()) != null) {
                    lines = new String(lines.getBytes(), "utf-8");
                    sb.append(lines);
                }
                System.out.println(sb);
                reader.close();
                connection.disconnect();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        public static String sendPost(String url, String param,String charset) {
            PrintWriter out = null;
            BufferedReader in = null;
            String result = "";
            String line;
            StringBuffer sb=new StringBuffer();
            try {
                URL realUrl = new URL(url);
                // 打开和URL之间的连接 
                URLConnection conn = realUrl.openConnection();
                // 设置通用的请求属性 设置请求格式
                conn.setRequestProperty("contentType", charset);  
                conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
                //设置超时时间
                conn.setConnectTimeout(60);
                conn.setReadTimeout(60);
                // 发送POST请求必须设置如下两行
                conn.setDoOutput(true);
                conn.setDoInput(true);
                // 获取URLConnection对象对应的输出流
                out = new PrintWriter(conn.getOutputStream());
                // 发送请求参数
                out.print(param);
                // flush输出流的缓冲
                out.flush();
                // 定义BufferedReader输入流来读取URL的响应    设置接收格式
                in = new BufferedReader(
                        new InputStreamReader(conn.getInputStream(),charset));
                while ((line = in.readLine()) != null) {
                    sb.append(line);
                }
                result=sb.toString();
            } catch (Exception e) {
                System.out.println("发送 POST请求出现异常!"+e);
                e.printStackTrace();
            }
            //使用finally块来关闭输出流、输入流
            finally{
                try{
                    if(out!=null){
                        out.close();
                    }
                    if(in!=null){
                        in.close();
                    }
                }
                catch(IOException ex){
                    ex.printStackTrace();
                }
            }
            return result;
        }  

        public static String sendPost(String url, String param) {
            PrintWriter out = null;
            BufferedReader in = null;
            String result = "";
            try {
                URL realUrl = new URL(url);
                // 打开和URL之间的连接
                URLConnection conn = realUrl.openConnection();
                // 设置通用的请求属性
                conn.setRequestProperty("accept", "*/*");
                conn.setRequestProperty("connection", "Keep-Alive");
                conn.setRequestProperty("user-agent",
                        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                // 发送POST请求必须设置如下两行
                conn.setDoOutput(true);
                conn.setDoInput(true);
                // 获取URLConnection对象对应的输出流
                out = new PrintWriter(conn.getOutputStream());
                // 发送请求参数
                out.print(param);
                // flush输出流的缓冲
                out.flush();
                // 定义BufferedReader输入流来读取URL的响应
                in = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            } catch (Exception e) {
                System.out.println("发送 POST 请求出现异常!"+e);
                e.printStackTrace();
            }
            //使用finally块来关闭输出流、输入流
            finally{
                try{
                    if(out!=null){
                        out.close();
                    }
                    if(in!=null){
                        in.close();
                    }
                }
                catch(IOException ex){
                    ex.printStackTrace();
                }
            }
            return result;
        }    
    
        public static void main(String[] args) {
            
                String sr=HttpRequest.sendPost("http://u.house.ifeng.com/user/login/validate?", "mobile=13133814608&pwd=933032shixinfa");
            //String sr2=HttpRequest.sendPost("https://www.baiwang.com/login/service/userRoute", "username=bwgf8&password=a123456");
            System.out.println(sr);
           // appadd();
        }

    }
restful格式的接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值