TestNG DataProvider的使用

直接上代码吧:

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 []这样的一维数组了,没办法用了啊。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值