Saturday, May 02, 2009

Scala and Ant JUnit Batchtest

When absorbing Scala into an existing large project at work, you would like to play it nicely with the build and tooling system (and engineers).
Many of the common testing tools out there are based on Ant, JUnit and some test manager, at LinkedIn we use Hudson (great tool by the way). Writing your tests in JUnit and Scala is not a problem, and you might want to check out some of the Scala testing specific libraries which plays nicely with JUnit.
A small problem you may encounter is with the JUnit task batchtest tag since we used it like this:

<attribute name="file-pattern" default="**/Test*.java, **/Test*.scala"/>
<junit fork="..." forkmode="..." dir="...">
...
<batchtest fork="..." todir="...">
...
<fileset dir="@{test-src-dir}" includes="${test.package.path}@{file-pattern}"/>
</batchtest>
</junit>
And it does not work for Scala sources (only for Java) because the tag "generates a test class name for each resource that ends in .java or .class.". Actually it might be just as well since unlike Java, Scala does not force classes names to match the file names they are declared in. The solution was to match against the classes in the build path since both Scala and Java compile into the same build directory into *.class files. Note we added the exclude pattern *$*.class that counts out inner classes.

<attribute name="file-pattern" default="**/Test*.class"/>
<attribute name="excludes" default="**/*$*.class"/>
<junit fork="..." forkmode="..." dir="...">
...
<batchtest fork="..." todir="...">
...
<fileset dir="@{test-build-dir}" includes="${test.package.path}@{file-pattern}" excludes="@{excludes}"/>
</batchtest>
</junit>

1 comments:

Eishay Smith May 4, 2009 at 12:11 AM  

Forgot to mention, we compile both Java and Scala sources to the same build directory destination.

Creative Commons License This work by Eishay Smith is licensed under a Creative Commons Attribution 3.0 Unported License.