承接上篇

改变我们的测试驱动方式,灵活设置测试用例。

数据驱动测试

数据驱动测试的核心是:

测试数据与测试脚本分离,实现测试脚本参数化,

提高测试脚本的可重用性。在自动化功能测试中如果灵活使用数据源与测试脚本,

便能轻松创建与运行成百上千个测试用例。自动化测试框架必须要有与电子表格、文本文件、数据库集成的能力。

首先小伙伴们就会问,你要先解析Excel吧,那你肯定得给我上代码,是的,必须的上代码。

package com.testapi.until;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
private static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static XSSFCell Cell;
private static XSSFRow Row;
public static Object[][] getTableArray(String FilePath, String SheetName) throws Exception {
String[][]tabArray = null;
try {
FileInputStream ExcelFile = new FileInputStream(FilePath);
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
int startRow = 1;
int startCol = 1;
int ci,cj = 0;
int totalRows = ExcelWSheet.getLastRowNum();
int totalCols = 2;
tabArray=new String[totalRows][6];
ci=0;
cj=0;
int cm = 0;int cl = 0;int ch = 0;
for (int i=startRow;i<=totalRows;i++, ci++) {
tabArray[ci][0]=getCellData(i,2);
tabArray[ci][1]=getCellData(i,3);
tabArray[ci][2]=getCellData(i,4);
tabArray[ci][3]=getCellData(i,5);
tabArray[ci][4]=getCellData(i,6);
}
}
catch (FileNotFoundException e){
System.out.println("Could not read the Excel sheet");
e.printStackTrace();
}
catch (IOException e){
System.out.println("Could not read the Excel sheet");
e.printStackTrace();
}
return(tabArray);
}
public static String getCellData(int RowNum, int ColNum) throws Exception {
try{
Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
int dataType = Cell.getCellType();
if (dataType == 3) {
return "";
}
else{
String CellData = Cell.getStringCellValue();
return CellData;
}
}
catch (Exception e){
// System.out.println(e.getMessage());
throw (e);
}
}
public static void main(String[] args) throws Exception {
ExcelUtils excelUtils=new ExcelUtils();
Object[][] m = excelUtils.getTableArray("C:\\Users\\Administrator\\eclipse-workspace\\ApiTest\\casedata\\casedata.xlsx","Sheet1"); }
}

这就是我们的代码,那么小伙伴迫不及待了,你快来告诉我,

你的测试用例怎么组织的,好的 ,热腾腾的钙素你,我给你的就是代码,就是源码。源码如下

package com.testapi.casev;
import static org.testng.Assert.assertEquals; import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test; import com.testapi.until.ExcelUtils;
import com.testapi.until.Getcode;
import com.testapi.until.Makejson;
import com.testapi.until.ParseJson;
import com.testapi.until.*;
@Listeners({ZTestReport.class})
public class Testapi {
@DataProvider(name="DP1")
public Object[][] createData() throws Exception {
ExcelUtils excelUtils=new ExcelUtils();
Object[][] m = ExcelUtils.getTableArray("casedata\\casedata.xlsx","Sheet1");
return m;
}
@Test(dataProvider="DP1")
public void f(String url,String path,String meth,String map,String jsonid,String qiwang) {
String param1 = Makejson.makejson(map);
Getcode getcode=new Getcode();
url=url+path;
String resulst=getcode.getHttpResponse(param1, url,meth.toUpperCase());
String bnei=ParseJson.Json(resulst);
assertEquals(bnei,qiwang);
}
@BeforeTest
public void beforeTest() {
}
@AfterTest
public void afterTest() {
} }

这就是源码,这就是源码,

那么,你能让我看看你的Excel怎么写的吗,可以

目前支持的断言是断言code的字段,其实还可以丰富,接下来会优化这方面。

运行testng测试

控制台输出

最后的测试报告

测试报告,要感谢飞总的ztest

开源代码   github  传送门   喜欢的可以star。

作者寄语:

前进的道路我们充满着迷茫,

前进的每一步我们都会有收获。

路在脚下,我们决定不了我们的出身,但是我们可以努力改变我们未来。

告别昨天失败的自己,努力拼搏今天,成就美好明天

