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

原文地址:https://www.cnblogs.com/xiaoshubass/p/12865232.html