栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

HttpClient5 - testNG集成HttpClient之编写Get自动化测试用例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HttpClient5 - testNG集成HttpClient之编写Get自动化测试用例

目录

1. 概述

2. 编写Get自动化测试用例 

2.1 TestNG XML文件 


1. 概述

首先将HttpClient和TestNG进行集成,如何进行HTTP接口自动化测试。

创建maven工程,引入如下依赖:

1. TestNG: 自动化测试框架TestNG的依赖。

2. JSON: 用于处理JSON的数据,在Restful POST请求体以及响应体中均可使用。

3. DOM4J: 用于处理XML数据,在SOAP请求的请求体和响应体中均可使用。

4. JAXEN: 当DOM4J用XPath方式获取节点时需要依赖该jar.

在依赖下载完成后,在工程右键, TestNG->Convert to TestNG, 从而生成TestNG xml测试驱动文件。

    
        org.testng
        testng
        6.11
    
    
        org.apache.httpcomponents
        httpclient
        4.5.9
    
    
        org.json
        json
        20220320
    
    
        org.dom4j
        dom4j
        2.1.3
    
    
        jaxen
        jaxen
        1.2.0
    

2. 编写Get自动化测试用例 

1. 创建GetMobilePhoneTest. 

2. init()方法初始化了一个HTTP客户端,并在sendHttpGetRequests()方法中使用,最后在clear()方法中关闭。

3. 将构建URI, 发送GET请求和转换服务器响应等过程封装在sendHttpGetRequest()方法中,通过传递HTTP客户端和参数来使用该方法。

4. 在clear()方法中对HTTP客户端和服务器进行关闭操作,在操作中可能出现异常,所以需要对异常进行捕获和处理。  

package com.pig.httpinterfacetest;

import java.net.URI;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;


public class GetMobilePhoneTest {
    private CloseableHttpClient client;
    private CloseableHttpResponse response;

    @BeforeClass
    public void init(){
        client = HttpClients.createDefault();
    }
    @Test
    public void testCase1(){
        Assert.assertEquals("{"brand":"Apple","model":"iPhone 6S","os":"IOS"}", sendHttpGetRequest(client, "iPhone 6S"));
    }
    @Test
    public void testCase2(){
        Assert.assertEquals("", sendHttpGetRequest(client, ""));
    }
    @Test
    public void testCase3(){
        Assert.assertEquals("", sendHttpGetRequest(client, null));
    }
    @Test
    public void testCase4(){
        Assert.assertEquals("", sendHttpGetRequest(client, "0123456789012345678901234567890123456789"));
    }
    @AfterClass
    public void clear(){
        try{
            response.close();
            client.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private String sendHttpGetRequest(CloseableHttpClient client, String model){
        String result = null;
        try {
            URI uri = new URIBuilder().setScheme("http").setHost("localhost").setPort(8080).setPath("/mobilePhone")
                    .setParameter("model", model).build();
            response = client.execute(new HttpGet(uri));
            result = EntityUtils.toString(response.getEntity());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result
    }
}

2.1 TestNG XML文件 



    
        
            
        
    

运行结果

 

 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/980719.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号