Passing data to the @BeforeMethod at every iteration from the @Test method

33 views
Skip to first unread message

MCD dotCom

unread,
Feb 17, 2024, 10:49:41 PMFeb 17
to testng-users
Hello,

The @Test and @BeforeMethod annotations are in two different classes. Is there a way to pass data to the @BeforeMethod at every iteration from the @Test method when performing data-driven testing?


"TestData.xlsx"

Column Header:Ref No

Row 1
Column Value: DS01

Row 2
Column Value: DS02

public class AppTest extends BaseTest{

private static final String TEST_DATA_FILE = "TestData.xlsx";
private static final String SHEET_NAME = "Sheet1";

@DataProvider(name = "testData")
public Object[][] getWorkflowData() {

// Logic to fetch the data from the excel data file by passing TEST_DATA_FILE and SHEET_NAME
return dataArray;
}


@Test(dataProvider = "testData")
public void testWorkflow(Map<String, String> rowData) {

// Test logic goes here.
// I can retrieve the data from rowData.
// In addition, I also would like to retrieve the data in the BeforeMethod annotation.

}
}

public class BaseTest{


@BeforeMethod
public void beforeMethod(Method method) {

// During the first iteration, I would like to print out reference number DS01,
// and during the second iteration, I would like to print out DS02 reference number.
 

}
}

Thank you!





Krishnan Mahadevan

unread,
Feb 17, 2024, 11:36:30 PMFeb 17
to testng...@googlegroups.com
This should solve your use case (Remember to use TestNG v7.9.0 which is the latest release if you are on JDK11 and TestNG v7.5.1 if you are on JDK8)


import org.testng.ITestResult; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.reflect.Method; import java.lang.reflect.Parameter; import static java.lang.annotation.ElementType.PARAMETER; public class Sample { /** * A marker annotation which can be used to annotate the method parameters of a data driven test * to indicate that a particular parameter is to be regarded as a reference number which will be used * for special processing. */ @Retention(java.lang.annotation.RetentionPolicy.RUNTIME) @Target({PARAMETER}) public @interface Reference {} @BeforeMethod public void beforeMethod(ITestResult itr) { Method testMethodThatIsAboutToBeExecuted = itr.getMethod().getConstructorOrMethod().getMethod(); int position = -1; for (Parameter each : testMethodThatIsAboutToBeExecuted.getParameters()) { position += 1; if (each.isAnnotationPresent(Reference.class)) { break; } } if (position != -1) { Object reference = itr.getParameters()[position]; System.err.println("Reference : " + reference); } } @Test(dataProvider = "getData") public void testMethod(int number, @Reference String text) { System.err.printf("Test method received the parameters ( %d, %s) \n", number, text); } @DataProvider public Object[][] getData() { return new Object[][]{ {1, "one"}, {2, "two"} }; } }


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/009e997c-51a4-4277-92a9-dd8adb0a4bb8n%40googlegroups.com.

MCD dotCom

unread,
Feb 18, 2024, 3:44:49 PMFeb 18
to testng-users
Thank you! It helped. 
Reply all
Reply to author
Forward
0 new messages