How to set up a flex unit test with ant and Hudson

I’ve done a lot of unit testing in java before, but none in actionscript and not really much on front-end code in general, so I thought I’d give it a try. Before I tried anything too tricky, I wanted to focus first on defining a simple test, running it through the IDE, and then automating it on a build server.

I’m working with Flex 4.1, Windows, and the FlashBuilder Eclipse plug-in. For automated builds, I’m using ant and Hudson. By default, FlashBuilder will include FlexUnit 4 libraries for you to use. Mine are version 4.0.1.277662, which is not too old. The FlexUnit site has version 4.1 release candidate if you want the latest and greatest.

My class for this example is Sample.as and it has one wee method-

package com.copperykeenclaws {
  public class Sample {
    public static function add(a:int, b:int):int {
      return a + b
    }
  }
}

Now, set up a place for your tests. You can put your tests wherever you want, but I tend to follow Maven conventions and put source code in src/main/flex and test code in src/test/flex. Create your test folder and add it to your flex build path so that you can run tests in your IDE. Create your test by clicking New->Test Case Class, then enter the name of your class (here, it’s SampleTestCase). Check Select class and browse to your class to test (Sample). Now add a test-

package com.copperykeenclaws {
  import org.flexunit.Assert;

  public class SampleTestCase {
    [Test(description="Test the add method")]
    public function add():void {
      var a:int = 1
      var b:int = 2
      var result:int = Sample.add(a, b)

      Assert.assertEquals(a + b, result)
    }
  }
}

At this point, you can right-click your project and select ‘Execute FlexUnit Tests’. Your tests will be run and the results displayed in the FlexUnit Results view. Take a look in your main source code folder (here, it’s src/main/flex) and refresh if necessary. There are two new files- FlexUnitApplication.mxml and FlexUnitCompilerApplication.mxml. Open up the first- it’s a test runner that was created for you behind the scenes.

Those are the basics for running FlexUnit from your IDE. Now, for an ant script. For this, I’m using the latest FlexUnit. Move the flexunit swcs and the flexUnitTasks jar into the libs folder. You’ll also need the regular flexTasks.jar if you don’t have it already .

A test suite can help manage your tests, though I don’t think it’s strictly necessary. Create one by selecting New->FlexUnitSuite named ‘SampleTestSuite’ and select the test you made.

You need a testrunner like the one Eclipse created. Here’s a simple one, saved at src/main/test/FlexUnitApplication.mxml:

I’m not going to cover using ant for flex in general. Here, I have a stripped-down build script for this example (note: there’s a good article on ant and FlexUnit at http://devzone.zend.com/article/12564). It builds the FlexUnitApplication.mxml into a swf then passes it as an argument to the flexunit task. I’m going to output test results to target/reports.

Try running it in your IDE and look for output in the reports folder- there should be an xml results file there. Assuming your project is already set up in Hudson to run an ant build script, the only configuration you have to do there is to check ‘Publish JUnit test result report’ and enter the path to your results xml files: ‘target/reports/*.xml’. Hudson will find them and set up the usual good unit test stuff- a Test Result Trend graph, a link to Test Results, etc.

If you see this error in your hudson console output:

[flexunit] Starting server …
[flexunit] Opening server socket on port [1024].
[flexunit] Waiting for client connection …
[flexunit]
[flexunit] Stopping server …
[flexunit] End of test data reached, sending acknowledgement to player …
[flexunit] Closing client connection …
[flexunit] Closing server on port [1024] …

You need to download the standalone FlashPlayer (also called the ‘Projector’ version) on your build server. It needs to be associated with the running of swf files (remember, we’re on windows), which should happen when you install it.

That’s it. Of course, FlexUnit’s most interesting capabilities involve asynchronous tests, and code coverage would be nice, but those will have to be later posts.

This entry was posted in flex, software and tagged . Bookmark the permalink.

4 Responses to How to set up a flex unit test with ant and Hudson

  1. Pingback: How to set up a flex unit test with ant and Hudson

  2. Yoonsook says:

    thank you so much :) .
    I fails the FlexUnit in CI server.
    I don’t know the reason . For 7days, I have been in trouble .
    I search the solution in your post.
    That’s flash player !
    I reinstall flash player (stand alone) ant get the good result :)

  3. Kamal says:

    Hi

    Thanks for the post ..I am able to run FlexUnit with ANT file from my local machine and it is now working in hudson .I am getting the same error that you mentioned . I installed Standalone FlashPlayer also . but still I am getting that error . Can you please help to find out a solution

    Thanks in Advance

    Regards,
    Kamal

    • tborthwick says:

      If your hudson installation is on Windows, make sure you’ve associated .swf files with your standalone flashplayer. Not sure outside of that.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>