I have a class with multiple @Test methods each with a different functionality.
class myTests
{
@Test
test1()
{
...............
}
@Test
test2()
{
...............
}
}
Am following the Data driven approach where I have the data in Excel. Each row in excel sheet corresponds to a different test case that should execute the test methods in above class.
Below is the sample data from excel sheet which contains the method name as one of the parameters(eg: test1, test2) and also a flag which determines if the case should be picked up for execution or not at runtime (eg: y,n)
case1 data1 data2 data3........test1 y
case2 data4 data5 data6........test1 y
case3 data7 data8 data9........test2 y
case4 data10 data11 data12........test1 n
Below are the questions I have:
My understanding is that using DataProvider annotation, a test method can run with different input data. But am not sure how to map the test methods with corresponding test data if there are multiple test methods in single class.
Also I tried looking for IAnnotationTransformer that can be used to alter the runtime behaviour of test method, but could not find a way to send the flag data from excel to the transformer class.
Thanks in advance..
There are bits and pieces of how this can be done, which you can do, but its going to require a lot of customization from your side. There's nothing in TestNG that gives you this sort of capability out of the box.
IAnnotationTransformer
will not help you because, this listener only considers methods and is not aware of the instances. It also runs before anything runs and there's no way wherein you can have this iterate. So that can be ruled out.
Your best bet would be to use a @Factory
and a @DataProvider
wherein the @Factory
will produce ONLY one instance of your test class and all your Test methods will have to mandatorily be part of that same test class.
Here's the idea :
IHookable
interface, so that you can to decide which test method will run and which can be skipped.Here's a sample that shows some of this in action.
import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestResult;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TestclassSample implements IHookable {
private List<String> methodsToRun = new ArrayList<>();
@Factory(dataProvider = "dp")
public TestclassSample(List<String> methodsToRun) {
this.methodsToRun = methodsToRun;
}
@Override
public void run(IHookCallBack callBack, ITestResult testResult) {
String testMethodName = testResult.getMethod().getMethodName();
if (methodsToRun.contains(testMethodName)) {
System.err.println("About to run " + testResult.getMethod().getMethodName());
callBack.runTestMethod(testResult);
} else {
testResult.setStatus(ITestResult.SKIP);
}
}
@Test
public void testMethod() {
System.err.println("testMethod()");
}
@Test
public void anotherTestMethod() {
System.err.println("anotherTestMethod()");
}
@Test
public void thirdTestMethod() {
System.err.println("thirdTestMethod()");
}
@DataProvider(name = "dp")
public static Object[][] getData() {
return new Object[][]{
{Arrays.asList("testMethod", "thirdTestMethod")}
};
}
}
Here's the output :
About to run testMethod
testMethod()
About to run thirdTestMethod
thirdTestMethod()
Test ignored.
===============================================
Default Suite
Total tests run: 3, Failures: 0, Skips: 1
===============================================