• 常用网络接口自动化测试框架应用


     一、RESTful(resource representational state transfer)类型接口测试

    (一)GUI界面测试工具:jmeter

    1、添加线程组

    2、添加http请求

    3、为线程组添加察看结果树

    4、写入接口参数并运行

    5、在查看结果树窗口查看结果

    6、多组数据可增加CSVDataSetConfig(添加.csv格式的文件,并在参数值里以${x}格式写入)

    此时变量值填写${变量名},上图x,y表示每次从文件里读取两个参数,分别命名为x,y

    (二)JAVA语言脚本测试(HttpClient)

    1、GET请求接口测试

    1. 1 public void TestGet throws URISyntaxException, ClientProtocolException, IOException{
    2. 2   //1、创建一个客户端对象
    3. 3   CloseableHttpClient client=HttpClients.createDefault();
    4. 4   //2、使用URIBuilder()来生成一个get类型的USI
    5. 5   URI uri=new URIBuilder().setScheme("http")
    6. 6                 .setPort(8080)
    7. 7                 .setHost("localhost")
    8. 8                 .setPath("/test1334/Calc")
    9. 9                 .setParameter("a", "2")
    10. 10                 .setParameter("b", "3").build();
    11. 11   //3、新建一个httpget类型请求对象,并将uri传入请求
    12. 12  HttpGet get=new HttpGet(uri);
    13. 13   //4、新建响应对象,用于接收客户端执行get结果
    14. 14  CloseableHttpResponse response=client.execute(get);
    15. 15   //5.从响应对象中提取实际结果,与预期结果进行比对
    16. 16   if(response.getStatusLine().getStatusCode()==200){
    17. 17  System.out.println(EntityUtils.toString(response.getEntity()));
    18. 18 }
    19. 19 }

    2、POST请求接口测试

    样例(测一个输入两个参数求和的接口):

    1. 1 public void TestPOST () throws ClientProtocolException, IOException{
    2. 2   //1.新建一个客户端对象
    3. 3   CloseableHttpClient client=HttpClients.createDefault();
    4. 4   //2.新建post类型请求对象,并传入uri
    5. 5   HttpPost post = new HttpPost("http://172.31.6.155:8080/test1334/Calc");
    6. 6   //3.使用NameValuePair对参数进行打包
    7. 7   List<NameValuePair> list=new ArrayList<NameValuePair>();
    8. 8   list.add(new BasicNameValuePair("a","1"));
    9. 9   list.add(new BasicNameValuePair("b","2"));
    10. 10   //4.对打包好的参数,使用UrlEncodedFormEntity工具类生成实体类型数据
    11. 11   //Consts.UTF_8设置服务器字符集类型
    12. 12   UrlEncodedFormEntity entity=new UrlEncodedFormEntity(list,Consts.UTF_8);
    13. 13   //5.将含有请求参数的实体对象放入到post请求对象里
    14. 14   post.setEntity(entity);
    15. 15   //6.新建一个响应对象接收客户端执行post请求的结果
    16. 16   CloseableHttpResponse response=client.execute(post);
    17. 17   //7.从响应对象中提取实际结果,与预期结果进行比对
    18. 18   if(response.getStatusLine().getStatusCode()==200){
    19. 19     System.out.println(EntityUtils.toString(response.getEntity()));
    20. 20 }
    21. 21 }

    3、自动化框架

    1. 1 @RunWith(Feeder.class)
    2. 2 public class getParameter {
    3. 3 @Test
    4. 4 @Source("data/datas.csv") //数据源
    5. 5 public void test_get(int x,int y,int expect) throws ClientProtocolException, URISyntaxException, IOException{//expect为预期结果,用于与实际结果进行比对
    6. 6 TestRESTfultest=new TestRESTful();//TestRESTful为前边创建TestGet所属类
    7. 7 int returns=test.TestGet(x, y);//此处的为修改后的TestGet,添加了参数和返回值;
    8. 8 assertEquals(returns,expect); //将结果与预期进行比较
    9. 9 }
    10. 10 }

    二、WebService接口测试

    (一)GUI界面测试工具:SoapUI

    1、新建项目

    2、输入WSDL地址或文件

    3、修改“?”内的数据

    4、开始测试

    (二)JAVA语言脚本测试(HttpClient)

    1、GET请求接口测试

    1. 1 public int testGet(int x, int y) throws RemoteException {
    2. 2 String target = "http://172.31.6.94:8080/axis2/services/calc?wsdl";//传入地址
    3. 3 //创建一个CalcStub对象
    4. 4     CalcStub stub = new CalcStub(target);
    5. 5 CalcStub.Add add = new CalcStub.Add();
    6. 6 //传入参数
    7. 7 add.setX(x);
    8. 8 add.setY(y);
    9. 9 AddResponse response = stub.add(add);//结果
    10. 10 int result = response.get_return();
    11. 11 return result;
    12. 12 }

    2、POST请求接口测试

    1. 1 public static void testPOST(int a,int b) throws ClientProtocolException, IOException{
    2. 2 //创建客户端对象
    3. 3 CloseableHttpClient cli=HttpClients.createDefault();
    4. 4 HttpPost po=new HttpPost("http://172.31.6.61:8080/axis2/services/MyService?wsdl");
    5. 5 //将soap协议内容添加进来,即soapXML字符串
    6. 6 String soapXML="http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.day3.com\">"
    7. 7 +""
    8. 8 +""
    9. 9 +""
    10. 10 +""+a+""
    11. 11 +""+b+""
    12. 12 +""
    13. 13 +""
    14. 14 +"";
    15. 15 //String转换成实体类型
    16. 16 StringEntity entity=new StringEntity(soapXML,Charset.forName("UTF-8"));
    17. 17 po.setEntity(entity);
    18. 18 CloseableHttpResponse re=cli.execute(po);
    19. 19 System.out.println((re.getEntity()).toString());
    20. 20 }

  • 相关阅读:
    学历不高?基础不好?誉天数通学员演绎完美逆袭,薪资狂涨4倍!
    测评测试测试测试测从入门到精通
    使用pyqt 创造一个软件,
    2023.11.4 Idea 配置国内 Maven 源
    C#开发的OpenRA游戏之调试菜单2
    文档管理控件Aspose.total for.NET 迎来重大更新,全新版本v22.7全新版本来袭,快来看看都有哪些~
    200、使用默认 Exchange 实现 P2P 消息 之 消息生产者(发送消息) 和 消息消费者(消费消息)
    【愚公系列】2022年09月 python人工智能-PyTorch环境配置及安装
    4种API性能恶化根因分析
    ES6中新增加的Proxy对象及其使用方式
  • 原文地址:https://blog.csdn.net/xiao1542/article/details/128191968