Thursday, May 31, 2007

JUnit results in free form projects

At work, I'm working with a couple of colleagues on a project. Now, as is probably usual in many other environments, we all use different tools to work on the project: I use NetBeans, and the other two guys use Eclipse and Emacs. Thus, we have agreed that the Ant build scripts will be "the truth" : they have to be maintained and kept as the main tool for building, testing, and running the application.

So, all of that is great; however, I really like my NetBeans IDE, and I just couldn't continue living life without being able to use all of it's goodness. The best thing about NetBeans is that it is Ant based and it has the smarts/hooks to understand what you're trying to do (even in a freeform project), so that it can help you best. Here are the steps that I took to get my testing configuration going for a freeform project:

1. The first thing I did is to review the NetBeans Advanced Free Form Project Configuration

2. I added a compile selected item and debug project tasks (the debug task was quite useful since the code I was trying to understand was kinda convoluted and the debugger was invaluable in understanding how it works):

Here is what I added to my nbproject/project.xml (the ide-actions section):

<action name='debug'>
<script>nbproject/ide-file-targets.xml</script>
<target>debug-nb</target>
</action>
<action name='compile.single'>
<script>nbproject/ide-file-targets.xml</script>
<target>compile-selected-files-in-test</target>
<context>
<property>files</property>
<folder>test</folder>
<pattern>\.java$</pattern>
<format>relative-path</format>
<arity>
<separated-files>,</separated-files>
</arity>
</context>
</action>
<action name='test.single'>
<script>nbproject/ide-file-targets.xml</script>
<target>run-selected-files-in-test</target>
<context>
<property>classname</property>
<folder>test</folder>
<pattern>\.java$</pattern>
<format>java-name</format>
<arity>
<one-file-only></one-file-only>
</arity>
</context>
</action>



My ide-file-targets.xml additions look like this:

<target name='compile-selected-files-in-test'>
<fail unless='files'>Must set property 'files'</fail>
<mkdir dir='${test.classes.dir}'></mkdir>
<javac srcdir='test' source='1.6' includes='${files}' destdir='${test.classes.dir}'>
<classpath refid='run.test.class.path'></classpath>
</javac>
</target>
<target name='run-selected-files-in-test'>
<fail unless='classname'>Must set property 'files'</fail>
<mkdir dir='${test.classes.dir}'></mkdir>
<junit dir='${test.classes.dir}' printsummary='true' showoutput='true' fork='true'>

<classpath refid='run.test.class.path'></classpath>
<formatter type='brief' usefile='false'></formatter>
<formatter type='xml'></formatter>
<test name='${classname}'></test>
</junit>
</target>
<target name='debug-nb' depends='compile, compile-test'>
<path id='sourcepath'>
<pathelement path='src/'></pathelement>
<pathelement path='test/'></pathelement>
<pathelement path='..\\DeclTypeSys\\src'></pathelement>
<pathelement path='..\\DeclTypeSys\\test'></pathelement>
</path>
<nbjpdastart transport='dt_socket' name='perspective' addressproperty='jpda.address'>
<classpath refid='run.test.class.path'></classpath>
<sourcepath refid='sourcepath'></sourcepath>
</nbjpdastart>
<junit dir='${test.classes.dir}' showoutput='true' printsummary='yes' fork='true'>
<jvmarg value='-Xdebug'></jvmarg>
<jvmarg value='-Xnoagent'></jvmarg>
<jvmarg value='-Djava.compiler=none'></jvmarg>
<jvmarg value='-Xrunjdwp:transport=dt_socket,address=${jpda.address},suspend=y'></jvmarg>

<formatter type='xml'></formatter>
<formatter usefile='false' type='brief'></formatter>
<classpath refid='run.test.class.path'></classpath>
<batchtest>
<fileset dir='${basedir}/test'>

<include name='**/**/*Test.java'></include>
</fileset>
</batchtest>
</junit>
</target>

After I did that, my JUnit results look like this:











So, that was OK, but far from great. I thought that there should be a way to invoke the NetBeans JUnit test runner; however, googling around for it didn't help much. Then, just when I was about to lose hope, I ran upon these couple of posts:

Binding Freeform to Output and
UPortal Develop by Greg Sporar .

3.A slight complication on my end : I really didn't want to mess around with the target name of the original build.xml since the other team members were using that already. Thus, I added the following task to my ide-file-targets.xml:

<target name='test-project'>

<antcall target='junit'></antcall>
</target>

and changed the test single target name to "test-run-selected-files-in-test", so that the target name starts with "test" and changed the corresponding entry in my project.xml to run the right target.

And now, my test results look like this:








BEAUTY !!!

No comments:

Post a Comment