java 接口测试,使用excel做数据驱动(二)的更多相关文章

  1. Selenium+java - 使用csv文件做数据驱动

    前言 早期我们使用TestNG 来做数据驱动进行测试,测试数据是写在测试用例脚本中.这会使得测试脚本的维护工作量很大.因此我们可以将测试的数据和脚本分开. 而我们经常使用会使用csv文件来做为导出数据 ...

  2. java结合testng,利用excel做数据源的数据驱动实例

    数据驱动部分,是自动化测试常用部分,也是参数化设计的重要环节,前面分享了,mysql.yaml做数据源,那么再来分享下excel做数据驱动 思路: 先用POI读取excel.解析读取数据,返回list ...

  3. 接口测试 java+httpclient+testng+excel

    最近项目不忙,研究了下java实现接口自动化,借助testng+excel实现数据驱动 目前只用post方式测试,返回结果列没有通过列名去找 另外,请求参数是转义之后的,接口之间的依赖也是个问题,批量 ...

  4. java读取大容量excel之二(空格、空值问题)

    最近在项目中发现,对于Excel2007(底层根本是xml) ,使用<java读取大容量excel之一>中的方式读取,若待读取的excel2007文件中某一列是空值,(注意,所谓的空值是什 ...

  5. Testng之使用@DataProvider注解做数据驱动【转】

    原文:http://www.jianshu.com/p/8e333a0ec42a 前两天学了一下@DataProvider,今天有时间总结一下.testng很强大,提供了很多注解,其中利用@DataP ...

  6. 12、借助Jacob实现Java打印报表(Excel、Word)

    12.使用Jacob来处理文档 Word或Excel程序是以一种COM组件形式存在的.如果能够在Java中调用相应组件,便能使用它的方法来获取文档中的文本信息.Jacob是一个JAVA到微软的COM接 ...

  7. [Java聊天室server]实战之二 监听类

    前言 学习不论什么一个稍有难度的技术,要对其有充分理性的分析,之后果断做出决定---->也就是人们常说的"多谋善断":本系列尽管涉及的是socket相关的知识,但学习之前,更 ...

  8. Java JDBC的基础知识(二)

    在我的上一篇Java JDBC的基础知识(一)中,最后演示的代码在关闭资源的时候,仅仅用了try/catch语句,这里是有很大的隐患的.在程序创建连接之后,如果不进行关闭,会消耗更多的资源.创建连接之 ...

  9. 20172301 《Java软件结构与数据结构》实验二报告

    20172301 <Java软件结构与数据结构>实验二报告 课程:<Java软件结构与数据结构> 班级: 1723 姓名: 郭恺 学号:20172301 实验教师:王志强老师 ...

随机推荐

  1. app.get is not a function解决方案

    在express4.x中app.js被申明为一个模块,而不是一个主程序入口,在文件的最后暴露出了这个模块,如下所示 app.js module.exports = app; 但是我们在routes目录 ...

  2. DOM 对象

    DOM  ==  document object model   document 对象是唯一同时属于  BOM 和 DOM  的   rows 是一种DOM集合,不是数组,所以没有sort() 函数 ...

  3. 30个CSS3选择器的应用

    或许大家平时总是在用的选择器都是: id class 以及标签选择器.可是这些还远远不够,为了在开发中更加得心应手,本文总结了30个CSS3选 或许大家平时总是在用的选择器都是:#id  .class ...

  4. phpstudy APACHE支持.htaccess以及 No input file specified解决方案

    APACHE支持.htaccess以及 No input file specified解决方案 你的Apache安装文件夹conf里找到httpd.conf文件 索LoadModule rewrite ...

  5. 学习C++ 模板类

    #include<iostream>#include<typeinfo>#include<cstring> using namespace std; class A ...

  6. Codeforces 862A Mahmoud and Ehab and the MEX

    传送门:CF-862A A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 ...

  7. smm框架整合实现登录功能

    一.准备所需的jar包 1.1所需jar包 1.Spring框架jar包 2.Mybatis框架jar包 3.Spring的AOP事务jar包 4.Mybatis整合Spring中间件jar包 5.a ...

  8. 一:Spring Boot、Spring Cloud

    上次写了一篇文章叫Spring Cloud在国内中小型公司能用起来吗?介绍了Spring Cloud是否能在中小公司使用起来,这篇文章是它的姊妹篇.其实我们在这条路上已经走了一年多,从16年初到现在. ...

  9. Android 工程师

    转发:https://zhuanlan.zhihu.com/p/30429725 这句话我真的憋了好久.Android 工程师只要关注我,我就能让你达到大师级水平,不是面试时的吹牛逼水平,不是自我欺骗 ...

  10. 使用GetThumbnailImage进行图片缩放操作

    /// <summary>        /// 获取等比例缩放图片的方法        /// </summary>        /// <param name=&q ...