June 13, 2010 - No Comments!

Unit Testing with Flash Builder and Flex Unit 4

Over the last couple of days I've had some time to look into a few features of Flash Builder, one of the exciting ones being the inclusion of unit testing. After having a quick look at online examples I was running into a few issues finding a complete example using Flex Unit 4 within Flash Builder, so here is a quick overview to get you up and running based on a mixture of examples I have been playing with.

We start by building a new flex project, >File >New >Flex Project. For this example I will just name it UnitTestDemo, keep the other settings as default (i.e. Application type: web, SDK:Flex 4), hit Finish. Now lets create a class we will perform our tests on. Here is my example class, named TestObject.

package
{
public class TestObject
{
public function TestObject()
{
}
public function returnNumberOne():int
{
return 1;
}
public function returnString(value:String):String
{
return value;
}
public function returnTrue():Boolean
{
return true;
}
}
}

This is just a simple class with a few example functions which we will run our tests on, ok... now for our test class. >File >New >Test Case Class. Create a name for your class, in this case I have named it as TestClass, Flash Builder will create folder to place these into named flexUnitTests.


You may also have noticed it is possible to select the class we wish to test, go and and select our TestObject class, hit Next.

Select the class we wish to test and the methods we wish to create tests for, in this case I have selected all of them. So now that we have our class created we need to edit the tests to provide the results we are looking for. By default although the tests are created they have no content to them to test, simply set up to return a fail.

Assert.fail("Test method Not yet implemented");

To implement our tests we first need to instantiate an instance of our class, we do this in the setUp method of our test class. It is also wise to remember to get this instance ready for garbage collection through the tearDown method. Before showing you my code, I will run through the tests themselves.

In our testReturnNumberOne we need to edit our code to test the method we have created. We are looking for a return value of 1 for our test to be a success. Using the assertEquals method of the Assert class we pass in 2 parameters, the 1st being our method and the 2nd being our expected result. Our code will look like this...

Assert.assertEquals(classReference.returnNumberOne(), 1);

I will save talking through the other examples as they follow the same path. Our complete test will look like this...

package flexUnitTests
{
import flexunit.framework.Assert;
public class TestClass
{
public var classReference: TestObject;
[Before]
public function setUp():void
{
classReference = new TestObject();
}
[After]
public function tearDown():void
{
classReference = null;
}
[BeforeClass]
public static function setUpBeforeClass():void
{
}
[AfterClass]
public static function tearDownAfterClass():void
{
}
[Test]
public function testReturnNumberOne():void
{
Assert.assertEquals(classReference.returnNumberOne(), 1);
}
[Test]
public function testReturnString():void
{
Assert.assertEquals(classReference.returnString("string"), "string");
}
[Test]
public function testReturnTrue():void
{
Assert.assertEquals(classReference.returnTrue(), true);
}
}
}

Now we now have this test case ready, we now need to create a suite to add it to. So >File >New >Test Suite Class. Create a suitable name for your suite and then select the test classes you wish to add, hit Finish. Looks like we're set, we have our class, our test class with all of our tests and our test suite which will run our test classes.

There are a number of ways I have discovered to run these tests. Here's a quick run through...

Run all of the unit tests within your project - Alt+Shift+A, F
Run the selected unit test - Alt+Shift+E, F
Project Context Menu >Run As >Flex Unit Tests

By running these tests we will be greeted by the output window, displaying the results of our test suite. As you'll see we have passed all of our tests, try editing the tests to give incorrect results. You can also view our results through the FlexUnit Results window (>Window >FlexUnit Results).

Extended reading - FlexUnit test environment

Published by: nick in AS3, Flash, Flex

Leave a Reply