具体实现如下:
@DataProvider(name="couponListData") public Object[][] couponListData(){ //自己定义Object[][]值 List<ListManage> couponManageList = ExcelUtil.simpleRead(AdminFileName.CouponManagePath, ListManage.class); Object[][] o = new Object[couponManageList.size()][]; for(int i=0; i<couponManageList.size();i++){ JSONObject jsonObject = new JSONObject(true); Object[] objects = new Object[3]; jsonObject.put("condition",couponManageList.get(i).getCondition());//code:券码编号,name:券码名字,customer_name:采购用户 jsonObject.put("content",couponManageList.get(i).getContent()); jsonObject.put("current_page",couponManageList.get(i).getCurrentPage()); jsonObject.put("page_size",couponManageList.get(i).getPageSize()); objects[0] = couponManageList.get(i).getCode(); objects[1] = couponManageList.get(i).getAccountInformation(); objects[2] = jsonObject; o[i] = objects; } return o; } @Test(dataProvider = "couponListData") public void couponList(String code,String accountInformation,JSONObject data) throws Exception{ }
不同类的用法:
数据驱动类DataProviderDemo .java
package com.yck.demo.data; import org.testng.annotations.DataProvider; import com.yck.demo.entity.TestDataVo; import com.yck.demo.utils.MybatisUtil; public class DataProviderDemo { @DataProvider(name = "providerName") public TestDataVo[][] demo() { String statement = "mapper.TestDataMapper.getdataByCaseName"; TestDataVo vo1 = MybatisUtil.excuteSelect(statement, "1"); TestDataVo vo2 = MybatisUtil.excuteSelect(statement, "2"); TestDataVo vo3 = MybatisUtil.excuteSelect(statement, "3"); TestDataVo[][] dataVo = {{vo1},{vo2},{vo3}}; return dataVo; } }
测试用例类:
package com.yck.demo.testcase; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import com.yck.demo.entity.TestDataVo; public class DemoTestCase { @Test(dataProvider= "providerName",dataProviderClass= com.yck.demo.data.DataProviderDemo.class) public void test(TestDataVo vo) { System.out.println(vo.getActual()); } }