Illidanek
Illidanek

Reputation: 1016

Generating reports for junit when test methods run individually

I have a Java project with JUnit tests which I run on Jenkins using Ant. Following various tutorials online, I made it possible for all the tests to run individually.

My build.xml looks like this:

<target name="run_junit" depends="compile, ensure-test-name"
                                  description="Run JUnit tests">
    <echo message="Execute Test" />
    <junit printsummary="withOutAndErr" fork="yes">
        <sysproperty key="tests" value="${tests}"/>
        <classpath>
            <path refid="classpath"/>
            <pathelement path="bin"/>
        </classpath>
      <formatter type="xml"/>
      <batchtest>
         <fileset dir="src">
             <include name="${test}.java"/>
         </fileset>
      </batchtest>
    </junit>
</target>

This, together with my own Runner and Filter classes, and @RunWith annotations, means that I can run an individual test within an individual class like this:

ant run_junit -Dtest=ClassWithTests -Dtests=testMethod

This works perfectly fine, in Jenkins as well, and I am also able to run the tests in parallel. The problem I have is that the reports for each class get overwritten after each method is run.

For example, the above command will produce a file TEST-ClassWithTests.xml with information about the result and output of testMethod. However, if I then run

ant run_junit -Dtest=ClassWithTests -Dtests=anotherTestMethod

the TEST-ClassWithTests.xml file will get overwritten and will only contain information about the result and output of anotherTestMethod.



So my question is: How do I make JUnit generate separate test reports for each method? And is there some way of nicely combining them so that they display well on Jenkins? I would ask whether it is possible to append methods to an existing JUnit report, but these tests are meant to be running in parallel, so that would probably not be helpful.

Upvotes: 1

Views: 1462

Answers (1)

Volker Stampa
Volker Stampa

Reputation: 849

You could use <test> instead of <batchtest> as this allows to specify a dedicated output-file. Like this:

  <test name="${test}" outfile="TEST-${test}-${tests}"/>

Since ant 1.8.2 this even allows you to run individual test methods through the attribute methods:

  <test name="${test}" outfile="TEST-${test}-${tests}" methods="${tests}"/>

This might help you getting rid of your custom runner.

Jenkins should be able to collect all report files properly and combine them into a single view.

Upvotes: 2

Related Questions