how to pass parameters programatically

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

how to pass parameters programatically

printpreview
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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: how to pass parameters programatically

Cédric Beust ♔-2
Why not use a @Factory to create an instance of your test with the parameters?

--
Cédric


On Thu, May 20, 2010 at 8:48 AM, printpreview <[hidden email]> wrote:
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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
Cédric

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: how to pass parameters programatically

printpreview
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 ♔ <[hidden email]> wrote:

> Why not use a @Factory to create an instance of your test with the
> parameters?
>
> --
> Cédric
>
> On Thu, May 20, 2010 at 8:48 AM, printpreview <[hidden email]>wrote:
>
>
>
> > 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 [hidden email].
> > To unsubscribe from this group, send email to
> > [hidden email]<testng-users%[hidden email]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/testng-users?hl=en.
>
> --
> Cédric
>
> --
> You received this message because you are subscribed to the Google Groups "testng-users" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to [hidden email].
> For more options, visit this group athttp://groups.google.com/group/testng-users?hl=en.

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

Reply | Threaded
Open this post in threaded view
|

Re: Re: how to pass parameters programatically

Cédric Beust ♔-2
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


On Thu, May 20, 2010 at 12:18 PM, printpreview <[hidden email]> wrote:
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 ♔ <[hidden email]> wrote:
> Why not use a @Factory to create an instance of your test with the
> parameters?
>
> --
> Cédric
>
> On Thu, May 20, 2010 at 8:48 AM, printpreview <[hidden email]>wrote:
>
>
>
> > 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 [hidden email].
> > To unsubscribe from this group, send email to
> > [hidden email]<[hidden email]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/testng-users?hl=en.
>
> --
> Cédric
>
> --
> You received this message because you are subscribed to the Google Groups "testng-users" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to [hidden email].
> For more options, visit this group athttp://groups.google.com/group/testng-users?hl=en.

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




--
Cédric

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: how to pass parameters programatically

printpreview
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 ♔ <[hidden email]> 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
>
> On Thu, May 20, 2010 at 12:18 PM, printpreview <[hidden email]>wrote:
>
>
>
> > 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 ♔ <[hidden email]> wrote:
> > > Why not use a @Factory to create an instance of your test with the
> > > parameters?
>
> > > --
> > > Cédric
>
> > > On Thu, May 20, 2010 at 8:48 AM, printpreview <[hidden email]
> > >wrote:
>
> > > > 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 [hidden email].
> > > > To unsubscribe from this group, send email to
> > > > [hidden email]<testng-users%[hidden email]>
> > <testng-users%[hidden email]<testng-users%[hidden email]>
>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/testng-users?hl=en.
>
> > > --
> > > Cédric
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > "testng-users" group.
> > > To post to this group, send email to [hidden email].
> > > To unsubscribe from this group, send email to
> > [hidden email]<testng-users%[hidden email]>
> > .
> > > For more options, visit this group athttp://
> > groups.google.com/group/testng-users?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "testng-users" group.
> > To post to this group, send email to [hidden email].
> > To unsubscribe from this group, send email to
> > [hidden email]<testng-users%[hidden email]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/testng-users?hl=en.
>
> --
> Cédric
>
> --
> You received this message because you are subscribed to the Google Groups "testng-users" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to [hidden email].
> For more options, visit this group athttp://groups.google.com/group/testng-users?hl=en.

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

Reply | Threaded
Open this post in threaded view
|

Re: how to pass parameters programatically

Udaya Kumar Anem
In reply to this post by Cédric Beust ♔-2
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);
    }

On Friday, 21 May 2010 00:59:38 UTC+5:30, Cedric 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


