How to pass the parameters from ant to testng.xml

3,069 views
Skip to first unread message

Magus

unread,
Aug 15, 2011, 4:49:25 AM8/15/11
to testng-users
Hi all
I did search some discussion on the group and found some answers, but
none of them work...

1. set the delegateCommandSystemProperties to true and add
<sysproperty key="basedir" value="${basedir}"/> under testng tag
does due to this bug? http://jira.opensymphony.com/browse/TESTNG-342

2. set jvm args value: <jvmarg value="-Djava.compiler=NONE" />


Is there any mistake in my config sample?
I read some older post and found someone once suggest adding a new
<parameter> tab inside <testng>, does it work with version 6.1?

Thank you.

Cédric Beust ♔

unread,
Aug 15, 2011, 12:48:27 PM8/15/11
to testng...@googlegroups.com
Are you using Eclipse? Ant? Maven? Command line?

-- 
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 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.


Magus Chen

unread,
Aug 15, 2011, 9:10:25 PM8/15/11
to testng...@googlegroups.com

Hi cedric
I am using hudson to perform an ant job. The ant job defined an testng task in build.xml and specifying some testng.xml to be contain.
I want to override some parameters under suite node in testng.xml. How can I achieve this?

Thank you.

Cédric Beust ♔

unread,
Aug 16, 2011, 5:06:59 PM8/16/11
to testng...@googlegroups.com
I just tried and it seems to work for me. I have:

  @Test
  @Parameters("foo")
  public void a1(String foo) {
    System.out.println("Context:" + foo); 
  }

I invoke TestNG with delegateCommandSystemProperties:

    <testng outputdir="${testng.report.dir}"
        classpathref="run.cp"
        useDefaultListeners="true"
     delegateCommandSystemProperties="true"
        outputDir="${testng.report.dir}">
      <xmlfileset dir="${test.resources.dir}" includes="testng-single.xml"/>
     </testng>

Running with:

ant -Dfoo=bar -f build-tests.xml run:single

and I get the result:

   [testng] Context:bar
   [testng] PASSED: a1("bar")

If I don't specify the system property with -D, I get:

   [testng] SKIPPED: a1
   [testng] org.testng.TestNGException: 
   [testng] Parameter 'foo' is required by @Test on method a1
   [testng] but has not been marked @Optional or defined in /Users/cbeust/java/testng/src/test/resources/testng-single.xml


-- 
Cédric

Magus

unread,
Aug 17, 2011, 3:57:10 AM8/17/11
to testng-users
Thanks Cédric, I got you idea.

I encounter another problem...

When a pass a parameter thru ant, the test that didn't use @Parameters
annotation cannot retrieve the parameter from ITestContext.

For example:

@Test
public void tobecall(ITestContext context){
String base_url =
context.getCurrentXmlTest().getParameter("base_url");
System.out.println("Context:" + base_url;
}

@Test
@Parameters("base_url")
public void parent(String base_url, ITestContext context){
System.out.println("Context:" + base_url; // happy
this.tobecall(context); // oops, cannot retrieve the right
base_url
}

ant -Dbase_url=http://www.google.com -f build-tests.xml
> > > On Mon, Aug 15, 2011 at 1:49 AM, Magus <magusc...@gmail.com> wrote:
>
> > >> Hi all
> > >> I did search some discussion on the group and found some answers, but
> > >> none of them work...
>
> > >> 1. set the delegateCommandSystemProperties to true and add
> > >> <sysproperty key="basedir" value="${basedir}"/> under testng tag
> > >> does due to this bug?http://jira.opensymphony.com/browse/TESTNG-342

Ramaraju Gelli

unread,
May 4, 2016, 2:50:08 AM5/4/16
to testng-users, ced...@beust.com

Hi Cédric,

I just tried the same example it worked fine. But if want to access System.getProperty("foo") inside the test method it is not accessible. Do we need to set the values else where to access foo as system variable?

Regards,
Ramaraju 

Krishnan Mahadevan

unread,
May 4, 2016, 4:00:17 AM5/4/16
to testng-users

Ram

The example Cedric shared basically leverages a JVM arg way of passing values. So you should definitely be able to access it via System.getProperty()

 

What are you seeing ? Can you please share more info around the code you used and error info if any.

 

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.

Reply all
Reply to author
Forward
0 new messages