Viewing By Entry / Main
July 27, 2006
Paul Kenney will soon (hopefully) make available a new version of cfcUnit with ant integration so that you can automatically run your unit tests as part of your build process. He let me have a preview copy and it works really well.

Since a couple of folks have heard me talk about this and asked me to blog the 'how to' I figured that even in advance of Paul officially releasing this, I'd go ahead and explain what you can do with it and how easy it is to set it all up.

In the new release, cfcUnit adds a JAR file that defines a task in ant. To use it in a build.xml, you need to define the task:

<taskdef name="cfcUnit" classname="org.cfcunit.ant.CFCUnitTask" classpath="${cfcUnitLib}/ant-cfcunit.jar"/>
(where cfcUnitLib is a property in the ant file that specifies the directory in which the ant-cfcunit.jar file is deployed).

Then you can define a test case target like this:

<target name="test" depends="deploy">
   <cfcUnit hostname="${hostname}" testcase="com.adobe.hs.test.suite" verbose="false"/>
</target>
(where hostname is a property that defines the server name, port and context root for your test server, e.g., localhost:8300/cfmx).

Note how my test target depends on my deploy target so updated code is automatically deployed before the test suite is run.

Now I can just execute the test target and cfcUnit will run my test suite (in the example, the suite is the CFC /com/adobe/hs/test/suite.cfc).

That's the first step.

The real power is that I can now set up Eclipse to run the test suite automatically every time I change any file in my project!

In Eclipse, right-click on your project and select Properties. In the properties dialog, select Builders and then click New... and then select Ant Build. In the Main tab, select your build.xml file (by browsing your project workspace). Then in the Targets tab, choose Set Targets... for the Auto Build and pick which target to execute. Next - very important! - in the Classpath tab, click on User Entries and then Add External JARs... and navigate to your ant-cfcUnit.jar file. Finally, in the Build Options tab, enable Launch in background (and make sure Allocate Console is enabled too). OK, you're done.

Whenever you refresh the project, add a new file or save changes to an existing file, Eclipse will run the specified target in the build file. In my case, that auto-deploys any changed files to my localhost setup and then runs a verbose test suite. I get to see all the results in my Console window in Eclipse. Awesome!

Verbose test? Ah, I only showed the simple test suite target above. Here's the verbose one:

<target name="verboseTest" depends="deploy">
   <cfcUnit hostname="${hostname}" testcase="com.adobe.hs.test.suite"
      verbose="true"
      haltonfailure="true"
      haltonerror="true"
      showstacktrace="true"/>

</target>

My workflow is now: edit the test harness, add a new test case, save. Auto-deploy, auto-test, failure. Now edit the application code to add logic to pass the test case, save. Auto-deploy, auto-test, pass. Rinse and repeat.

And here's the sort of output you get from the verbose test:

Buildfile: /Users/scorfield/.../release/build.xml

deploy:
[copy] Copying 1 file to /www

verboseTest:
[cfcUnit] URL: http://localhost:8300/cfmx/cfcunit/XmlService.cfm? -testclassname=com.adobe.hs.test.suite
[cfcUnit] Running : com.adobe.hs.test.suite
[cfcUnit] Tests Run : 24
[cfcUnit] Failures : 0
[cfcUnit] Errors : 0
BUILD SUCCESSFUL
Total time: 3 seconds
Isn't that sweet?

Comments

That is sweet. Can't wait for the official release. I've been waiting for the ANT integration for a long time ;-)


Thanks Sean! We're starting to implement cfcUnit now that we've had some pitfalls in the road. Anything that helps automate the testing of over 100 CFCs (and counting) is very much appreciated.


I have also setup a very similar automated Ant-based unit test process in Eclipse using CFUnit and CFUnit-Ant. I just recently discovered CFUnit and CFCUnit, so I'm not really sure what the differences between the two are. I just wanted to let everyone know that it is also possible to accomplish the same thing with CFUnit as well.


Justin, I've previously blogged about using cfUnit with ant but the cfUnit codebase is not as idiomatic for ColdFusion as cfcUnit (because cfUnit is pretty much a direct port of jUnit whereas cfcUnit was written from the ground up for ColdFusion - for example, in cfUnit, the optional message argument in the various assert*() methods is first but that forces you to used named arguments for expected and actual whereas in cfcUnit, the optional message argument is third so it can be omitted without requiring named arguments).

The only reason I used cfUnit was the ant integration. Now that cfcUnit has ant integration, I've dumped cfUnit completely.


Does anyone know if the new version of CFCUnit with ANT integration was ever released? The CFCUnit project page doesn't say anything about it that I can see.


Paul will be working on this shortly. It's just a matter of packaging and updating the cfcUnit site.


Sean - any chance you can post the full build.xml file?


Bob, a sample build.xml file will be posted with the ant task when cfcUnit is updated, I expect.


Post Your Comments
Name:
Email Address:
Comments
*** Please note that all comments require moderation so it may be some time before your comment posts to this blog! ***
Remember My Information:
 



Hosting provided by