Created
June 4, 2012 00:09
-
-
Save djangofan/2865537 to your computer and use it in GitHub Desktop.
Dynamically load data into a DataProvider from a file using TestNG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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