Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit & Ant : How to show detailed error message on the screen?

Tags:

java

junit

ant

I put up an Ant project which includes a unit test using JUnit.

The test target is as:

<target name="test">
    <mkdir dir="target/test/reports"/>
    <junit printsummary="yes" haltonfailure="yes">
        <classpath>
            <pathelement location="${test.classes.dir}"/>
            <pathelement location="${test.junit.jar}" />
            <pathelement path="${classes.dir}"/>
            <pathelement path="${java.class.path}"/>
        </classpath>
        <formatter type="plain"/>

        <batchtest fork="yes" todir="${test.reports.dir}">
            <fileset dir="${test.src.dir}">
                <include name="**/*Test*.java"/>
            </fileset>
        </batchtest>
    </junit>
</target>

When I run this test, it show on the screen only the summary of the test like:

Buildfile: F:\test\build.xml

test:
    [junit] Running com.mycompany.myproject.myTest
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.013 sec
    [junit] Running com.mycompany.myproject.myTest1
    [junit] Tests run: 3, Failures: 1, Errors: 0, Time elapsed: 0.018 sec

BUILD FAILED
F:\test\build.xml:30: Test com.mycompany.myproject.myTest1 failed

Total time: 1 second

Is there anyway I can tell JUnit or Ant to display the detailed result on the screen?

Also, if I want to write something in the Unit test to the screen, how can I do this? I tried to insert System.out.println() in the test but it does not display anything on the screen.

Many thanks.

like image 873
Kevin Avatar asked Mar 05 '12 17:03

Kevin


3 Answers

Set the showOutput flag to true.

What are you trying to accomplish via the S.o.p in the middle of a test?

like image 195
Dave Newton Avatar answered Oct 19 '22 08:10

Dave Newton


change printsummary value to withOutAndErr, that will cause JUnit to print System.out and System.err text

like image 2
Alex Avatar answered Oct 19 '22 07:10

Alex


IMHO, you are solving the wrong problem.

The junit results are collected and sitting "${test.reports.dir}" to be 'seen' by you. Ant has task that could help you in getting an HTML report

Introduce a target to generate html from the collected data (they are XML files)

<junitreport todir="./reports">
  <fileset dir="${test.reports.dir}">
    <include name="TEST-*.xml"/>
  </fileset>
  <report format="frames" todir="./report/html"/>
</junitreport>
like image 2
Jayan Avatar answered Oct 19 '22 08:10

Jayan