25

I tried to write an ant with JUnit test, but get below result:

unittest:
    [junit] Running com.mytest.utiltest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] Test com.mytest.utiltest FAILED

it just shows error without print details, I specify below parameter in build.xml also tried to start with ant -v or ant -debug, but did not get any luck. Can anyone help?

<junit printsummary="yes" showoutput="true">

ant 1.8.2, sun jdk1.6.0_20, junit 4.8.2

to narrow down the problem, I created a seperate project this is my build.xml

<project name = "TestPrj" default="unittest" basedir = ".">

    <target name="unittest" >
        <junit printsummary="yes" showoutput="true" >
            <classpath>
                <pathelement location="./junit-4.8.2.jar"/>
                <pathelement location="./ant-junit4.jar"/>
            </classpath>
            <test name = "com.mytest.unittest.SimpleTest" todir="."/>
        </junit>
    </target>

</project>

below is simpletest.java

package com.mytest.unittest;

import junit.framework.TestCase;


public class SimpleTest extends TestCase{
    public void testFirst()
    {
        assertTrue(true);
    }

}

C:\TestPrj>ant

Buildfile: C:\TestPrj\build.xml

unittest:
    [junit] Running com.mytest.unittest.SimpleTest
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0 sec

BUILD SUCCESSFUL
Total time: 0 seconds

C:\TestPrj>dir

 Directory of C:\TestPrj

04/02/2011  02:00 PM    <DIR>          .
04/02/2011  02:00 PM    <DIR>          ..
04/02/2011  01:56 PM               280 .classpath
04/02/2011  01:54 PM               519 .project
04/02/2011  02:00 PM             7,120 ant-junit4.jar
04/02/2011  02:00 PM               403 build.xml
04/02/2011  01:55 PM    <DIR>          com
11/17/2010  05:36 PM           237,344 junit-4.8.2.jar
               5 File(s)        245,666 bytes
               3 Dir(s)  28,451,311,616 bytes free

Why there's not a JUnit results/detail/report generated? So that in my real case of failure, i can not troubleshoot my questions?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
hetaoblog
  • 1,990
  • 5
  • 26
  • 34

2 Answers2

42

You have to use a formatter element inside the junit task. The formatter will create a report file by default, but you can force it to print the results on screen. You can use two formatters: one for output to screen, another for output to file.

Note that you no longer need the attributes printsummary="yes" and showoutput="true" in the junit task. The formatter is taking care of output now.

<project name = "TestPrj" default="unittest" basedir = ".">

<target name="unittest" >
    <junit>
        <classpath>
            <pathelement location="./junit-4.8.2.jar"/>
            <pathelement location="./ant-junit4.jar"/>
        </classpath>
        <formatter type="plain" usefile="false" /> <!-- to screen -->
        <formatter type="plain" /> <!-- to file -->
        <test name = "com.mytest.unittest.SimpleTest" todir="."/>
    </junit>
</target>

</project>

Read the junit page in the ant manual for more information.

Lesmana
  • 25,663
  • 9
  • 82
  • 87
  • 2
    This works but it outputs the results to the screen once all the tests are done running. I would like to see them running as they go so that I can monitor progress. Any way that you know of to do that? – crowmagnumb May 29 '14 at 19:50
  • 1
    see here: http://stackoverflow.com/questions/18293054/immediate-junit-test-logging-with-junit-ant-task – Lesmana May 29 '14 at 21:30
1

Could you post the snippet which calls junit from build.xml here? There can be several reasons why the build fails. Also, please post the testcase which you are trying to test.

EDIT: Do you need a static for a test?

public class utilTest extends TestCase {
  public void testfun() {
    assertTrue(true);
  }
}

EDIT: Try using the argument outfile

<target name="unittest">
    <junit printsummary="yes" showoutput="true">
        <classpath>
            <pathelement location="./junit-4.8.2.jar"/>
            <pathelement location="./ant-junit4.jar"/>
        </classpath>

        <formatter type="plain" />

        <test name="com.mytest.unittest.SimpleTest" outfile="./testresult" />
    </junit>
    <fail message="test failed" if="test.failure" />
</target>
Shankar Raju
  • 4,356
  • 6
  • 33
  • 52
  • public class utilTest extends TestCase {public static void testfun(assertTrue(true);)} – hetaoblog Apr 02 '11 at 05:36
  • Please remove the static modifier and let me know if it works fine. – Shankar Raju Apr 02 '11 at 05:45
  • 1
    meanwhile, my question is more about: why isn't there a report in current folder? as from "test" of ant junit task, by default it should generate a report which is expected to see a report, but i did not see it.. – hetaoblog Apr 02 '11 at 05:51
  • Could you please post the entire contents of the build file? You could post it by editing your question. Unless I see the entire file, I cannot say why the build fails – Shankar Raju Apr 02 '11 at 05:52
  • since you have printsummary and showoutput set to true, you should be able to see the report in the folder. Please check the folder from where you are running the command. But, otherwise I cannot think of any reason why it should fail, probably I need more information on the contents of the build file. – Shankar Raju Apr 02 '11 at 06:03
  • thx, i've listed my detail there with build.xml/ant/dir, but SO's edit box seems to loose the format,especially the return (not WYSIWYG?) – hetaoblog Apr 02 '11 at 06:10
  • Thats okay, but you have not yet given me the code you have written in the build.xml file. I could see only the output generated by the ant task on the command line. – Shankar Raju Apr 02 '11 at 06:18
  • now i've formatted it there, and i've figured out, i should add it will generate xml; html report is available from junitreport – hetaoblog Apr 02 '11 at 06:42
  • well, the key thing is add and it has worked. thx for your really kind help:) i'm a bit surprised that SO users like u really like to help others in such a short time and continuous effort. amazing! – hetaoblog Apr 02 '11 at 07:12