Search code examples
testngtestng-dataprovider

@DataProvider returns 100 column values


I am using TestNG framework for my automation. DataProvider method returns 100 column values(data needed for test) for each row from Excel sheet.I need to use them in my @Test. How do i pass 100 parameters in @Test method?Is there any easy way to do it? It is difficult to write 100 parameters in @Test method. Any other way of doing it?

@DataProvider(name="InputData")
public Object[][] InputData() throws IOException
{
    Object[][] data = testData(dataFile,"Sheet1");
    return data;
}


@Factory(dataProvider = "InputData")
public A_Test(String a,String b, , , , , , , , , , , )
{

        this.stream=a;
        this.Keyword=b;

}

Solution

  • Just pass varaggs String... str

    @Factory(dataProvider = "InputData")
    public A_Test(String... str) {
    }
    

    Some things to consider

    1. Big, unreadable test reports

    Every invocation is logged separately, with all parameters. It becomes unreadable even with about 5 parameters and about 50 invocations.

    2. Exception handling

    Doing complex logic in data provider is not an good idea. When exception occurs in @DataProvider all tests using it are skipped. When you read Excel file in @DataProvider it may fail because of file not found, file too big, wrong file format, etc.

    Most likely, you prefer those tests to fail, so your build would fail too. See http://rolf-engelhard.de/2011/10/fail-instead-of-skip-a-test-when-testngs-dataprovider-throws-an-exception/ for possible solutions. For me Solution 1 (test for data provider) or 2 (return empty array on exception) makes most sense.