How to run a single test using Ant

4,330 views
Skip to first unread message

Dinesh

unread,
Jan 9, 2011, 1:51:34 PM1/9/11
to testng-users
Hi,

I have many tests in my test suite but if I want to run a specific
test using Ant, how do I do it? I have seen that it is possible to do
so in Eclipse but would be interested in knowing how to do it using
Ant from the command line?

Any help and pointers would be very much appreciated.

Thanks,
Dinesh

vinay sambyal

unread,
Jan 9, 2011, 4:58:45 PM1/9/11
to testng...@googlegroups.com
try putting only one test in your TestNG.xml and then see. Not sure if that was not your question.

Cheers,
-Vinay
 

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


Mohamed Mansour

unread,
Jan 9, 2011, 5:18:11 PM1/9/11
to testng...@googlegroups.com
Hi Dinesh,

Well, you can do something like that as an ANT task. So if you want to do something like: 

ant runtest -Dtest-class=path.to.FooTest

Just make a target something like the following:

<target name="runtest" description="Run a single test">
    <fail unless="test-class" message="Property not set: 'test-class'"/>
    <echo message="                             -- Running single test --"/>
    <echo message="                                 -- ${test-class} --" />
    <testng classpathref="run.cp"
            outputdir="${testng.report.dir}">
      <classfileset includesfile="${test-class}"/>
      <jvmarg value="-Dtest.resources.dir=${test.resources.dir}" />
    </testng>
</target>

I haven't tested the above, but Cedric placed the ant documentation up on the site:


Kind regards,
Mohamed Mansour



Dinesh

unread,
Jan 10, 2011, 3:10:34 AM1/10/11
to testng-users
Hello Vinay,

Adding only test in the testng.xml will work but that is not my
requirement. I have multiples tests in one class and and would like to
run one or more tests in a class, from the command line. Mohamed got
my question right.

On Jan 10, 2:58 am, vinay sambyal <vinay.samb...@gmail.com> wrote:
> try putting only one test in your TestNG.xml and then see. Not sure if that
> was not your question.
>
> Cheers,
> -Vinay
>
>
>
>
>
>
>
> On Sun, Jan 9, 2011 at 6:51 PM, Dinesh <dinesh.ramasw...@gmail.com> wrote:
> > Hi,
>
> > I have many tests in my test suite but if I want to run a specific
> > test using Ant, how do I do it? I have seen that it is possible to do
> > so in Eclipse but would be interested in knowing how to do it using
> > Ant from the command line?
>
> > Any help and pointers would be very much appreciated.
>
> > Thanks,
> > Dinesh
>
> > --
> > 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<testng-users%2Bunsubscribe@google groups.com>
> > .

Dinesh

unread,
Jan 10, 2011, 4:16:11 AM1/10/11
to testng-users
Ideally, it should be run this way:
ant run -Dtestnames="test1"
where run --> ant target name and test1 --> name of a test method in a
class

The above mentioned approach is not working for me. Instead, all the
tests are getting executed.

Mohamed Mansour

unread,
Jan 10, 2011, 10:29:52 AM1/10/11
to testng...@googlegroups.com, Cédric Beust ♔
Hi Dinesh,

It is kind of strange, I might be totally incorrect.

<target name="runtest" depends="compile" description="Run a single test">
   <fail unless="testclass" message="Property not set: 'testclass'"/>
   <echo message="                             -- Running single test --"/>
   <echo message="                                 -- ${testclass} --" />
   <testng classpathref="run.cp" outputdir="test-reports">
     <jvmarg value="-Dtest.resources.dir=test-resources" />
    <classfileset includesfile="${testclass}" dir="." />
   </testng>
</target>

I was trying it out, and for some reason, when I specified <classfileset includesfile="${testclass}" dir="." /> it invoked something then spit out the command line saying:

 [testng] You need to specify at least one testng.xml, one class or one method
 [testng] Usage: <main class> [options]
 [testng]     -testclass                        The list of test classes

Then, I added that: 

<jvmarg line="-verbose -testclass com.foo.NewTest" />

