zoukankan      html  css  js  c++  java
  • 用Java语言实现单接口的批量测试【多测师】

    一、准备3条测试数据:
    1.合格手机号,合格密码
    2.重复以上的手机号,密码
    3.传手机号,不传密码

    import java.util.ArrayList;
    import java.util.List;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    import org.testng.annotations.Test;
    public class Tester {
            public static void main(String[] args) {
                #main方法用来做测试
            }
            @Test
            public void test1(){
                    String restUrl = "http://119.23.241.154:8080/futureloan/mvc/api/member/register";
                    //1.创建post对象,以post方式提交接口请求
                    HttpPost httpPost = new HttpPost(restUrl);
                    //2.准备提交参数
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    BasicNameValuePair basicNameValuePair1 = new BasicNameValuePair("mobilephone", "18999700122");
                    BasicNameValuePair basicNameValuePair2 = new BasicNameValuePair("pwd", "123456");
                    params.add(basicNameValuePair1);
                    params.add(basicNameValuePair2);
                    //3.参数封装到请求体当中
                    try {
                            httpPost.setEntity(new UrlEncodedFormEntity(params));
                            System.out.println("method:"+httpPost.getMethod());
                            //4.准备客户端
                            CloseableHttpClient httpClient = HttpClients.createDefault();
                            //5.提交请求
                            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
                            //6.解析接口返回数据,返回字符串
                            String result = EntityUtils.toString(httpResponse.getEntity());
                            //7.输出结果到控制台验证数据
                            System.out.println("*********返回数据:"+result);
                    } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
            }
            @Test
            public void test2(){
                    String restUrl = "http://119.23.241.154:8080/futureloan/mvc/api/member/register";
                    //1.创建post对象,以post方式提交接口请求
                    HttpPost httpPost = new HttpPost(restUrl);
                    //2.准备提交参数
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    BasicNameValuePair basicNameValuePair1 = new BasicNameValuePair("mobilephone", "18999700122");
                    BasicNameValuePair basicNameValuePair2 = new BasicNameValuePair("pwd", "123456");
                    params.add(basicNameValuePair1);
                    params.add(basicNameValuePair2);
                    //3.参数封装到请求体当中
                    try {
                            httpPost.setEntity(new UrlEncodedFormEntity(params));
                            System.out.println("method:"+httpPost.getMethod());
                            //4.准备客户端
                            CloseableHttpClient httpClient = HttpClients.createDefault();
                            //5.提交请求
                            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
                            //6.解析接口返回数据,返回字符串
                            String result = EntityUtils.toString(httpResponse.getEntity());
                            //7.输出结果到控制台验证数据
                            System.out.println("*********返回数据:"+result);
                    } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
            }
            @Test
            public void test3(){
                    String restUrl = "http://119.23.241.154:8080/futureloan/mvc/api/member/register";
                    //1.创建post对象,以post方式提交接口请求
                    HttpPost httpPost = new HttpPost(restUrl);
                    //2.准备提交参数
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    BasicNameValuePair basicNameValuePair1 = new BasicNameValuePair("mobilephone", "");
                    BasicNameValuePair basicNameValuePair2 = new BasicNameValuePair("pwd", "123456");
                    params.add(basicNameValuePair1);
                    params.add(basicNameValuePair2);
                    //3.参数封装到请求体当中
                    try {
                            httpPost.setEntity(new UrlEncodedFormEntity(params));
                            System.out.println("method:"+httpPost.getMethod());
                            //4.准备客户端
                            CloseableHttpClient httpClient = HttpClients.createDefault();
                            //5.提交请求
                            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
                            //6.解析接口返回数据,返回字符串
                            String result = EntityUtils.toString(httpResponse.getEntity());
                            //7.输出结果到控制台验证数据
                            System.out.println("*********返回数据:"+result);
                    } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
            }
            @Test(dataProvider="datas")    
    #改动点:这里加了(dataProvider="datas") 数据提供者
            public void test(String mobilephone,String pwd){ 
    这里加了2个形式参数(String mobilephone,String pwd)
                    String restUrl = "http://119.23.241.154:8080/futureloan/mvc/api/member/register";
                    //1.创建post对象,以post方式提交接口请求
                    HttpPost httpPost = new HttpPost(restUrl);
                    //2.准备提交参数
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    BasicNameValuePair basicNameValuePair1 = new BasicNameValuePair("mobilephone", mobilephone);   
    #这里以参数的形式传入
                    BasicNameValuePair basicNameValuePair2 = new BasicNameValuePair("pwd", pwd);
                    params.add(basicNameValuePair1);
                    params.add(basicNameValuePair2);
                    //3.参数封装到请求体当中
                    try {
                            httpPost.setEntity(new UrlEncodedFormEntity(params));
                            System.out.println("method:"+httpPost.getMethod());
                            //4.准备客户端
                            CloseableHttpClient httpClient = HttpClients.createDefault();
                            //5.提交请求
                            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
                            //6.解析接口返回数据,返回字符串
                            String result = EntityUtils.toString(httpResponse.getEntity());
                            //7.输出结果到控制台验证数据
                            System.out.println("*********返回数据:"+result);
                    } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
            }
    }

     

    二、用dataprovider数据提供者
     


    测试用例执行结果为:
     

    三、总结:

    对于同一接口的批量测试,变动的数据可能就只有测试数据,因此,我们我们可以考虑通过@Dataprovider来提供几组测试数据,

    测试方法引用了dataprovider后就能拿到数据,依次注入完成批量测试,从而简化代码,简化测试。

  • 相关阅读:
    linux shell习题
    The logback manual #02# Architecture
    The logback manual #01# Introduction
    算法导论(第三版)练习 10.1-1 ~ 10.1-7
    算法笔记 #006# 快速排序 × 算法导论(第三版)练习 7.1-1 ~ 7.1-4
    Linux笔记 #08# shell编程从零开始到低配学生管理系统
    Maven笔记 #01# 入门
    Java日志学习资料收集
    jsp中用EL读取了数据库里面的时间,怎么设置格式显示的格式
    ajax异步处理时,如何在JS中获取从Servlet或者Action中session,request
  • 原文地址:https://www.cnblogs.com/xiaoshubass/p/12865232.html
Copyright © 2011-2022 走看看