Tuesday, January 5, 2010

FlexBuilder 3 and FlexUnit4 RC1

This will be a quick post on how to setup FlexBuilder project to use FlexUnit4. This post will be more prescriptive with just the steps without a lot of explanation. The FlexUnit site has good documentation.

1) Setup a Flex Project as you normally would.
2) Download the FlexUnit4 from this link.
3) Unzip and copy all of the swc files to your projects libs directory. There should be 4 swc files.
4) create a package/folder under the src directory called flexUnitTests. It could be called anything.
5) Create a test Suite class. This is just a POAO ( plain old actionscript object ). I called mine FlexUnit4Suite and it looked like the following:
1:  package flexUnitTests  
2: {
3: import org.flexunit.runners.Suite;
4: [Suite]
5: [RunWith("org.flexunit.runners.Suite")]
6: public class FlexUnit4Suite
7: {
8: public var testCase1:MyTestCase;
9: }
10: }


Lines 4 and 5 are the most interesting. Use the Suite and RunsWith metadata tags. Line 8 is a reference to your TestCase class. As you add more TestCase classes, just add a new variable here. You dont have to instantiate it, or anything - just declare it.

6) Create your TestCase class, which again is a POAO. Mine looked like the following:

1:  package flexUnitTests  
2: {
3: import flexunit.framework.Assert;
4: public class MyTestCase
5: {
6: public function MyTestCase()
7: {
8: }
9: [BeforeClass]
10: public static function runBeforeClass():void {
11: trace("runBeforeClass");
12: }
13: [AfterClass]
14: public static function runAfterClass():void {
15: trace("runAfterClass");
16: }
17: [Before]
18: public function runBeforeEveryTest():void
19: {
20: trace("runBeforeEveryTest");
21: }
22: [After]
23: public function runAfterEveryTest():void
24: {
25: trace("runAfterEveryTest");
26: }
27: [Test]
28: public function testTrue():void
29: {
30: trace("checkMethod");
31: Assert.assertTrue( true );
32: }
33: [Ignore("Not Ready to Run")]
34: [Test]
35: public function testNotReady():void
36: {
37: Assert.assertFalse( true );
38: }
39: }
40: }


7) Create a new Application for the test runner. You dont have to create a new application, but I assume you already have an Application class that really runs your application. I created another Application called FlexUnitTestRunner. It looked like the following:

1:  <?xml version="1.0" encoding="utf-8"?>  
2: <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
3: xmlns:flexunitrunning="http://www.adobe.com/2009/flexUnitUIRunner"
4: minWidth="1024" minHeight="768" creationComplete="onCreationComplete()">
5: <mx:Script>
6: <![CDATA[
7: import flexUnitTests.FlexUnit4Suite;
8: import org.flexunit.listeners.UIListener;
9: import org.flexunit.runner.FlexUnitCore;
10: private var flexUnitCore:FlexUnitCore;
11: private function onCreationComplete():void {
12: flexUnitCore = new FlexUnitCore();
13: flexUnitCore.addListener(new UIListener( testRunner));
14: flexUnitCore.run(FlexUnit4Suite);
15: }
16: ]]>
17: </mx:Script>
18: <flexunitrunning:TestRunnerBase id="testRunner" width="100%" height="100%" />
19: </mx:Application>



Run the FlexUnitTestRunner application and my console output looked like:
1:  runBeforeClass  
2: runBeforeEveryTest
3: checkMethod
4: runAfterEveryTest
5: runAfterClass


and a browser window popped up with the results of my test case.

Thats it in a nutshell how to get a Flex Builder 3, sdk 3.5, FlexUnit4 up and running quickly.

No comments: