Send parameter to a testng listener

3,939 views
Skip to first unread message

Phil

unread,
Oct 5, 2009, 5:26:57 PM10/5/09
to testng-users
My apologies if this has been discussed previously, I could not find
an answer in the forums.

I was wondering if it is possible to send a parameter from my
testng.xml to a custom listener. For example, I would like to do
something like this:

testng.xml:

<suite name="My Tests">
<parameter name="myParameter" value="100" />
<listeners>
<listener class-name="MyListener" />
</listeners>

<test name="Test1">
<classes>
<class name="myClass" />
</classes>
</test>
</suite>

MyListener.java:

...

@Parameters({"myParameter"})
public MyListener(@Optional Integer myParameter)
{
System.out.println(myParameter);
}



or...


@Parameters({"myParameter"})
public void onStart(ITestContext context, @Optional Integer
myParameter)
{
System.out.println(myParameter);
}



Obviously this isnt working for me, but basically I want to set a
parameter and have that parameter be available to my listener somehow
during test execution.

Any Ideas?

Thanks!
Phil

Cédric Beust ♔

unread,
Oct 5, 2009, 5:35:59 PM10/5/09
to testng...@googlegroups.com
Hi Phil,

As you already know, this is not supported right now.  It wouldn't be too hard to support something like:

       <listeners>
               <listener class-name="MyListener">
                 <parameter class="string" value="Foo" />
                 <parameter class="int value="42" />
               </listener>
       </listeners>

However, I'm always a bit worried when I see Java type information specified in XML.  First, because it's not statically type safe and second, because it's still very limited.  What if you want to pass non primitive parameters to your listener?

I would recommend using the programmatic API to achieve what you want (create your own TestNG object, create your listeners with the parameters, etc...) but I'm open to hearing arguments in favor of either approach.

What does everyone think?

--
Cédric

borojevic

unread,
Oct 6, 2009, 3:59:31 AM10/6/09
to testng-users
Hi all,
I agree with Cedric, but I am curious what Phil is trying to achieve?
Phil, can you please explain why you need this parameters passed to
listener?

Aleksandar

On Oct 5, 11:35 pm, Cédric Beust ♔ <cbe...@google.com> wrote:
> Hi Phil,
>
> As you already know, this is not supported right now.  It wouldn't be too
> hard to support something like:
>
>        <listeners>
>                <listener class-name="MyListener">
>                  <parameter class="string" value="Foo" />
>                  <parameter class="int value="42" />
>                </listener>
>        </listeners>
>
> However, I'm always a bit worried when I see Java type information specified
> in XML.  First, because it's not statically type safe and second, because
> it's still very limited.  What if you want to pass non primitive parameters
> to your listener?
>
> I would recommend using the programmatic API to achieve what you want
> (create your own TestNG object, create your listeners with the parameters,
> etc...) but I'm open to hearing arguments in favor of either approach.
>
> What does everyone think?
>
> --
> ***Cédric
> *

Phil

unread,
Oct 6, 2009, 11:40:06 AM10/6/09
to testng-users
Specifically, I am making a listener to analyze the amount of tests
that get skipped. In the tests that I am writing there are times when
I am unable to create the initial data to run some of my tests and if
that is the case I will throw a SkipException.

I would like my listener to keep track of the amount of tests that are
skipping and if a threshold is reached (either a percentage of the
tests, or a value is reached) I want to start marking the remainder of
the tests as failed rather than skipped. I am using Hudson as a
launcher for my tests and I would like it to report a failure if, for
example, 95% of the tests dont even run because they got skipped but
the tests that did run all passed.

The parameter I would like is this threshold. I would like to manage
the "acceptable" amount of skipping on a suite by suite basis rather
than having to hard code it into the listener.

If there are other ideas on how to accomplish this I'd love to hear
them.
Thanks,
Phil

borojevic

unread,
Oct 7, 2009, 5:01:20 AM10/7/09
to testng-users
Hi Phil,
I assume you are implementing ITestListener, if that is the case then
you have
method onStart(ITestContext context) before the test method starts
and accordingly you have onFinish method.
It accepts parameter ITestContext from which you can get ISuite and
therefore you can get parameter specified in suite.

Aleksandar

Phil

unread,
Oct 7, 2009, 4:35:35 PM10/7/09
to testng-users
Thanks Aleksandar, I was able to pull the parameter that way you
suggested.

Phil

Cédric Beust ♔

unread,
Oct 7, 2009, 4:46:13 PM10/7/09
to testng...@googlegroups.com
Very clever, Aleksandar.  I hadn't thought of that :-)

I always assumed these parameters were going to be used for tests but it never occurred to me that listeners can access them as well.

--
Cédric

Andrea Turli

unread,
May 2, 2012, 1:24:43 PM5/2/12
to testng...@googlegroups.com
Hi guys,

I've found this old post and potentially things are changed on testng 6.4

I need to sent parameters attached to my Test class to the listener that the test is using

Here my test class

@Listeners(MyListener.class)

@Test(testName = "MyTest")

public class MyTest {

   @Parameters({ "apiurl", "useratorg", "password", "catalogname", "vapptemplatename", "vappusr", "vapppwd", "jobid" })

   @BeforeClass

   public void setUpConnectionProperties(String apiUrl, String userAtOrg, String password, String catalogName, String vAppTemplateName, String vAppUsr, String vAppPwd, String jobId) { ...}
}


and MyListener is like:


public class MyListener extends TestListenerAdapter {

...


   @Override

   public void onStart(ITestContext testContext) {

      jobId = testContext.getSuite().getParameter("jobid");

      super.onStart(testContext);

   }

...

}


unfortunately the getParameter returns null.


Is there a better way to implement this scenario?

Thanks,

Andrea 

Cédric Beust ♔

unread,
May 2, 2012, 1:46:15 PM5/2/12
to testng...@googlegroups.com
getParameter() returns parameters defined in testng.xml, are you sure you defined that parameter there?

-- 
Cédric




--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/ve39WJ4fInEJ.
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.

Andrea Turli

unread,
May 2, 2012, 2:50:39 PM5/2/12
to testng...@googlegroups.com
No, I was using system property (like -Dapiurl+htp://myurl.com) to avoid testng.xml. Is it the same?

Andrea

-- 
Cédric




To unsubscribe from this group, send email to testng-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages