I'm struggling to understand the flow of execution of some Java TestNG code that my boss assigned to me. Unfortunately, the original author is no longer with my company, so I'm on my own. I've read through a few TestNG tutorials (especially this one) but still have questions.
Here is the code's test class, which learns about the tests it is to run from an external file, runs the tests, then closes everything up:
public class MyTestDriver {
public Object[][] data = null;
@BeforeSuite
public void beforeSuite() {
// open external info file
}
@DataProvider(name = "GetConfigData")
public Object[][] GetSyncConfigData() throws IOException {
try {
// Using external file, gather info about individual tests
// load that info into Object[][] data, I think
} catch (Exception e) {
// log errors
}
return data;
}
}
A test may be either an async'ed test or a sync'ed test, so those cases are handled by a subclass each. Here's the async'ed subclass:
public class AsyncAPITest extends MyTestDriver {
@Test(dataProvider = "GetConfigData")
public void asyncTestExecution(String RUN, String TYPE, String ENVIRONMENT, String TESTNAME) throws Exception {
try {
// run tests
} catch (Exception e) {
// log errors
}
}
}
Coders familiar with Java TestNG will spot the annotations.
Now, let's say I run the code and the external file specifies only one Async test should be run. In that event, I'm certain the code's order of execution would be:
@BeforeSuite
MyTestDriver.beforeSuite()
@DataProvider(name = "GetConfigData")
MyTestDriver.GetSyncConfigData()
@Test(dataProvider = "GetConfigData")
AsyncAPITest.asyncTestExecution()
But here's what I don't understand: How is information passed from MyTestDriver.GetSyncConfigData()
to AsyncAPITest.asyncTestExecution()
? If you look at method asyncTestExecution()
, that method actually takes in quite a few arguments:
public void asyncTestExecution(String RUN, String TYPE, String ENVIRONMENT, String TESTNAME) throws Exception
What is supplying those arguments? If I look through the code of MyTestDriver.GetSyncConfigData()
, shouldn't I see something like this somewhere:
// data initialized as Object[][]
// data = AsyncAPITest.asyncTestExecution(RUN, TYPE, ENVIRONMENT, TESTNAME);
return data;
I just don't understand how AsyncAPITest.asyncTestExecution()
is called, or what is supplying those arguments. I'm largely asking because I want to send in more arguments for later modifications. Thank you in advance for any suggestions or observations.
data has no significance. The arguments to your @Test method are being provided by @DataProvider. The return type of Dataprovider is Object[x][y]. x means sets of arguments. y means the arguments.
In your case, the dataprovider must be returning multiple values for a set of {String RUN, String TYPE, String ENVIRONMENT, String TESTNAME}. TestNG reads these sets and provides @Test, which would be run x times with each set of arguments.
How? If you are interested, you should read the implementation in testng code, but simply said, TestNG searches for annotations using the reflections API, creates multiple methods at runtime x times with y arguments and invokes them.
If you have to add an argument, then you need to add something to y - so you would need to make your dataprovider return it and your Test method accept it.