Unit Testing with FAMES

Today on the MTASC list, erixtekila was kind enough to share with us his unofficial AsUnit framework ported to work with MTASC. Beyond providing a port of the excellent ASUnit that compiles under MTASC, he also told us how to make it work with FAME.

You see, there's this neat PHP plugin for Eclipse called PHPEclipse that comes with a web browser view. ASUnit's UI is a SWF that uses LocalConnection and so you can load it into the web browser to integrate it with Eclipse. This ingenious technique comes courtesy of Sonke Rohde who used it to integrate the Net Connection Debugger into Eclipse.

Here are the steps to use ASUnit with FAMES:

1. Install the PHPEclipse plugin (either using the update mechanism in Eclipse or by copying it to the plug-ins folder)

2. Open up the PHP Browser view (Window -> Show View -> Other -> PHPEclipse Web Development -> PHP Browser)

3. In the address bar of the PHP Browser, enter the path to the ASUnit UI (on my machine it's C:\Documents and Settings\Aral Balkan\Local Settings\Application Data\Macromedia\Flash MX 2004\en\Configuration\WindowSWF\AsUnit Ui.swf)

4. Copy the AsUnit source folder from erixtekila's download to your Flash classes folder (on my machine it's C:\Documents and Settings\Aral Balkan\Local Settings\Application Data\Macromedia\Flash MX 2004\en\Configuration\Classes\)

To create a simple test project:

1. Create a new ActionScript project in ASDT and call it AsUnit Test

2. Create a new xml (swfml) file and call it all_tests.xml. It's just a bare-bones, empty SWF:

XML:
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <movie width="320" height="240" framerate="30">
  3. <background color="#ffffff"/>
  4. </movie>

3. Compile it from the command line using

swfmill simple all_tests.xml alltests.swf

4. Create the following package (folder) structure: com / ariaware / asUnitTest. In the asUnitTest folder, create a new AS file called EmptyClass.as. The contents of this aptly-named class are shown below:

ActionScript:
  1. class com.ariaware.asUnitTest.EmptyClass
  2. {
  3. // Empty class
  4. }

5. Create a new class in the asUnitTest package called EmptyClassTest.as. This will contain the standard AsUnit unit tests (with one passing and one failing test.) The code is shown below:

ActionScript:
  1. import com.asunit.framework.*;
  2.  
  3. class com.ariaware.asUnitTest.EmptyClassTest extends TestCase
  4. {
  5. private var className:String = "com.ariaware.asUnitTest.EmptyClassTest";
  6. private var instance:Test;
  7.  
  8. public function setUp():Void
  9. {
  10. instance = new Test();
  11. }
  12.  
  13. public function tearDown():Void
  14. {
  15. delete instance;
  16. }
  17.  
  18. public function testInstantiated():Void
  19. {
  20. // This will always pass
  21. assertTrue("Test instantiated", instance instanceof Test);
  22. }
  23. public function test():Void
  24. {
  25. // This will always fail
  26. assertTrue("failingtest", false);
  27. }
  28. }

6. Create a new AS file in the root package (same folder as the all_tests.swf) called AllTests.as. It's contents are given below:

ActionScript:
  1. import com.asunit.framework.*;
  2. import com.ariaware.asUnitTest.*;
  3.  
  4. class AllTests extends TestSuite
  5. {
  6. private var className:String = "AllTests";
  7. static var allTests:AllTests = null;
  8.  
  9. public function AllTests()
  10. {
  11. super();
  12. addTest(new EmptyClassTest());
  13. // This causes warnings:
  14. // Sys.println("AllTests running");
  15. }
  16. static public function main ()
  17. {
  18. allTests = new AllTests();
  19. }
  20. }

Note: As shown in the code, I wasn't able to get Sys.println() working. It only causes warnings (not errors) but Flashout doesn't seem to compile when it encounters warnings. (Of course, you can always use the Flashout.log() method instead.)

7. Create a new flashout file called asunit.flashout and set your "Path to swf" to the alltests.swf you just created and for your "Root (main) class" browse to the AllTests.as file.

8. Compile your SWF using Flashout and you shold see the test results update in the PHP Browser/ASUnit view.

[Update] I haven't even finished writing this blog post and I just read an email from Luke Bayes sent to the Asunit-users list to announce the launch of ASUnit Version 2.6 with... guess what? MTASC support!

From the email: "This release should support the following development environments/compilers

- Flash MX 2004 Professional (export to Flash Player 6.0++) - MTASC / Eclipse (or any text editor) - FLEX

- Central Development using Flash MX Professional"

[Update 2] I just downloaded and played with the official release and everything works the same way. A couple of issues I ran into:

1. I tried installing the AsUnit MXP over the existing one. It apparently did not update all of the classes under com.asunit.*, so I was getting errors ("type error class not found : TextFile"). Downloading the source distribution and copying the classes to the Flash classes folder manually fixed this.

2. MTASC throws up a couple of "import not used" warnings. Normally, these wouldn't be anything to worry about but Flashout doesn't like them. The funny thing is, when you hit Compile, the Flashout Compilation Status screen shows the warnings and doesn't run the swf *but* if you then hit the Refresh button, the SWF *is* run. I noticed this by refreshing the ASUnit UI to show a green bar and 0 out of 0 assets passed and then hitting Compile (no change) followed by Refresh (failed test is shown.) I am assuming that this is a bug in Flashout.

Download the example project here (6kb)

Comments