Skip to content

Instantly share code, notes, and snippets.

@djangofan
Created June 4, 2012 00:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djangofan/2865537 to your computer and use it in GitHub Desktop.
Save djangofan/2865537 to your computer and use it in GitHub Desktop.
Dynamically load data into a DataProvider from a file using TestNG
@DataProvider(name = "DPMethodAlias") // Get A Form
public static Object[][] getFromFile() {
System.out.println("Loading data provider DPMethodAlias...");
// lastRun is a timestamp value used to find the .tests file
File tFile = new File( "test-output/work/" + WSUtils.getTestProperty("lastRun", "testng.config") + ".tests");
FileReader fileReader = null;
List<String> lines = new ArrayList<String>();
try {
String line = null;
fileReader = new FileReader( tFile );
BufferedReader bufferedReader = new BufferedReader( fileReader );
while ((line = bufferedReader.readLine()) != null ) {
lines.add(line);
}
bufferedReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int testSize = lines.size();
System.out.println("Test file has " + testSize + " tests contained within it." );
int i = 0;
Object[][] data = new Object[testSize][3];
for ( String lin : lines ) {
Object[] aTest = new Object[3];
StringTokenizer st = new StringTokenizer( lin, "=" );
aTest[0] = i;
String a = st.nextToken();
aTest[1] = a;
String b = st.nextToken();
aTest[2] = b;
System.out.println("Loaded test: " + i + "," + a + "," + b );
data[i] = aTest;
i++;
}
System.out.println( Arrays.deepToString(data) );
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment