手把手教你搭建java接口自动化测试框架(三):基础代码填充

手把手教你搭建java接口自动化测试框架(三):基础代码填充

base包下新建TestBase.java

package com.qa.base;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class TestBase {
    //新建测试基类 此类为所有测试类的父类
    public Properties pro;
    //读取配置文件  把读取配置文件的操作卸载构造方法中  我也不知道为什么(摊手 可能这样 效率比较高
    public TestBase()  {
        pro = new Properties();
        try {
            FileInputStream fis = new FileInputStream(System.getProperty("user.dir")//获取当前工程目录
                                      + "/src/main/com/qa/config/config.properties");//获取config.properties文件目录
            pro.load(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

config包下新建config.properties文件

#项目的根url
HOST = https://reqres.in

#测试数据excel地址   进阶版本  数据驱动接口测试会用到  我本人目前是不会(lll¬ω¬)
#postdata = xxxxx
#getdata = xxxx

https://reqres.in是一个提供免费接口调用的网站

restClient包下新建ResClient.java

package com.qa.restClient;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

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

public class RestClient {
    //本类中包含post get put delete请求方法
    //1 get请求 不带请求头
    /**
     *
     * @param url  请求地址
     * @return
     * @throws IOException
     */
    public CloseableHttpResponse getApi(String url) throws IOException {
        //新建一个可关闭的HTTPclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //新建get对象
        HttpGet get = new HttpGet(url);

        //执行 get请求  存储返回的响应
        CloseableHttpResponse httpResponse = httpClient.execute(get);
        return httpResponse;
    }
    //2 get请求 带有请求头 方法重载
    public CloseableHttpResponse getApi(String url , HashMap<String,String> headermap) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        //加载请求头到HTTP中
        for (Map.Entry<String, String> entry : headermap.entrySet()) {
            httpGet.addHeader(entry.getKey(),entry.getValue());
        }
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
        return httpResponse;
    }
    /**
     *
     * @param url
     * @param entityString 设置json 请求参数
     * @param headermap 请求头
     * @return
     * @throws IOException
     */
    //3 post请求
    public CloseableHttpResponse postApi(String url, String entityString, HashMap<String,String> headermap) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        //设置payload
        httpPost.setEntity(new StringEntity(entityString));
        //加载请求头对象到 httppost
        for (Map.Entry<String,String> entry : headermap.entrySet()){
            httpPost.addHeader(entry.getKey(),entry.getValue());
        }
        //发送post请求
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        return httpResponse;
    }
    //4 Put方法
    public CloseableHttpResponse put(String url, String entityString, HashMap<String,String> headerMap) throws ClientProtocolException, IOException {

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPut httpput = new HttpPut(url);
        httpput.setEntity(new StringEntity(entityString));
        for(Map.Entry<String, String> entry : headerMap.entrySet()) {
            httpput.addHeader(entry.getKey(), entry.getValue());
        }
        //发送put请求
        CloseableHttpResponse httpResponse = httpclient.execute(httpput);
        return httpResponse;
    }
    //5 Delete方法
    public CloseableHttpResponse delete(String url) throws ClientProtocolException, IOException {

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpDelete httpdel = new HttpDelete(url);
        //发送put请求
        CloseableHttpResponse httpResponse = httpclient.execute(httpdel);
        return httpResponse;
    }
}

到这一步呢请求方法就基本完成了 加上请求内容
就可以进行接口测试了
是不是很想试一试(~ ̄▽ ̄)~

请看下集👇

  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是手把手搭建 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 的基本步骤,希望对你有帮助!如果有任何问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值