Skip to content
admc edited this page Sep 14, 2010 · 2 revisions

Before jumping into this, you will want your application setup for flash testing by following the setup instructions: Flash Testing Setup

Writing ActionScript Tests

Tests are simple AS classes which extend `org.flex_pilot.TestCase`. Here’s an example:

package {
  import org.flex_pilot.TestCase;
  public class TestSnowDog extends TestCase {
    public var order:Array = ['testNeilPeart', 'testAssertEqualsString', 'testAssertEqualsNumber'];

    public function setup():void {
      // Do any required setup here
    }   
    public function testNeilPeart():void {
      asserts.assertDisplayObject({id: 'neilPeart'});
    }   
    public function testAssertEqualsString():void {
      var foo:String = 'geddy';
      asserts.assertEquals('geddy', geddy);
    }   
    public function testAssertEqualsNumber():void {
      var num:int = 2111;
      asserts.assertEquals(2112, num);
    }   
    public function teardown():void {
      // Do any needed teardown here
    }   
  }
}

FlexPilot Flash AS tests use the standard XUnit convention of prefacing all test functions with “test.” If there is a setup function in the class, it will be run first. If there is a teardown function, it will be run last.

Because Flash introspection sucks, it’s impossible to know what order methods are defined in a class, so there is no way to ensure the order any of the tests will execute. Because of this, you can include an optional `order` property — a simple array of string keys defining the order you want your test methods to run in.

Again, when you’re compiling your tests, you need to give the compiler a way to find the TestCase super class. Use the `-source-path` option to append a directory to look in for libraries, like so:

mxmlc -source-path=. -source-path+=../lib TestSnowDog.as -o TestSnowDog.swf

Example tests can be found in /test/flash_tests. They can be built using `./build.py -t tests`.

Asserts

The AS tests have access to an `asserts` property which contains the full range of XUnit-compatible asserts. Following is the list of provided asserts.

assertTrue
assertFalse
assertEquals
assertNotEquals
assertLessThan
assertMoreThan
assertNull
assertNotNull
assertUndefined
assertNotUndefined
assertNaN
assertNotNaN
assertEvaluatesToTrue
assertEvaluatesToFalse
assertContains

Waits

Waits allow you to insert arbitrary wait times, or to wait for a certain condition before proceeding forward in your code.

For arbitrary wait times, use `waits.sleep`:

public function testWaitSleep():void { 
  waits.sleep({milliseconds: 5000});
}

You can use `waits.forCondition` to test asynchronous processes. The `forCondition` wait will cause the test to idle until some desired condition is met, or the wait times out. `forCondition` takes as an argument a parameter object with a `test` property — this property is an ActionScript function which will return `true` when your desired condition is met.

Here’s an example of a simple conditional wait:

public function testWaitCondition():void { 
  var now:Date = new Date();
  var nowTime:Number = now.getTime();
  var thenTime:Number = nowTime + 5000; // Five seconds from now

  // Wait until the current date is greater than thenTime
  waits.forCondition({test: function ():Boolean { 
      var dt:Date = new Date();
      var dtTime:Number = dt.getTime();
      return (dtTime > thenTime);
  }});
}

Running AS Tests with the Flashmill Firefox Extension

The Flashmill Firefox extension is designed to make life easy by providing an interface to simply select your tests off the local file system and watch them run.

The addon uses the XPCOM and httpd.js to simulate a HTTP server from the extension, which handles cross domain security issues for you.

Steps
  • Simply install the addon, here on github
  • Navigate to Tools → Flashmill in Firefox
  • Load the page containing the Flash movie you want to test (needs to import FPBootstrap, see above)
  • Click the “Run Test File(s)” button
  • Select your files, and press OK
  • Watch your flash unit tests run!

If you have multiple embeds on the page that are FlexPilot enabled, use the “Target SWF” section to select the SWF on the page that you would like to run the tests against.

AS Tests API

Currently the only way of loading and running AS tests is by calling the fp_runASTests fuction exposed via ExternalInterface on the Flash movie to be tested. Pass this function an array of paths to your test SWFs. These are loaded dynamically via a Loader class, so again, these need to be served via a Web server — they cannot be local paths.

For example, if you had a Flash movie on your page with an id of `devMovie`, you could do the following:

devMovie.fp_runASTests(['/flash_tests/TestFoo.swf', '/flash_tests/TestBar.swf']);

RAW AS test output

Currently, test output is simply handed to FlexPilot Flash’s logger. The logger can be configured to send output either to the built-in `trace`, or to `console.log` for the Web page the movie lives in. The default is `trace`. Set it using `FPLogger.mode`, like so:

FPLogger.mode = FPLogger.modes.BROWSER;

Output will look something like this:

Running TestSnowDog.setup ...
SUCCESS
Running TestSnowDog.neilPeart ...
FAILURE: Error #1009: Cannot access a property or method of a null object reference.
Running TestSnowDog.testAssertEqualsString ...
SUCCESS
Running TestSnowDog.testAssertEqualsNumber ...
FAILURE: assertEquals -- expected <2112> (int) but was <2111> (int).
Running TestSnowDog.teardown ...
SUCCESS