Java+Excel+POI+testNG基于数据驱动做一个简单的接口测试【杭州多测师_王sir】

这篇具有很好参考价值的文章主要介绍了Java+Excel+POI+testNG基于数据驱动做一个简单的接口测试【杭州多测师_王sir】。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、创建一个apicases.xlsx放入到eclipse的resource里面,然后refresh刷新一下

Java+Excel+POI+testNG基于数据驱动做一个简单的接口测试【杭州多测师_王sir】,java,excel,开发语言

二、在pom.xml文件中加入poi和testng的mvn repository、然后在eclipse的对应目录下放入features和plugins,重启eclipse就可以看到testNG了

        <!--poi excel解析 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.8</version>
            <scope>test</scope>
        </dependency>

三、封装一个读取Excel表格的工具类

package com.duoceshi.test;

import java.io.InputStream;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;;

public class ExcelUtil1 {
    
    public static Object[][] readExcel1(String excelPath) throws Exception {
        InputStream is = null;
        Workbook workbook = null;
        try {
            is = ExcelUtil1.class.getResourceAsStream(excelPath);
            workbook = WorkbookFactory.create(is);
            Sheet sheet = workbook.getSheetAt(0);  //获取到具体的sheet
            //通过sheet获取行数
            int lastRowNum = sheet.getLastRowNum();
            Object[][] allData = new Object[lastRowNum][];  //创建二维数组存Excel表格数据
            System.out.println(lastRowNum);
            for (int i = 1; i <= lastRowNum; i++) {
                Row row = sheet.getRow(i);
                
                int lastColumn = row.getLastCellNum();
                Object[] objects = new Object[lastColumn];
                for (int j = 0; j < lastColumn; j++) {
                    Cell cell = row.getCell(j, MissingCellPolicy.CREATE_NULL_AS_BLANK);
                    cell.setCellType(CellType.STRING);  //把cell当成字符串处理
                    String value = cell.getStringCellValue();
                    System.out.println(value);
                    objects[j] = value;
                }
                allData[i-1] = objects;
            }
            return allData;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            is.close();
            workbook.close();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        readExcel1("/apicases.xlsx");
    }

}

四、通过testng创建一个testng类,引入@DataProvider数据提供者

package com.duoceshi.test;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.alibaba.fastjson.JSON;

/**
 * 读取Excel表格数据 做数据驱动
 * 
 * @author 多测师王sir
 *
 */
public class LoginTest1 {

    @DataProvider
    public Object[][] dp() throws Exception {
        Object[][] allData = ExcelUtil1.readExcel1("/apicases.xlsx");
        for (int i = 0; i < allData.length; i++) {
            Object[] objects = allData[i];
            for (int j = 0; j < objects.length; j++) {
                Object object = objects[j];
                System.out.println(object);
            }
        }
        return allData;
    }

    @SuppressWarnings("unchecked")
    @Test(dataProvider = "dp")
    public void loginTest(String url, String requestBody, String result) throws Exception {
        List<NameValuePair> allData = new ArrayList<NameValuePair>();
        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        map = JSON.parseObject(requestBody, map.getClass());  //把字符串转换为map类型
        Set<String> ketSet = map.keySet();
        for (String key : ketSet) {
            String value =  map.get(key);
            allData.add(new BasicNameValuePair(key, value));
        }
        String requestBodyStr = URLEncodedUtils.format(allData, "utf-8");
        String baseUrl = url + "?" + requestBodyStr;
        CloseableHttpClient client =  HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(baseUrl);
        CloseableHttpResponse reponseStr = client.execute(httpPost);
        HttpEntity httpEntity =  reponseStr.getEntity();
        String responseEntity = EntityUtils.toString(httpEntity);
        //对响应文本进行断言
        Assert.assertTrue(responseEntity.contains(result));
        System.out.println(responseEntity);
    }
}

五、运行输出结果:

{"code":"200","msg":"登录成功!","model":{}}
{"code":"400","msg":"登录密码不正确!","model":{}}文章来源地址https://www.toymoban.com/news/detail-647547.html

到了这里,关于Java+Excel+POI+testNG基于数据驱动做一个简单的接口测试【杭州多测师_王sir】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

原文地址:https://blog.csdn.net/weixin_39362573/article/details/132242940

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包