手把手教你搭建java接口自动化测试框架(四):get、post方法实现

手把手教你搭建java接口自动化测试框架(四):get、post方法实现

看完了三篇 各种配置、代码 到这一篇终于可以进行实战了
是骡子是马拉出来溜溜( •̀ ω •́ )✧

执行get请求
tests包下新建GetTest01.java

package com.qa.tests;
import com.alibaba.fastjson.JSONObject;
import com.qa.base.TestBase;
import com.qa.restClient.RestClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.io.IOException;
public class GetTest01 extends TestBase {
    TestBase testBase;
    RestClient restClient;
    String host;
    String url;
    CloseableHttpResponse response;
    @BeforeClass
    public void setUp(){
        testBase = new TestBase();
        host = testBase.pro.getProperty("HOST");//读取config.properties里面的根url
        url = host + "/api/users?page=2"; //进入https://reqres.in/ 网站下拉 有Get/Post/Put方法测试的说明文档
    }
    @Test
    public void getListUsers() throws IOException {
        restClient = new RestClient();
        response = restClient.getApi(url);
        //获取响应内容
        String responseString = EntityUtils.toString(response.getEntity(),"UTF-8");
        //创建JSON对象  把得到的响应字符串 序列化成json对象
        JSONObject responseJson = JSONObject.parseObject(responseString);
        System.out.println("response json---->" + responseJson);
    }
}

输出的response json内容应该跟页面一样
在这里插入图片描述
在这里插入图片描述
执行post请求
tests包下新建PostTest01.java
data包下新建userData.java
前面的步骤还是一样 不过post请求带参数 所以需要传入指定参数,参数就在userData里面啦

package com.qa.tests;

import com.alibaba.fastjson.JSON;
import com.qa.base.TestBase;
import com.qa.data.userData;
import com.qa.restClient.RestClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.HashMap;

public class PostTest01 {
    TestBase testBase;
    RestClient restClient;
    String host;
    String url;
    CloseableHttpResponse response;

    @BeforeClass
    public void setUp(){
        testBase = new TestBase();
        host = testBase.pro.getProperty("HOST");//读取config.properties里面的根url
        url = host + "/api/users"; //进入https://reqres.in/ 网站下拉 有Get/Post/Put方法测试的说明文档
    }
    @Test
    public void postCreate() throws IOException {
        restClient = new RestClient();
        //准备请求头信息
        HashMap<String,String> headermap = new HashMap<String, String>();
        headermap.put("Content-Type","application/json");

        //实例化数据对象 并将其转换成json格式
        userData userData = new userData("morpheus","leader");
        String dataJsonString = JSON.toJSONString(userData);
        //post请求
        response = restClient.postApi(url,dataJsonString,headermap);
        //得到响应结果 并输出
        String responseString = EntityUtils.toString(response.getEntity());
        System.out.println("response---->" + responseString);
    }
}
package com.qa.data;

public class userData {
    //登录网站查看接口说明 发现post 方法需要传入两个参数 name job
    private String name;
    private String job;

    public userData() {
    }
    public userData(String name, String job) {
        this.name = name;
        this.job = job;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }
}

运行成功✌
在这里插入图片描述
在这里插入图片描述
运行成功了 人工对比接口文档也没问题
然鹅 每个接口都可靠人工对比是不是效率太低了呢╮(╯-╰)╭

所以!!断言功能用起来 这在接口测试中也是十分重要的一环
通过编写断言来判断接口得到的值是否正确

那么断言怎么使用呢
请看下集👇

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值