Java+Maven+TestNG接口(API)自动化测试二(转)

本文介绍了使用Java、Maven和TestNG进行接口自动化测试的实践。讲解了如何利用TestNG进行断言,实现POST、PUT、DELETE方法的请求,并展示了如何美化TestNG测试报告,以及通过Jenkins实现自动化测试和持续集成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

七、TestNG 断言

7.1  引入 TestNG
上一章中我们还没有很好的手段来执行测试和验证结果,这里我们引入 TestNG 来帮助完成这部分功能。

7.1.1 创建 TestNG 测试类

在项目目录 src/test/java 下的包 com.mytest.httpclient.test 下新建一个支持testNG 的类,类名为 testGet。

写入以下测试代码:

package com.mytest.httpclient.test;
 
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.CloseableHttpResponse; 
import org.apache.http.client.methods.HttpGet;
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.BeforeTest;
import org.testng.annotations.Test; 
import com.mytest.httpclient.Util; 
import com.alibaba.fastjson.JSON; 
import com.alibaba.fastjson.JSONObject; 
 
public class GetTest {
    CloseableHttpClient client; 
    CloseableHttpResponse response; 
    HttpEntity responseBody;
    int responseCode;
 
    @BeforeTest
    public void setUp() {
        // 创建一个httpClient的实例
        client = HttpClients.createDefault();
    }
 
    @Test
    public void getTest() throws ClientProtocolException, IOException { 
        String	url = "https://reqres.in/api/users?pages=2";
        // 创建一个httpGet请求实例
        HttpGet httpGet = new HttpGet(url);
        // 使用httpClient实例发送刚创建的get请求,并用httpResponse将响应接收
        response = client.execute(httpGet);
        // 从响应中提取出状态码
        responseCode = response.getStatusLine().getStatusCode();                     
        Assert.assertEquals(responseCode, 200, "The response code should be 200!");
        // 从响应中提取出响应主体
        responseBody = response.getEntity();
        //转为字符串
        String responseBodyString = EntityUtils.toString(responseBody,"utf-8");
        // 创建Json对象,把上面字符串序列化成Json对象
        JSONObject responseJson = JSON.parseObject(responseBodyString);
        // json内容解析
        int page = responseJson.getInteger("page"); Assert.assertEquals(page, 2, "The                                                                                                                                                     
        page value should be 2!"); 
        String firstName = Util.getValueByJPath(responseJson, "data[0]/first_name");
        Assert.assertEquals(firstName, "Michael", "The first name should be Michael!");
    }
}

这里我们在 beforeTest 里去创建一个 HttpClient 的实例,beforeTest 方法是 TestNG 内置的方法,它会在测试方法执行之前被执行,所以可以在该方法中写入一些初始化的代码,然后在 getTest 方法中调用上一章中封装好的方法获取响应主体和状态码。

最后通过 Util 去获取响应中的 first_name 信息,做为核心验证点。

在 getTest 方法里我们使用 Assert.assertEquals 方法做了两个断言:

1.     判断状态码是否正确;

2.     判断 first_name 是否正确。

7.1.2   执行测试并查看结果

可以选中该类,右键点击 Run As 中的 TestNG Test 开始测试,结果如下所示:

八、Post,Put 和 Delete 方法

8.1 发送 POST 方法


post 方法和 get 方法是我们在做接口测试时,用的最多的两个请求方法。 在发送请求时它们显著的一个差别就在于,get 方法我们只需要将 url 发送即可,post 我们还需发送一个请求主体;在作用方面,get 方法用于查询,不会改变服务器中的信息;而 Post 方法可用于修改服务器中的信息。

8.1.1 修改 HttpClientUtil 实现发送 POST 请求和获取响应

根据请求的数据是 JSON 对象还是表单形式的键值对,分成了 sendPostByJson 和 sendPostByForm 两类方法,其中 sendPostByForm 方法用 ArrayList 存储 NameValuePair 键值对,再设置请求的主体;sendPostByJson 方法将对象序列化为 JONS 字符串,再设置请求的主体。
 

public JSONObject sendPostByJson(String url, Object object, HashMap<String, String> headers) throws Exception {
    httpClient = HttpClients.createDefault(); 
    HttpPos
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值