直接上代码吧:
public class XlsTestFactory {
private SoftAssert sa = new SoftAssert();
private String getSheet() throws Exception {
return new File("dataExcel/data.xlsx").getAbsolutePath();
}
@DataProvider(name = "getSource")
public Object[][] getSource() throws Exception {
InputStream inputStream = new FileInputStream(getSheet());
TotalSheetData totalSheetData = new TotalSheetData();
totalSheetData.inject(inputStream);
for (final Map.Entry<String, Container> totalSuite : totalSheetData.getContainerTotalMap()
.entrySet()) {
if (totalSuite.getValue().getActionTotalMap().get(totalSuite.getKey()).getRunMode().equals("Y")) {
InputStream inputStream2 = new FileInputStream(getSheet());
CaseSheetData caseSheetData = new CaseSheetData();
caseSheetData.inject(inputStream2, totalSuite.getValue().getActionTotalMap().get(totalSuite.getKey()).getSheetName());
HashMap<String, String>[][] obj = new HashMap[caseSheetData.getCaseContainerMap().size()][1];
LinkedHashMap<String, CaseAction> actionCaseMap = new LinkedHashMap<>();
ArrayList<String> key = new ArrayList<>();
for (final Map.Entry<String, Container> caseSuite : caseSheetData.getCaseContainerMap()
.entrySet()) {
key.add(caseSuite.getKey());
actionCaseMap.put(caseSuite.getKey(), caseSuite.getValue().getActionCaseMap().get(caseSuite.getKey()));
}
for (int i = 0; i < actionCaseMap.size(); i++) {
obj[i][0] = new HashMap<>();
obj[i][0].put("Id", actionCaseMap.get(key.get(i)).getId());
obj[i][0].put("TaskType", actionCaseMap.get(key.get(i)).getTaskType());
obj[i][0].put("MethodName", actionCaseMap.get(key.get(i)).getMethodName());
obj[i][0].put("MethodDescription", actionCaseMap.get(key.get(i)).getMethodDescription());
obj[i][0].put("RequestType", actionCaseMap.get(key.get(i)).getRequestType());
obj[i][0].put("Host", actionCaseMap.get(key.get(i)).getHost());
obj[i][0].put("Location", actionCaseMap.get(key.get(i)).getLocation());
obj[i][0].put("Params", actionCaseMap.get(key.get(i)).getParams());
obj[i][0].put("DBUrl", actionCaseMap.get(key.get(i)).getDBUrl());
obj[i][0].put("DBUserName", actionCaseMap.get(key.get(i)).getDBUserName());
obj[i][0].put("DBPassword", actionCaseMap.get(key.get(i)).getDBPassword());
obj[i][0].put("Commits", actionCaseMap.get(key.get(i)).getCommits());
obj[i][0].put("ClassName", actionCaseMap.get(key.get(i)).getClassName());
}
return obj;
}
}
return null;
}
@BeforeClass
public void setUp() throws MalformedURLException {
System.out.println("test begin~~\n");
}
@Test(dataProvider = "getSource")
public void test_Total(HashMap<String, String> map) throws Exception {
System.out.println("clasename = " + map.get("ClassName"));
Class<?> cla = Class.forName("com.jane.methods." + map.get("ClassName"));
Method method = cla.getMethod(map.get("MethodName"));
method.invoke(cla.newInstance());
sa.assertAll();
}
@AfterClass
public void tearDown() throws Exception {
System.out.println("test finish!!!\n");
}
}
在这里@Test表情里的dataprivider获取的是一个HashMap 对象,在@DataProvider里需要先产生一个HashMap [][] obj数组;最重要的是,这边这个obj数组的二维的长度只要是1就可以了,不要建多个,不然@Test获取到的就是HashMap []这样的一维数组了,没办法用了啊。。。