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后就能拿到数据,依次注入完成批量测试,从而简化代码,简化测试。

  • 相关阅读:
    python入门基础
    iOS开发应用学习笔记
    ios开发之NavBar和TarBar使用技巧
    iOS开发中常用第三方库的使用和配置-GDataXML
    iOS开发中常用第三方库的使用和配置-GDataXML
    ios程序发布测试打包
    iOS的动画效果类型及实现方法
    iOS定位服务编程详解
    键盘样式风格有关设置-iOS开发
    如何在ios 系统 中抓包??
  • 原文地址:https://www.cnblogs.com/xiaoshubass/p/12865232.html
Copyright © 2011-2022 走看看