On Thu, May 20, 2010 at 12:18 PM, printpreview <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="v2GVgYzP6VsJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">simong...@...> wrote:
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 ♔ <[hidden email]> wrote:
> Why not use a @Factory to create an instance of your test with the
> parameters?
>
> --
> Cédric
>
> On Thu, May 20, 2010 at 8:48 AM, printpreview <[hidden email]>wrote:
>
>
>
> > 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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="v2GVgYzP6VsJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">testng...@....
> > To unsubscribe from this group, send email to
> > <a href="javascript:" target="_blank" gdf-obfuscated-mailto="v2GVgYzP6VsJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">testng-users...@googlegroups.com<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="v2GVgYzP6VsJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">testng-users%2Bunsubscribe@...>
> > .
> > For more options, visit this group at
> ><a href="http://groups.google.com/group/testng-users?hl=en" target="_blank" rel="nofollow" onmousedown="this.href='http://groups.google.com/group/testng-users?hl\75en';return true;" onclick="this.href='http://groups.google.com/group/testng-users?hl\75en';return true;">http://groups.google.com/group/testng-users?hl=en.
>
> --
> Cédric
>
> --
> You received this message because you are subscribed to the Google Groups "testng-users" group.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="v2GVgYzP6VsJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">testng...@....
> To unsubscribe from this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="v2GVgYzP6VsJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">testng-users...@googlegroups.com.
> For more options, visit this group athttp://<a href="http://groups.google.com/group/testng-users?hl=en" target="_blank" rel="nofollow" onmousedown="this.href='http://groups.google.com/group/testng-users?hl\75en';return true;" onclick="this.href='http://groups.google.com/group/testng-users?hl\75en';return true;">groups.google.com/group/testng-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="v2GVgYzP6VsJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">testng...@....
To unsubscribe from this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="v2GVgYzP6VsJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">testng-users...@googlegroups.com.
For more options, visit this group at <a href="http://groups.google.com/group/testng-users?hl=en" target="_blank" rel="nofollow" onmousedown="this.href='http://groups.google.com/group/testng-users?hl\75en';return true;" onclick="this.href='http://groups.google.com/group/testng-users?hl\75en';return true;">http://groups.google.com/group/testng-users?hl=en.




--
Cédric

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="v2GVgYzP6VsJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">testng...@....
To unsubscribe from this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="v2GVgYzP6VsJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">testng-users...@googlegroups.com.
For more options, visit this group at <a href="http://groups.google.com/group/testng-users?hl=en" target="_blank" rel="nofollow" onmousedown="this.href='http://groups.google.com/group/testng-users?hl\75en';return true;" onclick="this.href='http://groups.google.com/group/testng-users?hl\75en';return true;">http://groups.google.com/group/testng-users?hl=en.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: how to pass parameters programatically

Krishnan Mahadevan
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/

On Tue, May 26, 2015 at 10:01 AM, Udaya Kumar Anem <[hidden email]> wrote:
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);
    }

On Friday, 21 May 2010 00:59:38 UTC+5:30, Cedric 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


On Thu, May 20, 2010 at 12:18 PM, printpreview <[hidden email]> wrote:
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 ♔ <[hidden email]> wrote:
> Why not use a @Factory to create an instance of your test with the
> parameters?
>
> --
> Cédric
>
> On Thu, May 20, 2010 at 8:48 AM, printpreview <[hidden email]>wrote:
>
>
>
> > 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 [hidden email].
> > To unsubscribe from this group, send email to
> > [hidden email]<testng-users%[hidden email]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/testng-users?hl=en.
>
> --
> Cédric
>
> --
> You received this message because you are subscribed to the Google Groups "testng-users" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to [hidden email].
> For more options, visit this group athttp://groups.google.com/group/testng-users?hl=en.

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




--
Cédric

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

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: how to pass parameters programatically

shreejitnair18
In reply to this post by printpreview
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

On Thursday, May 20, 2010 at 11:48:05 AM UTC-4, printpreview wrote:
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="jZntcr3i8JwJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@....
To unsubscribe from this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="jZntcr3i8JwJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users...@googlegroups.com.
For more options, visit this group at <a href="http://groups.google.com/group/testng-users?hl=en" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://groups.google.com/group/testng-users?hl\x3den&#39;;return true;" onclick="this.href=&#39;http://groups.google.com/group/testng-users?hl\x3den&#39;;return true;">http://groups.google.com/group/testng-users?hl=en.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: how to pass parameters programatically

Krishnan Mahadevan
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/

On Thu, Jun 23, 2016 at 3:17 AM, Shreejit Nair <[hidden email]> wrote:
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

On Thursday, May 20, 2010 at 11:48:05 AM UTC-4, printpreview wrote:
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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: how to pass parameters programatically

