ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan Matèrne (jhm) <apa...@materne.de>
Subject AW: How to run a single junit test method with ant
Date Tue, 14 May 2013 07:18:09 GMT
Here is a skeleton which could be what you need.	
  ant test -Dtestclass=xy -Dtestmethod=xy  :  runs the testmethod in that test class
  ant test -Dtestclass=xy                  :  runs all tests in that test class
  ant test                -Dtestmethod=xy  :  invalid as the class must be specified, so fail
the build
  ant test                                 :  runs all tests


Jan


<project>

    <target name="-test.check">	
        <!-- 
        decision matrix:
                testclass   testmethod
        suite      -             -
        class      X             -
        method     X             X
        error      -             X
        -->
        <condition property="testaction.suite">
            <and>
                <not><isset property="testclass"/></not>
                <not><isset property="testmethod"/></not>
            </and>    
        </condition>
        <condition property="testaction.class">
            <and>
                <isset property="testclass"/>
                <not><isset property="testmethod"/></not>
            </and>    
        </condition>
        <condition property="testaction.method">
            <and>
                <isset property="testclass"/>
                <isset property="testmethod"/>
            </and>    
        </condition>
        <condition property="testaction.error">
            <and>
                <not><isset property="testclass"/></not>
                <isset property="testmethod"/>
            </and>    
        </condition>
    </target>
    
    <target name="-test.suite" if="testaction.suite">
        <echo>Run JUnit using 'batchtest' and fileset</echo>
    </target>
    
    <target name="-test.class" if="testaction.class">
        <echo>Run JUnit using 'test name=$${testclass}'</echo>
    </target>
    
    <target name="-test.method" if="testaction.method">
        <echo>Run JUnit using 'test name=$${testclass} method=$${testmethod}'</echo>
    </target>
    
    <target name="-test.error" if="testaction.error">
        <fail>When specifying -Dtestmethod=${testmethod} you also have to specify the
property 'testclass'</fail>
    </target>
    
    
    <target name="test" depends="-test.check,-test.suite,-test.class,-test.method,-test.error"
description="Runs the tests"/>

</project>



> -----Ursprüngliche Nachricht-----
> Von: Antoine Levy Lambert [mailto:antoine@gmx.de]
> Gesendet: Sonntag, 12. Mai 2013 17:34
> An: Ant Users List
> Betreff: Re: How to run a single junit test method with ant
> 
> Hello Xiaomou,
> 
> the documentation of the junit task
> http://ant.apache.org/manual/Tasks/junit.html
> mentions that the test nested element can have a methods attribute to
> specify the methods you want to run
> 
> Comma-separated list of names of test case methods to execute. Since
> 1.8.2
> 
> <junit …>
>    <test name="com.something.XyzTest" methods="test1">
>    </test>
> </junit>
> 
> Regards,
> 
> Antoine
> On May 11, 2013, at 2:15 PM, 肖谋 wrote:
> 
> > Thanks for your reply. Perhaps I did not express my problem clearly,
> > or may not understand your solution.
> > My problem is : I have junit test class:
> > class TestService {
> >  @Test
> >   void test1() {}
> >  @Test
> >   void test2() {}
> > }
> >
> > now I'd like to only test the "test2" with ant command line like
> this:
> > ant -Dtest=TestService -Dtestcase=test2 test-target Then how to write
> > the build.xml?
> >
> > Thanks
> >
> >
> > XiaoMou
> >
> >
> >
> >
> >
> > On Sat, May 11, 2013 at 4:00 PM, Jan Matèrne (jhm)
> <apache@materne.de>
> > wrote:
> >
> >> Basic idea is
> >> - specify the test to run via command line property (-Dtestcase=...)
> >> - test-targets depends on two targets running a single test or the
> >> whole test suite
> >>   <target name="test" depends="-test.single,-test.all"/>
> >> - that two targets are conditional according to that property
> >>   <target name="-test.single" if="testcase">
> >>   <target name="-test.all" unless="testcase">
> >>
> >> Jan
> >>
> >>> -----Ursprüngliche Nachricht-----
> >>> Von: Matt Benson [mailto:gudnabrsam@gmail.com]
> >>> Gesendet: Freitag, 10. Mai 2013 16:55
> >>> An: Ant Users List
> >>> Betreff: Re: How to run a single junit test method with ant
> >>>
> >>> Typically this would be done using separate targets.  Ant's own
> >>> build.xml provides an example.
> >>>
> >>> HTH,
> >>> Matt
> >>>
> >>>
> >>> On Thu, May 9, 2013 at 11:33 PM, 肖谋 <xiaomou@gmail.com> wrote:
> >>>
> >>>> Hi, Users.
> >>>>   I am using ant in Linux. I can use ant junit task to run all
> test
> >>>> methods in a junit4 test class but fail to find the way to  run a
> >>>> single test method.  I googled and found a helpful article at
> >>>> https://today.java.net/pub/a/today/2003/09/12/individual-test-
> >>> cases.html.
> >>>> However, the solution it provides does not help. My ant seems not
> >>>> to recognize the "tests" sysproperty.
> >>>>  My ant version is 1.8.4. and the related content of build.xml is
> >>>> listed as follows:
> >>>>
> >>>>   <target name="ensure-test-name" unless="test">
> >>>>        <fail message="You must run this target with -
> >>> Dtest=TestName"/>
> >>>>    </target>
> >>>>
> >>>>    <target name="run-general-test" description="Runs the test you
> >>>> specify on the command
> >>>>        line with -Dtest=" depends="compile, ensure-test-name">
> >>>>        <junit printsummary="withOutAndErr" fork="true">
> >>>>            <sysproperty key="tests" value="${tests}"/>
> >>>>            <classpath refid="test.classpath" />
> >>>>            <formatter type="plain" usefile="false"/>
> >>>>            <batchtest>
> >>>>                <fileset dir="${test.src.dir}">
> >>>>                    <include name="**/${test}.java"/>
> >>>>                </fileset>
> >>>>            </batchtest>
> >>>>        </junit>
> >>>>    </target>
> >>>>
> >>>>  Please help! Thanks a lot!
> >>>>
> >>>> Regards
> >>>> xiaomou
> >>>>
> >>
> >>
> >> --------------------------------------------------------------------
> -
> >> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org For
> >> additional commands, e-mail: user-help@ant.apache.org
> >>
> >>



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message