access the parameter names of a data-driven test

142 views
Skip to first unread message

sergio...@gmail.com

unread,
Jan 11, 2023, 11:49:09 AM1/11/23
to testng-users
Hi all,
I'm trying to enhance the integration with Xray test management. I wanted to obtain the parameters on a report listener.
it seems that the ITestResult based result provides access to the parameter values.. using result.getParameters() . 
But I don't see a way to obtain the parameter names.. Is there a way? Can you give me some pointers in terms of API please?

My ultimate goal would be to have an array of hashes, by parameter name, or some method that returns the parameter names in the same order as the values that are provided by .getParameters() on the ITestResult.

Thanks,
Sérgio Freire

⇜Krishnan Mahadevan⇝

unread,
Jan 11, 2023, 10:05:31 PM1/11/23
to testng...@googlegroups.com
You can try one of the following:
  1. testResult.getTestClass().getXmlClass().getAllParameters()
  2. Parameters params = testResult.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Parameters.class);
I am assuming that here, you are trying to retrieve the values for the parameters that represent the parameters provided via the "@Parameters" annotation.

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/99d6ca00-0205-46f5-a839-5327062344d0n%40googlegroups.com.

sergio...@gmail.com

unread,
Jan 12, 2023, 5:08:26 AM1/12/23
to testng-users
Hi Krishnan,
well, let me try to reformulate.
Parameter values may come from @Parameters annotation or from @DataProvider. Those will provide values for the parameters .
I would like to know the names of the parameters.
So let me try to showcase that with an example; let's consider 2 tests as follows.. in my perspective, the names for the parameters would be "v1" and "v2" for the test "parameterAnnotatedTest()" and "n1" and "n2" for the dataProviderTest(). For the parameterAnnotatedTest() I think we could also consider the names coming from the @Parameters... it's discussable but I think an approach where the names of parameters are always derived from the name of the arguments of the test method makes probably more sense.

    @Test
    @Parameters ({"val1", "val2"})
    public void parameterAnnotatedTest(int v1, int v2) {
        int finalsum = v1 + v2;
        System.out.println("The final sum of the given values is " + finalsum);
    }

    @DataProvider(name = "test1")
    public Object[][] createData1() {
        return new Object[][] {
            { "Cedric", new Integer(36) },
            { "Anne", new Integer(37)},
        };
    }
  
    //This test method declares that its data should be supplied by the Data Provider
    //named "test1"
    @Test(dataProvider = "test1")
    public void dataProviderTest(String n1, Integer n2) {
        System.out.println(n1 + " " + n2);
    }
 

⇜Krishnan Mahadevan⇝

unread,
Jan 12, 2023, 5:21:54 AM1/12/23
to testng...@googlegroups.com
Sergio,

TestNG currently does not know/remember the variable names that are associated with a data driven test powered by a data provider. I don't think you could use reflection also to determine this information without you specifying some sort of metadata that helps identify a variable by its name (Similar to how spring lets you associate a name with a bean via the "Qualifier" annotation)

So for the use cases that involve "@Parameters" you can use the approach I cited earlier.

Now for data driven tests, you would have to do the following:

1. Alter each of your data driven tests and start adding a custom annotation (lets call it "@Qualifier") via which you can cite a name for the data member.
2. Now within your reporter listener, you already have access to the actual parameter values. 
3. To get the variable names, you can always query the underlying method via iTestResult.getTestMethod().getConstructorOrMethod().getMethod() and extract out its parameter types and for each of the parameters, iterate through them and read their annotations and look for "@Qualifier" which would give you the parameter names.
4. You club (2) and (3) and you have your hashmap of key/value pairs available.




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/

sergio...@gmail.com

unread,
Jan 12, 2023, 5:38:34 AM1/12/23
to testng-users
Thanks Krishnan,
I will now do some work  / evaluation from my side.
Regards,
Sérgio

Reply all
Reply to author
Forward
0 new messages