Chase Brammer Thinking in Code

14Sep/090

A first date with Unit Testing in Flex 4

Unit testing is a topic that has creates a variety of opinions.  For a lot of people it is like a first date, the first time they try to take out unit testing for a spin it seems like it is fat, slow, and impedes what you want to get done.  For others, it is love at first sight.

To be honest, I still struggle to decide if I want to include unit uesting in my relationship with each new project I take on.  I know that it is needed, and that it really is best practice, but then I get thoughts like

  • "Is Unit Testing this really mission critical?"
  • "This is just a small project, it really doesn't need it"
  • "Crap, I don't like Test Driven Development"

Even though I murmur about the different times to use Unit Testing, as Flex and Flash move into become a bigger players in developing true business applications, the more we need to verify a applications functionality.

What is cool about Flash Builder 4 and unit testing, it that we finally have a unit testing solution that is actually built into the IDE.  It seems that the wars between FlexUnit, FlexMonkey, Structured Log Testing, and AsUnit was won by Flex Unit when they were crowned by Adobe to be integrated in the IDE.

If you need a refresher in what unit tests are, and why they are important, check this out.

So let's take a first date with unit tests in Flash Builder 4.

1) Create a project and create a simple class that will do some business logic that we want to test.  In this example the standard bank account example is used that makes deposits, and makes withdrawals.

package
{
public class Bank
{
public function Bank() { }
 
private var _balance:Number;
 
public function get balance():Number {
return _balance;
}
 
public function set balance(v:Number):void {
_balance = v;
}
 
/**
* Deposit some money into our bank
* @param ammount
* @return
*
*/
public function deposit(ammount:Number):Number {
_balance += ammount;
return balance;
}
 
/**
* Withdraw some money from our bank
* @param ammount
* @return
*
*/
public function withdraw(ammount:Number):Number {
_balance -= ammount;
return balance;
}
}
}

2) Now it is really easy to create a test case, just right click the class you want to test, and create a new test case off of it

newTestCase

This will name the class what it should be named, in our case BankTest.as and insert all of your stub functions to setup/tear down the test case.  You will also notice one extra file created in your directory, called flexUnitCompilerApplication.  This just exists to be the application that runs when you execute the test cases, it will also show you a nice pretty dialogue when all your tests run successfully.

3) Configure your test case.  Below is a sample.

package flexUnitTests
{
	import flexunit.framework.TestCase;
 
	public class BankTest extends TestCase
	{
		// please note that all test methods should start with 'test' and should be public
 
		// Reference declaration for class to test
		private var classToTestRef : Bank;
 
		public function BankTest(methodName:String=null)
		{
			super(methodName);
		}
 
		//This method will be called before every test function
		override public function setUp():void
		{
			super.setUp();
			classToTestRef = new Bank();
		}
 
		//This method will be called after every test function
		override public function tearDown():void
		{
			super.tearDown();
		}
 
		/* sample test method
		public function testSampleMethod():void
		{
			// Add your test logic here
			fail("Test method Not yet implemented");
		}
		*/
 
		/**
		 * Do a test deposit
		 *
		 */
		public function testDesposit():void {
 
			//Reset our balance to zero
			classToTestRef.balance = 0;
 
			//An ammount to deposit
			var ammount:Number = 1000;
 
			//Deposit the ammount
			classToTestRef.deposit(ammount)
 
			//Check to make sure that this deposit posts to the balance successfully
			assertEquals(ammount, ammount);
		}
 
		/**
		 * Do a test on a withdrawal
		 *
		 */
		public function testWithdrawal():void {
 
			//Reset our balance to zero
			classToTestRef.balance = 1000;
 
			//An ammount to withdraw
			var ammount:Number = 400;
 
			//What the end ammount of our withdrawal should be
			var endAmmount:Number = 600;
 
			//Withdraw the ammount
			classToTestRef.withdraw(ammount);
 
			//Check to make sure that this withdrawal posts to the balance successfully
			assertEquals(endAmmount, endAmmount);
		}
	}
}

Now just run your test!  See, it is that easy!

executeTests

If you are successfully you will see two screens where you can see the results of your tests.  The first, will be in the browser, but the most usefully one will be right inside you IDE where you can see what test executed successfully and which ones didnt!

ideTest

So there you go, that was a nice easy first date with unit tests in Flash Builder 4!

Download the source files.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.

Pages

Categories

Blogroll

Archive

Meta