views:

1638

answers:

3

We would like to run some of our tests each against a set of data values, verifying that the same conditions hold true for each. The data is currently stored in either flat files or in simple Excel spreadsheets.

My first thought was to create a TestNG DataProvider that would load the data from the file and be used to call the test method once for each data value. My problem is that different tests need to load data from different files and there doesn't appear to be any way to send a parameter to the DataProvider. Does anyone know if this is possible?

Ideally, I would like my code to look like the following (simplified example):

public class OddTest {
    @DataProvider(name = "excelLoader")
    public Iterator<Object[]> loadExcelData(String fileName) {
        ...
    }

    @Test(dataProvider = "excelLoader" dataProviderParameters = { "data.xls" })
    public void checkIsOddWorks(int num)
        assertTrue(isOdd(num));
    }
}
+1  A: 
yshua
+1  A: 

The answer from yshua is a bit limiting because you still have to hardcode the filepaths inside your data provider. This means you'd have to change the source code and then recompile to just rerun the test. This defeats the purpose of using XML files to configure the test run.

A better, definitely more hacky, kludge of a solution would be to create a dummy @test method that runs before suite, takes your filepaths as parameters and saves this information within the Class housing these test methods.

This solution isn't perfect, but until TestNG permits better parameter passing (Maybe this has changed) this might be viable for your needs.

esiegel
A: 

You can use your @BeforeClass method to initialize a value that is used by your DataProvider.