But it spits out (verbose works):

[testng] Could not create the Java virtual machine.
[testng] Unrecognized option: -testclass

I am thinking that command line option might not be available anymore? Perhaps Cedric can answer, I was going to look at the source to see if its there (or how its defined), but when I get home today I will do so.

Kind regards,
Mohamed Mansour



To unsubscribe from this group, send email to testng-users...@googlegroups.com.

Dinesh Ramaswamy

unread,
Jan 10, 2011, 11:10:10 AM1/10/11
to testng...@googlegroups.com, Cédric Beust ♔
Thank you very much, Mohamed. Hoping to hear from Cedric soon.

Cédric Beust ♔

unread,
Jan 10, 2011, 1:01:40 PM1/10/11
to testng...@googlegroups.com
Hi Dinesh,

On Sun, Jan 9, 2011 at 10:51 AM, Dinesh <dinesh.r...@gmail.com> wrote:
Hi,

I have many tests in my test suite but if I want to run a specific
test using Ant, how do I do it?

Use the `methods` attribute of the ant task:

    <testng outputdir="${testng.report.dir}"

        classpathref="run.cp"

        useDefaultListeners="false"

        methods="test.sample.Sample2.method1,test.sample.Sample2.method3">


To be honest, I just realized that this functionality was not documented, neither in the main nor the ant section, so I just added it. I'm letting you know in case you decide to go back to read the doc, see it there and wonder how you missed it in the first place :-)

The corresponding command line flag is "-methods".

--
Cédric


Cédric Beust ♔

unread,
Jan 10, 2011, 1:04:42 PM1/10/11
to Mohamed Mansour, testng...@googlegroups.com
Hi Mohamed,

On Mon, Jan 10, 2011 at 7:29 AM, Mohamed Mansour <m0.inte...@gmail.com> wrote:
Hi Dinesh,

It is kind of strange, I might be totally incorrect.

<target name="runtest" depends="compile" description="Run a single test">
   <fail unless="testclass" message="Property not set: 'testclass'"/>
   <echo message="                             -- Running single test --"/>
   <echo message="                                 -- ${testclass} --" />
   <testng classpathref="run.cp" outputdir="test-reports">
     <jvmarg value="-Dtest.resources.dir=test-resources" />
    <classfileset includesfile="${testclass}" dir="." />
   </testng>
</target>

I was trying it out, and for some reason, when I specified <classfileset includesfile="${testclass}" dir="." /> it invoked something then spit out the command line saying:

 [testng] You need to specify at least one testng.xml, one class or one method
 [testng] Usage: <main class> [options]
 [testng]     -testclass                        The list of test classes

Maybe your classfileset didn't match any file?

If it did, run `ant -v` and paste the result here if you can't figure out what's going on.


Then, I added that: 

<jvmarg line="-verbose -testclass com.foo.NewTest" />

But it spits out (verbose works):

[testng] Could not create the Java virtual machine.
[testng] Unrecognized option: -testclass

That's expected since you are passing this line to <jvmarg> (basically, the java executable), which obviously doesn't know this option.

--
Cédric


Dinesh

unread,
Jan 10, 2011, 1:19:40 PM1/10/11
to testng-users
Thank you Cedric for letting me know that. Works fine now. :)

Mohamed, appreciate your help also very much.

--Dinesh

On Jan 10, 11:01 pm, Cédric Beust ♔ <ced...@beust.com> wrote:
> Hi Dinesh,
>
> On Sun, Jan 9, 2011 at 10:51 AM, Dinesh <dinesh.ramasw...@gmail.com> wrote:
> > Hi,
>
> > I have many tests in my test suite but if I want to run a specific
> > test using Ant, how do I do it?
>
> Use the `methods` attribute of the ant task:
>
>        <testng outputdir="${testng.report.dir}"
>
>         classpathref="run.cp"
>
>         useDefaultListeners="false"
>
>         *methods="test.sample.Sample2.method1,test.sample.Sample2.method3"*>
Reply all
Reply to author
Forward
Message has been deleted
0 new messages