First, make sure that the JUnit task as a showoutput="true" attribute and has a formatter that explicitly states that doesn't use a file, e.g.
<formatter usefile="false" type="brief"/>
Emphasis on not using a file : e.g. you can use an xml formatter and it would work; however, if you don't specify the usefile="false" attribute, everything goes to the file and NetBeans doesn't get a chance to capture the output and display the results in a nice JUnit test results tree. As a result, the best combination ends up being a combination of a xml formatter that outputs to a file, and a brief formatter that doesn't, e.g. :
<junit fork="yes" printsummary="withOutAndErr" showoutput="true" errorProperty="test.failed" failureProperty="test.failed" filtertrace="false">
<formatter type="xml">
<formatter usefile="false" type="brief" />
<classpath refid="whatever-path-id">
</classpath>
</junit>
The second important part of working with the JUnit results in NetBeans is to make sure that when a test fails, when you click on the failure in the JUnit results, you want NetBeans to take you to the right line in the source code:
In order to accomplish that, make sure that you properly set the output directory for your test cases in your NetBeans project with the UI or project.xml:
- In the Project Properties UI
- In the nbproject/project.xml
<java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/2">\n ....
<compilation-unit>
<package-root>test/integration</package-root>
<unit-tests/>
<classpath mode="compile">${test.completion.classpath}</classpath>
<built-to>dest/test/unit</built-to>
<source-level>1.5</source-level>
</compilation-unit>
....
</java-data>
....
Hi,
ReplyDeleteMy name is James Branam and I'm the NetBeans Community Docs Manager. Your blog entry would make a fantastic tips & tricks entry for our Community Docs wiki (http://wiki.netbeans.org//CommunityDocs). Would you be willing to contribute it? If you need any help or have any questions, please contact me at james.branam@sun.com. I look forward to hearing from you.
What is the best way to attach Netbeans to the JUnit test run so that you can debug the code that is causing a test to fail? I haven't yet found a way to do this in a free-form project--I'd like to associate this with the debug command. Currently I run the JUnit tests from a command line and the attach Netbeans to that JVM.
ReplyDeleteI've got the debug session working with the JUnit tests now. The thing I was missing was to import my main build.xml script so that I have access to its class path references in the ide-file-targets.xml script.
ReplyDelete