手把手教你搭建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
    评论
当然可以!以下是手把手搭建 YOLOv5 的步骤: 1. 准备工作: - 确保你的电脑已经安装了 Python 3.8 或更高版本。 - 安装 PyTorch:可以根据你的操作系统和 CUDA 版本选择合适的安装命令,例如: ```shell pip install torch torchvision torchaudio ``` - 安装其他依赖库: ```shell pip install opencv-python matplotlib numpy ``` 2. 下载 YOLOv5 源代码: - 在终端中运行以下命令来克隆 YOLOv5 仓库: ```shell git clone https://github.com/ultralytics/yolov5.git ``` 3. 安装依赖库: - 进入 yolov5 目录: ```shell cd yolov5 ``` - 安装依赖库: ```shell pip install -r requirements.txt ``` 4. 准备数据集: - 准备好你的训练数据集,并将图像和对应的标签文件放入一个文件夹中。 5. 配置模型和数据: - 编辑 `yolov5/models/yolov5s.yaml` 文件,根据你的需求进行配置,例如设置 `nc` 参数为你的类别数量。 - 编辑 `data.yaml` 文件,设置 `train` 和 `val` 的路径,并配置类别数量和类别名称。 6. 训练模型: - 在终端中运行以下命令来开始训练模型: ```shell python train.py --img 640 --batch 16 --epochs 100 --data data.yaml --cfg models/yolov5s.yaml --weights '' ``` - 这个命令将使用默认参数来训练 YOLOv5 模型,你可以根据需要进行调整。 7. 测试模型: - 在终端中运行以下命令来测试模型效果: ```shell python detect.py --source your_image.jpg --weights runs/train/exp/weights/best.pt --conf 0.4 ``` - 这个命令将使用训练好的模型对指定图像进行目标检测,你可以调整 `--conf` 参数来控制检测结果的置信度阈值。 以上就是搭建 YOLOv5 的基本步骤,希望对你有帮助!如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值