Mark Kirby – Mobile Developer

ASUnit with FlashLite 2.x and 3.x

The tutorial explains a simple way to get started with ASUnit for unit testing your Flash Lite apps. There are many different ways of setting up ASUnit, but rather than be open and vague I’m going to prescribe a specific method which I feel separates your tests from the framework nicely, whilst being quick and easy to set up at the same time. Once you manage this, you’ll soon find your favourite method.

Please note – this method will also work with all Actionscript 2.0 Flash 8 apps.

1 – Download and install the ASUnit Framework

Download the latest version of the code.

Unpackage and locate the folder named ‘as25′.

Copy this folder ‘as25′ into the classpath (the root folder containing the code files) of your app.

Create another folder in the classpath called ‘tests’.

I’m assuming the classpath also contains all your components, classes etc, or if you have yet to code, its empty.

Here’s the contents of my root folder so you can follow my examples:

* AppName.fla (file)
* _classpath (folder, containing all my .as files)
** as25 (folder)
** tests (folder)
** other .as files

2 – Set up the ASUnitRunner app

2a – Copy the ASUnitRunner.fla and open it

Copy the ASUnitRunner.fla file to the same location as your apps fla file/s.

My folder now looks like this:

* AppName.fla (file)
* ASUnitRunner.fla (file)
* _classpath (folder, containing all my .as files)
** as25 (folder)
** tests (folder)
** other .as files

Open the newly moved ASUnitRunner file with your editor.

2b – Set the publish settings

Open publish settings and set the .swf to be published in your chosen location.

Alternatively you can create a folder called bin in the same folder the ASUnitRunner is sitting in, and it will automatically publish in there.

2c – Set the classpath

Open the Actionscript settings (in CS4 its File -> Publish Settings -> Flash -> Script settings button).

Remove both classpaths currently in place.

Set one class path pointing to the src folder within the as25 folder starting from the location of the fla e.g.

./path/to/as25/src

in my case:

./_classpath/as25/src

Set another class path pointing to the classpath root starting from the location of the fla e.g.

in my case:

./_classpath

2d – Set the action to the right folder

Open the first frame of the fla and change it to:

import tests.AsUnitRunner;
AsUnitRunner.main(this);

3 – Set up the tests folder

3a – copy test files

There are 2 more files to copy over into your tests folder.

Copy ‘as25/test/AllTests.as’ to ‘tests/AllTests.as’

Copy ‘as25/test/AsUnitRunner.as’ to ‘tests/AsUnitRunner.as’

3b – Add correct paths to AllTests.as

AllTests is set up to test ASUnit itself – so we will update it to test a sample file we will create.

Change tests/AllTests.as to:

class tests.components.AllTests extends asunit.framework.TestSuite {
        private var className:String = "AllTests";

        public function AllTests() {
                super();
                addTest(new tests.components.ExampleTest());
        }
}

3c – Add correct paths to AsUnitRunner.as

Change tests/AsUnitRunner.as to:

import asunit.textui.TestRunner;
import tests.AllTests;

class tests.AsUnitRunner extends TestRunner {

        public static function main(container:MovieClip) : Void {
                var tR = new AsUnitRunner();
        }

        public function AsUnitRunner() {
                start(AllTests);
        }
}

4 – Create sample file to test along with some tests

4a – Create components/Example.as

Create a folder called components at the root of your classpath, for me thats:

* AppName.fla (file)
* ASUnitRunner.fla (file)
* _classpath (folder, containing all my .as files)
** as25 (folder)
** tests (folder)
** components (folder)
** other .as files

Create a new file called Example.as and place it inside the components with the following code:

class components.Example {

        public function Example() {
        }

}

4b – Create tests/components/AllTests and ExampleTest

Create a folder within your tests folder called components, for me thats:

* AppName.fla (file)
* ASUnitRunner.fla (file)
* _classpath (folder, containing all my .as files)
** as25 (folder)
** tests (folder)
*** components (folder)
** components (folder)
** other .as files

Create the following two files:

AllTests.as

class tests.components.AllTests extends asunit.framework.TestSuite {
        private var className:String = "AllTests";

        public function AllTests() {
                super();
                addTest(new tests.components.ExampleTest());
        }
}

ExampleTest.as

import components.Example;
import asunit.framework.TestCase;

class tests.components.ExampleTest extends TestCase {
        private var className:String = "ExampleTest";
        private var instance:Example;

        public function ExampleTest(testMethod:String) {
                super(testMethod);
        }

        public function setUp():Void {
                instance = new Example();
        }

        public function tearDown():Void {
                delete instance;
        }

        public function testInstantiated():Void {
                assertTrue("Example instantiated", instance instanceof Example);
        }

        public function test():Void {
                assertTrue("failing test", false);
        }
}

5 – Run your tests

Now you are all set up, time to run those tests.

Simply publish and run AsUnitRunner.fla.

Expect to see a failure, the dummy test is set up expected to fail.

Now you can see how to set up ASUnit, you can expand Example.as and add some functions to test, and play around until you are ready to test your own functions. Advice on how to write tests can be found here. I hope to add another tutorial on this soon.

Leave a Reply

This site was created with Wordpress, using my own template.

Switch to our mobile site