Question

My Ant junit task is currently set to output minimal information. On the console, for each test class, it prints the class name, how many tests were run, failed, and errored, along with the elapsed time for each test. If the test failed, there's an additional line saying the test failed.

I'd like to get additional detail on the console, but ONLY if a test fails. If I follow advice like this, by adding an additional plain formatter with usefile=false, then I get additional redundant detail for ALL tests, even if all tests pass. This includes printing each test method executed, and a redundant line for each test class.

Is it possible to get this?

Was it helpful?

Solution

I believe I've implemented a reasonable solution.

The key points of the solution are: * Executing a task after the unit tests are complete, which only runs if "test.failed" * Use the "xmltask" task library to parse the test results files and emit concise results

I wrote the following target to do this:

<target name="show-unit-test-failures" if="test.failed">
    <taskdef if:set="xmltask.present" name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
    <echo if:set="xmltask.present" message="Unit test failure report details:"/>
    <xmltask if:set="xmltask.present">
        <fileset dir="gen/reports/artifacts/junit">
            <include name="TEST-*Test.xml"/>
        </fileset>
        <call path="//testcase[./error]">
            <parThatam name="className" path="@classname"/>
            <param name="methodName" path="@name"/>
            <param name="errorText" path="./error/text()"/>
            <actions>
                <echo>---------------------------------</echo>
                <echo>@{className}.@{methodName}:</echo>
                <echo>@{errorText}</echo>
            </actions>
        </call>
        <call path="//testcase[./failure]">
            <param name="className" path="@classname"/>
            <param name="methodName" path="@name"/>
            <param name="errorText" path="./failure/text()"/>
            <actions>
                <echo>---------------------------------</echo>
                <echo>@{className}.@{methodName}:</echo>
                <echo>@{errorText}</echo>
            </actions>
        </call>
    </xmltask>
</target>

Note that I also let it "fail gracefully" if the "xmltask.jar" file isn't available. The ability to reference namespaces in tasks is a new feature in Ant 1.9.1 and newer.

I call this target at the end of my "run-unit-test" target.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top