shreejitnair18
In reply to this post by printpreview
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:
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="jZntcr3i8JwJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@....
To unsubscribe from this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="jZntcr3i8JwJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users...@googlegroups.com.
For more options, visit this group at <a href="http://groups.google.com/group/testng-users?hl=en" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://groups.google.com/group/testng-users?hl\x3den&#39;;return true;" onclick="this.href=&#39;http://groups.google.com/group/testng-users?hl\x3den&#39;;return true;">http://groups.google.com/group/testng-users?hl=en.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: how to pass parameters programatically

Krishnan Mahadevan
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/

On Thu, Jun 23, 2016 at 7:40 PM, Shreejit Nair <[hidden email]> wrote:
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:
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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: how to pass parameters programatically

shreejitnair18
In reply to this post by printpreview
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:
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="jZntcr3i8JwJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@....
To unsubscribe from this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="jZntcr3i8JwJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users...@googlegroups.com.
For more options, visit this group at <a href="http://groups.google.com/group/testng-users?hl=en" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://groups.google.com/group/testng-users?hl\x3den&#39;;return true;" onclick="this.href=&#39;http://groups.google.com/group/testng-users?hl\x3den&#39;;return true;">http://groups.google.com/group/testng-users?hl=en.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: how to pass parameters programatically

Krishnan Mahadevan
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/

On Thu, Jun 23, 2016 at 9:18 PM, Shreejit Nair <[hidden email]> wrote:
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:
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 [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: how to pass parameters programatically

Julien Herr
Default listeners are supposed to be run by default: 

https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/TestNG.java#L190
https://github.com/cbeust/testng/blob/master/src/main/java/org/testng/TestNG.java#L916-L929

Le lundi 27 juin 2016 07:39:52 UTC+2, Krishnan a écrit :
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 @ <a href="http://wakened-cognition.blogspot.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;">http://wakened-cognition.blogspot.com/
My Technical Scribbings @ <a href="http://rationaleemotions.wordpress.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;">http://rationaleemotions.wordpress.com/

On Thu, Jun 23, 2016 at 9:18 PM, Shreejit Nair <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="4Ml82EQYCAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">shreeji...@...> wrote:
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:
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 [hidden email].
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at <a href="http://groups.google.com/group/testng-users?hl=en" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://groups.google.com/group/testng-users?hl\x3den&#39;;return true;" onclick="this.href=&#39;http://groups.google.com/group/testng-users?hl\x3den&#39;;return true;">http://groups.google.com/group/testng-users?hl=en.

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="4Ml82EQYCAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="4Ml82EQYCAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@....
Visit this group at <a href="https://groups.google.com/group/testng-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;">https://groups.google.com/group/testng-users.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: how to pass parameters programatically

Sudheer Kakaraparthi
In reply to this post by Krishnan Mahadevan
Excellent. this is what I was looking for.

On Wednesday, June 22, 2016 at 8:49:27 PM UTC-7, Krishnan Mahadevan wrote:
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 @ <a href="http://wakened-cognition.blogspot.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwakened-cognition.blogspot.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHzOdYJCASIDF_28vQtkp9gnKAYSQ&#39;;return true;">http://wakened-cognition.blogspot.com/
My Technical Scribbings @ <a href="http://rationaleemotions.wordpress.com/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Frationaleemotions.wordpress.com%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEcBOKyRn0lpL8LDbBuyAlKdwgXwQ&#39;;return true;">http://rationaleemotions.wordpress.com/

On Thu, Jun 23, 2016 at 3:17 AM, Shreejit Nair <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="JDakLuzXBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">shreeji...@...> wrote:
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

On Thursday, May 20, 2010 at 11:48:05 AM UTC-4, printpreview wrote:
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 [hidden email].
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at <a href="http://groups.google.com/group/testng-users?hl=en" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://groups.google.com/group/testng-users?hl\x3den&#39;;return true;" onclick="this.href=&#39;http://groups.google.com/group/testng-users?hl\x3den&#39;;return true;">http://groups.google.com/group/testng-users?hl=en.

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="JDakLuzXBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="JDakLuzXBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">testng...@....
Visit this group at <a href="https://groups.google.com/group/testng-users" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/testng-users&#39;;return true;">https://groups.google.com/group/testng-users.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.