Search code examples
javatestngtestng-dataprovider

Testng does not count dataprovider tests individually


Here is my dataprovider

@DataProvider(name = "arrayBuilder")
public Object[][] parameterTestProvider() {
    //Code to obtain retailerIDList
    String[] retailerIDArray = retailerIDList.toArray(new String[retailerIDList.size()]);
assertEquals(1295, retailerIDList.size(), "Expected 1295, found " + retailerIDList.size() + " docs");
    return new Object[][] {{retailerIDArray}};
}

and this is my test

@Test(dataProvider = "arrayBuilder", invocationCount = 1, threadPoolSize = 1)
public void getRetailer(String[] retailerIDList) {

    for (String retailer_ID : retailerIDList) {
        //Code that uses the retailerID 
 }

When I execute this test, TestNG output lists "getRetailer" as the only test. I have 1295 records returned by dataprovider and I want 1295 tests to be reported. What am I missing?


Solution

  • Please use this, it should work. You need to return array of objects where each row is a row of data that you want to use for a test. Then only it will come in the report. What you were doing was sending it an array so it was treating it as a single test.

        @DataProvider(name="provideData")
        public  Iterator<Object[]> provideData() throws Exception
        {
            List<Object[]> data = new ArrayList<Object[]>();
            String[] retailerIDArray = retailerIDList.toArray(new String[retailerIDList.size()]);
            assertEquals(1295, retailerIDList.size(), "Expected 1295, found " + retailerIDList.size() + " docs");
            for(String retailerID : retailerIDArray ){
    
                data.add(new Object[]{retailerID});
    
            }
    
            return data.iterator(); 
    
        }
    
    @Test(dataProvider = "provideData")
    public void getRetailer(String retailerIDList) {
    
        for (String retailer_ID : retailerIDList) {
            //Code that uses the retailerID 
        }
    }
    

    For more Information please go through the documentation here