[testng-users] how to pass parameters programatically

4,294 views
Skip to first unread message

printpreview

unread,
May 20, 2010, 11:48:05 AM5/20/10
to testng-users
Hi,

I've scoured the net for answers but cannot get just the right
explanation for the following...

I want to pass a parameter to a TestNG class that I'm running
programatically.

The parameter will be used in the TestNG class inside the @BeforeClass
annotated method.

How do you do this?

The (cut down version of) TestNG class is as follows
----------------------------------------------------------------------------------------
import org.testng.annotations.Test;
import org.testng.annotations.Parameters;

public class MyTestNGClass {
@Parameters({ "myParameterString" })
@BeforeClass
public void setupTests(String myParameterString) {

System.out.println(myParameterString);
}
-----------------------------------------------------------------------------------------


The code calling this class is as follows:
----------------------------------------------------------------------------------------
TestNG testng = new TestNG();
testng.setTestClasses( new Class[] {MyTestNGClass });
testng.run();
----------------------------------------------------------------------------------------

I've no idea where to put the parameter in the calling code.

I was hoping there would be some sort of method in TestNG along the
lines of

testng.setParam(String ParamName, String ParamValue)

any ideas?

One other point - the parameter must be avalible to the @BeforeClass
annotated method (the documentation refers to passing params to the
methods with @Test annotated method.

Thanks.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

Cédric Beust ♔

unread,
May 20, 2010, 12:53:21 PM5/20/10
to testng...@googlegroups.com
Why not use a @Factory to create an instance of your test with the parameters?

--
Cédric
--
Cédric

printpreview

unread,
May 20, 2010, 3:18:13 PM5/20/10
to testng-users
Thanks Cédric. That solves the problem of adding the parameters to a
method not marked as @Test.

Now for the other issue. How do I send the parameter programatically
when I run/instantiate the test class?

Many thanks.

On 20 May, 17:53, Cédric Beust ♔ <ced...@beust.com> wrote:
> Why not use a @Factory to create an instance of your test with the
> parameters?
>
> --
> Cédric
>
> > testng-users...@googlegroups.com<testng-users%2Bunsu...@googlegroups.com>
> > .

Cédric Beust ♔

unread,
May 20, 2010, 3:29:38 PM5/20/10
to testng...@googlegroups.com
The factory method instantiates the class, so you can pass parameters to the constructor, store these parameters in field and your method can then access it from there.

@DataProviders are another way of doing this (more flexible, if it works for you).

--
Cédric

printpreview

unread,
May 20, 2010, 3:54:38 PM5/20/10
to testng-users
Ok, I think I understand... so my code becomes...

----------------------------------------------------------------------------------------
import org.testng.annotations.Test;
import org.testng.annotations.Factory;
import org.testng.annotations.Beforeclass;

public class MyTestNGClass {
private String localParameterString;

@Factory
public void myConstructorMethod(String parameterString){
localParameterString = parameterString;
}

@BeforeClass
public void setupTests() {

System.out.println(localParameterString);
}

-----------------------------------------------------------------------------------------

The code calling this class is as follows:
----------------------------------------------------------------------------------------
TestNG testng = new TestNG();
testng.setTestClasses( new Class[] {MyTestNGClass("Im the
parameter") });
testng.run();
----------------------------------------------------------------------------------------

This doesn't compile though. I'm getting the message "The method
myTestClass(String) is undefined for the type xxx" (where xxx is the
class I'm calling it from).
So it seems as soon as I add the parameter to the bit of code that
instantiates the class then the IDE treats it as a method call.
Am I doing it in the right place? I would have thought that TestNG
instantiates the class anyway and me calling setTestClasses is just
adding a class name to an arraylist?

Thanks for your help Cédric so far!


On 20 May, 20:29, Cédric Beust ♔ <ced...@beust.com> wrote:
> The factory method instantiates the class, so you can pass parameters to the
> constructor, store these parameters in field and your method can then access
> it from there.
>
> @DataProviders are another way of doing this (more flexible, if it works for
> you).
>
> --
> Cédric
>
> > <testng-users%2Bunsu...@googlegroups.com<testng-users%252Buns...@googlegroups.com>

Udaya Kumar Anem

unread,
May 26, 2015, 12:37:00 AM5/26/15
to testng...@googlegroups.com
Cedric,

I am sailing in the same boat.

Can you tell me how can i do it with @DataProvider?

Where in the below code i need to add this parameter passing?

Here is my java class method:
public class invokeTestNG
{
    public static void main(String[] args) throws ClassNotFoundException
    {

        TestNG testng = new TestNG();
        String strClassName="testNg.firstTestNg";
        testng.setTestClasses(new Class[] { Class.forName(strClassName) });
        testng.run();
    }
   
    @DataProvider(name = "test1")
    public String retrunName()
    {
        return "Uday";
    }
}

Here is my @Test method in different class:
    @Test(dataProvider = "test1")
    public void printMsg(String strName)
    {
        System.out.println("Hello "+strName);

Krishnan Mahadevan

unread,
May 26, 2015, 2:41:25 AM5/26/15
to testng...@googlegroups.com
Uday,

Are you asking for something like this ?

public class DemoClass {
public static void main(String[] args) {
TestNG testNG = new TestNG();
testNG.setTestClasses(new Class[] { MyTestClass.class });
testNG.setVerbose(3);
testNG.run();

}

public static class MyTestClass implements ITest{
private String name;

@Factory(dataProvider = "dp")
public MyTestClass(String name) {
this.name = name;
}

@DataProvider(name = "dp")
public static Object[][] generateObjects() {
return new Object[][] { { "Cedric" }, { "Beust" } };
}

@Test
public void testMethod() {
Assert.assertNotNull(this.name);
}

@Override
public String getTestName() {
return name;
}
}
}

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 Scribbings @ http://rationaleemotions.wordpress.com/

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.

Shreejit Nair

unread,
Jun 22, 2016, 5:47:59 PM6/22/16
to testng-users, simong...@gmail.com
Hi,
Does anyone have a solution for this?
I am also looking to dynamically pass in a parameter to a TestNG class from outside the class itself not hardcoded within Dataproviders or anything set the field and consume it within the test methods etc


TestNG testng = new TestNG();
        testng.setTestClasses( new Class[] {TestNGTestSuite("someJsonString")});               
        testng.run();
        testng.getStatus();

Is not getting compiled for me as well

⇜Krishnan Mahadevan⇝

unread,
Jun 22, 2016, 11:49:27 PM6/22/16
to testng...@googlegroups.com
Maybe this sample should help you get started

public class MyTestNGClass {

public static void main(String[] args) {
        TestNG testng = new TestNG();
        XmlTest xmlTest = new XmlTest();
xmlTest.setName("Sample Test");
xmlTest.addParameter("myParameterString", "KungFu Panda");
xmlTest.setClasses(Arrays.asList(new XmlClass(LocalTestClass.class)));
XmlSuite xmlSuite = new XmlSuite();
xmlSuite.setName("Sample Suite");
xmlTest.setSuite(xmlSuite);
xmlSuite.setTests(Arrays.asList(xmlTest));
testng.setXmlSuites(Arrays.asList(xmlSuite));
testng.run();
}

public static class LocalTestClass {
private String myParameterString;
@Parameters ({"myParameterString"})

@BeforeClass
public void setupTests(String myParameterString) {
            this.myParameterString = myParameterString;
}

@Test
public void helloWorld() {
System.err.println("Hello there " + myParameterString);
}

}
}

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 Scribbings @ http://rationaleemotions.wordpress.com/

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.

Shreejit Nair

unread,
Jun 23, 2016, 10:10:47 AM6/23/16
to testng-users
Thanks Krishnan,
Can we also generate the default junit report xml from this execution.
Ideally the entire framework is being called from an external java program in this fashion.

Executionengine.executeFramework(){

//Create an instance on TestNG

     TestNG myTestNG = new TestNG();

     

    //Create an instance of XML Suite and assign a name for it.

     XmlSuite mySuite = new XmlSuite();

     mySuite.setName("ASQA Sample Suite");

     

    //Create an instance of XmlTest and assign a name for it.

     XmlTest myTest = new XmlTest(mySuite);

     myTest.setName("ASQA Sample Test");

     

    //Add any parameters that you want to set to the Test.

     myTest.addParameter("myParameterString", "value");

     

     

    //Create a list which can contain the classes that you want to run.

     List<XmlClass> myClasses = new ArrayList<XmlClass> ();

     myClasses.add(new XmlClass("com.ibm.testing.LocalTestClass"));

     

    //Assign that to the XmlTest Object created earlier.

     myTest.setXmlClasses(myClasses);

     

    //Create a list of XmlTests and add the Xmltest you created earlier to it.

     List<XmlTest> myTests = new ArrayList<XmlTest>();

     myTests.add(myTest);

     

    //add the list of tests to your Suite.

     mySuite.setTests(myTests);

     

    //Add the suite to the list of suites.

     List<XmlSuite> mySuites = new ArrayList<XmlSuite>();

     mySuites.add(mySuite);

     

    //Set the list of Suites to the testNG object you created earlier.

     myTestNG.setXmlSuites(mySuites);

     

    //invoke run() - this will run your class.

     myTestNG.run();


}

//Separate TestNG class
LocalTestClass{

@Test
// some test method

}

I would need TestNG to generate the junit report after being called this way.
Please let me know if that is possible and thanks a lot for the parameter passing approach

On Thursday, May 20, 2010 at 11:48:05 AM UTC-4, printpreview wrote:

⇜Krishnan Mahadevan⇝

unread,
Jun 23, 2016, 10:23:18 AM6/23/16
to testng...@googlegroups.com
I think that should be generated by default. You might want to quickly run and ascertain the same.

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 Scribbings @ http://rationaleemotions.wordpress.com/

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.

Shreejit Nair

unread,
Jun 23, 2016, 11:48:26 AM6/23/16
to testng-users, simong...@gmail.com
Hi Krishnan,

They are not generated by default unfortunately if we are using the API.
I need to attach a listener and build out a custom reporter. But, all i need really is to extract the Junit xml report that TestNG typically provides directly. So, i dont want to have an overhead of building out my own custom xml reporter that needs to be consumed across a CI pipeline. Also, the xml is being sent back as a webservice response. I already have a Java Object that maps to the Junit xml schema hence, looking to get the Junit format xml out.


On Thursday, May 20, 2010 at 11:48:05 AM UTC-4, printpreview wrote:

⇜Krishnan Mahadevan⇝

unread,
Jun 27, 2016, 1:39:52 AM6/27/16
to testng...@googlegroups.com
Then you should be able to add the JUnit Reporter via the TestNG API and then have it done. Since I have always resorted to relying on Surefire plugin to run my TestNG tests, I was under the assumption that the default reporters are added by default [ That's the default behavior with Maven surefire plugin ]

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 Scribbings @ http://rationaleemotions.wordpress.com/

To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.

Julien Herr

unread,
Jun 29, 2016, 11:26:00 AM6/29/16
to testng-users

Sudheer Kakaraparthi

unread,
May 7, 2018, 5:01:21 AM5/7/18
to testng-users
Excellent. this is what I was looking for.
Reply all
Reply to author
Forward
0 new messages