Home > All, Flash/Flex/AIR > Unit Test in Flash/Flex/AIR

Unit Test in Flash/Flex/AIR

I.Unit Testing:

In computer programming, unit testing is a software verification and validation method where the programmer gains confidence that individual units of source code are fit for use… Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended. – wikipedia

II.ASUnit for Actionscript Project:

1 – AsUnit: AsUnit is the open-source unit test framework for ActionScript that works with ActionScript 2, 2.5, 3.0, Flash Authoring (back to MX and up to CS4), Flex Builder, the Flex Framework (2 and 3), MTASC, MXMLC, and any version of Flash Player since version 6, including AIR and even FlashLite.

We can even emit JUnit-compatible XML results. There is no dependency on any external tooling or framework. It just works, and it works everywhere.

AsUnit was originally developed in 2004 by Luke Bayes and Ali Mills, but has evolved over the years thanks to many contributions from our vibrant community.

2-Get Starting: Download it http://github.com/lukebayes/asunit/zipball/release. And then import to Flex Builder

-Create fla for test runner: test_runner.fla

Import AsUnit to test_runner.fla’s classpath:

-Create a separator package for Unit Testing:

unittesting package

3Create Unit Test:

Sample code:

BankAccount.as

package

{

public class BankAccount

{

private var balance:Number=0;

public function deposit(amount:Number):void{

balance=balance+amount;

}

public function withdraw(amount:Number):void{

balance=balance-amount;

}

public function getBalance():Number{

return balance;

}

}

}

Create a class BankAccountTest for unit testing in unittesting package,  extends from asunit.framework.TestCase :

BankAccountTest.as

Package unittesting{

import asunit.framework.TestCase;

public class BankAccountTest extends TestCase {

private var instance:BankAccount;

/**

* Constructor

*/

public function BankAccountTest(testMethod:String) {

super(testMethod);

}

/**

* Set up instance of BankAccount to test with

*/

protected override function setUp():void {

instance = new BankAccount();

}

/**

* Delete instance used for testing

*/

protected override function tearDown():void {

instance=null;

}

/**

* Test BankAccount instantiation

*/

public function testInstantiated():void {

assertTrue(“Example instantiated“, instance is BankAccount);

}

/**

* Test Deposit

*/

public function testDeposit():void {

instance.deposit(50);

assertTrue(“Balance on a new account after 50 deposit is 50“, instance.getBalance() == 50);

instance.deposit(25);

assertEquals(“Balance after 50 deposit and another 25 deposit is 75“, 75,instance.getBalance());

}

/**

* Test withdraw

*/

public function testWithdraw():void {

instance.deposit(100);

instance.withdraw(50);

assertTrue(“Balance on a new account after 100 deposit and a 50 withdraw is 50“, instance.getBalance() == 50);

}

}

}

Others Method:

· assertEquals – test to see if one value equals another.

· assertFalse – test to see if value equals false.

· assertNotNull – test to see if value is not equal to null.

· assertNotUndefined – test if value is not undefined.

· assertNull – test if value is null.

· assertStrictlyEquals – test to see if two values strictly equal each other.

· assertTrue – test to see if value is true.

· assertUndefined – test to see if value is undefined.

3-Create Test Suite:

Create a Test Suite Class, extends from asunit.framework.TestSuite;

AllTests.as

package unittesting {

import asunit.framework.TestSuite;

public class AllTests extends TestSuite {

public function AllTests() {

super();

addTest(new BankAccountTest(“testInstantiated“));

addTest(new BankAccountTest(“testDeposit“));

addTest(new BankAccountTest(“testWithdraw“));

}

}

}

4-Running the test:

Open the test_runner.fla and linkage it to AsUnitTestRunner.as

Finally, run the test_runner.fla to check your unit testing.

Successful Test

Unsuccessful Test

III.FlexUnit for Flex Project:

1 – FlexUnit: FlexUnit is a unit testing framework for Flex and ActionScript 3.0 applications and libraries. It mimics the functionality of JUnit, a Java unit testing framework, and comes with a graphical test runner.

FlexUnit 4 is meta-data driven allowing you to have extreme freedom and ease when creating tests.

2-Get Starting:

-Download it http://www.digitalprimates.net/downloadit/FlexUnit4TurnkeyRC_1.0.zip

And then import to Flex Builder

-Create a separator package for Unit Testing:

unittesting package

3-Create Unit Test:

Sample code:

BankAccount.as

package

{

public class BankAccount

{

private var balance:Number=0;

public function deposit(amount:Number):void{

balance=balance+amount;

}

public function withdraw(amount:Number):void{

balance=balance-amount;

}

public function getBalance():Number{

return balance;

}

}

}

Create a class BankAccountTest for unit testing in unittesting package,  extends from flexunit.framework.TestCase

BankAccountTest.as

package unittesting{

import flexunit.framework.TestCase;

public class BankAccountTest extends TestCase {

/**

* Test Deposit

*/

public function testDeposit():void {

var bankAccount:BankAccount=new BankAccount();

bankAccount.deposit(50);

assertTrue(“Balance on a new account after 50 deposit is 50“, bankAccount.getBalance() == 50);

bankAccount.deposit(25);

assertEquals(“Balance after 50 deposit and another 25 deposit is 75“, 75,bankAccount.getBalance());

}

/**

* Test withdraw

*/

public function testWithdraw():void {

var bankAccount:BankAccount=new BankAccount();

bankAccount.deposit(100);

bankAccount.withdraw(50);

assertTrue(“Balance on a new account after 100 deposit and a 50 withdraw is 50“, bankAccount.getBalance() == 50);

}

}

}

Others Method:

· assertEquals – test to see if one value equals another.

· assertFalse – test to see if value equals false.

· assertNotNull – test to see if value is not equal to null.

· assertNotUndefined – test if value is not undefined.

· assertNull – test if value is null.

· assertStrictlyEquals – test to see if two values strictly equal each other.

· assertTrue – test to see if value is true.

· assertUndefined – test to see if value is undefined.

3-Create Test Suite and Test Runner:

Create a TestRunner.mxml:

<?xml version=”1.0” encoding=”utf-8“?>

<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” xmlns=”*

xmlns:flexunit=”flexunit.flexui.*

creationComplete=”onCreationComplete()“>

<![CDATA[

import flexunit.framework.TestSuite;

// Create the test suite and run the tests

private function onCreationComplete():void

{

testRunner.test = createSuite();

testRunner.startTest();

}

// Creates the test suite to run

private function createSuite():TestSuite {

var testSuite:TestSuite = new TestSuite();

testSuite.addTestSuite( BankAccountTest );

return testSuite;

}

]]>

<flexunit:TestRunnerBase id=”testRunnerwidth=”100%height=”100%” />

</mx:Application>

Finally, compile the TestRunner.mxml to check your unit testing.

Successful Test

Unsuccessful Test

Categories: All, Flash/Flex/AIR Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: