Get the @test method parameters in before methods

2,103 views
Skip to first unread message

Musaffir Lp

unread,
May 27, 2015, 12:07:35 AM5/27/15
to testng...@googlegroups.com
Hello,

I have many @test methods  which are getting parameters from a @dataProvider method .fine 
i have a single @before method too which executes before each @test method as expected . fine 

I can very well retrieve each @test method name during run time in my @before method using the java reflection - java.lang.reflect.Method 
by just passing this in my before method arg list , some thing like below
@BeforeMethod()
public void startUpBeforeTestMethod(Method m)
{
        String testName= m.getName(); 
        }

But I am looking for retrieving the parameters list of my @Test method in the before method 
can we get this 
I have tried the same Reflection logic as below :

@BeforeMethod()
public void startUpBeforeTestMethod(Method m)
{
String testName= m.getName(); 
        Method m2 =null;
        Class class1 = getClass(); //this gives my currently running testclass where i have all my test methods
        Method[] methods = class1.getMethods();
        for (Method method : methods) 
        {
if(method.getName().equalsIgnoreCase(testName))
{
m2=method;
break;
}
}
        Parameter[] parameters = m2.getParameters();
        Parameter parameter = parameters[0]; //there is only one parameter that dataprovider supplies
  }

but parameter variable do no hold any info about the actual parameters values that are provided by the dataProvider to the test method 
please shed some light if you knows better logic

Warm Regards
Musafir


Musaffir Lp

unread,
May 27, 2015, 12:14:39 AM5/27/15
to testng...@googlegroups.com

The root cause is clear :)
@Before method executes even before the dataProvider supplies the data to the @Test method :)

Musaffir Lp

unread,
May 27, 2015, 12:36:34 AM5/27/15
to testng...@googlegroups.com
The below statement is not correct
before the @before method starts executing , the testNg checks how many parameters that the @Test method requires and how many the corresponding dataProvider is supplying etc .. 
if there is a mismatch it gives TestNGException: 
so dataProvider is executing before the @before methods ...

Yet to get away to retrieve the data in @beforemethod :)

Musaffir Lp

unread,
May 27, 2015, 10:57:40 PM5/27/15
to testng...@googlegroups.com
I did it with some simple java coding 
used a public variable of type List in my dataProvider class and assigned the dataProvider multiple set of data to it [coz dp run only once for a particular test method and gets the multiple set of data that it needs to supply to the test method]
retrieved each value from the list variable in my before method  when before method executes each time for a test method .


On Wednesday, May 27, 2015 at 12:07:35 PM UTC+8, Musaffir Lp wrote:

Gurjit Singh

unread,
Feb 28, 2020, 6:32:45 AM2/28/20
to testng-users
Hi Musaffir, hope you are doing good. I am also trying to get parameter values in @BeforeMethod, is there any reference you may help me with ?

⇜Krishnan Mahadevan⇝

unread,
Mar 1, 2020, 11:31:22 PM3/1/20
to testng...@googlegroups.com
This should now be possible once https://github.com/cbeust/testng/issues/2255 gets fixed via PR https://github.com/cbeust/testng/pull/2256 and available in the next upcoming TestNG release (greater than 7.1.0)


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/27b6a98b-220b-4276-84ee-3d004d2e30b4%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages