The following instructions outline how to set up an initial FlexUnit test application using the UIListener, CIListener, or Flash Builder itself.

Using a UIListener and TestRunner

(The following assumes you're at least using an Eclipse based IDE with Flex 3 plugin, or Flex Builder 3, Flex 4 plugin, or Flash Builder 4)

  • With your Flash Builder open, select File -> New -> Flex Project
  • Enter the name SampleFlexUnit4Tests for your project and click finish.
  • Download and add copies of the appropriate version of the FlexUnit swc files to your project's library path or libs folder (minimally you will need the main flexunit-.swc and the flexunit-uilistener-.swc if you are not using Flash Builder Professional)
  • Right click on your src folder select New -> Folder then enter "sampleSuite" as the folder name and click Finish.
  • Right click on your new "sampleSuite" folder select New -> Folder then enter "tests" as the folder name and click Finish.
  • In the sampleSuite/tests directory, right click, select New -> Actionscript Class named TestCase1. If you are using Flash Builder 4 you may instead select New -> Test Case Class. Leave the "Select class to test" blank for now.
    -The "tests" and "sampleSuite" folders are named simply for the tutorial steps to come, if you are setting up your own project feel free to name these whatever you like.
  • Add ActionScript code to TestCase1, see below for sample code:
    package sampleSuite.tests {
    	
    	import org.flexunit.Assert;
    	
    	public class TestCase1 {
    		private var count:int = 0;		
    		
    		[Before]
    		public function runBeforeEveryTest():void {   
    			count = 10;
    		}   
    		
    		[After]  
    		public function runAfterEveryTest():void {   
    			count = 0;  
    		} 
    		
    		[Test]  
    		public function subtraction():void { 
    			Assert.assertEquals(8, count-2);   
    		}
    	}
    }
    
  • In the sampleSuite directory, right click, select New -> Actionscript Class named SampleSuite. Alternatively, if you are using Flash Builder 4 you may select New -> Test Suite Class.
  • Add ActionScript code to SampleSuite, see below for sample code:
    package sampleSuite {
    	import sampleSuite.tests.TestCase1;
    	
    	[Suite]
    	[RunWith("org.flexunit.runners.Suite")]	
    	public class SampleSuite {
    		public var t1:TestCase1;
    	}
    }
    
  • Open up your SampleFlexUnitTests.mxml main application file
  • Add the tag <adobe:TestRunnerBase id="uiListener" /> into your application.
  • Next, add a script block with the following variable, function and imports.
<mx:Script>
	<![CDATA[
		import sampleSuite.SampleSuite;
		import org.flexunit.listeners.UIListener;
		import org.flexunit.runner.FlexUnitCore;
					
		private var core:FlexUnitCore;
		public function runMe():void {
			core = new FlexUnitCore();
			core.addListener( uiListener );
			core.run( sampleSuite.SampleSuite );
		}

	]>
</mx:Script>
<adobe:TestRunnerBase id="uiListener" width="100%" height="100%"  />

The core variable declared above is what will run your tests, you must add your TestRunnerBase as a listener if you want to output to your graphical test runner, seen on line 9 of the example. The reference to SampleSuite within the core.run is referring to a file created in the following parts of the tutorial, this can however be replaced with a reference to any other Suite, Test Case, Theory, or any combination of classes that you wish.

  • Finally, a bit of code must be added to your Application tag to import your TestRunnerBase and another to call to your runMe method after creationComplete() fires. Make sure your application looks like the following:
<mx:Application 
     xmlns:mx="http://www.adobe.com/2006/mxml"
     creationComplete="runMe()" 
     xmlns:adobe="http://www.adobe.com/2009/flexUnitUIRunner">

*You should then only need to run this application mxml to see your test run, provided the classes added have properly marked TestMethods inside them.

Using Flash Builder 4 Beta (version 257489)

  • With your Flash Builder open, select File -> New -> Flex Project
  • Enter the name SampleFlexUnit4Tests for your project
  • Make sure the SDK is set to use Flex 4.0 and click Finish.
  • Right click on your src folder select New -> Folder then enter "sampleSuite" as the folder name and click Finish.
  • Right click on your new "sampleSuite" folder select New -> Folder then enter "tests" as the folder name and click Finish.
  • In the sampleSuite/tests directory, right click, select New -> Test Case Class named TestCase1, be sure the FlexUnit4 radio button is selected and click Finish.

Note: By adding this Test Case class to the project Flash Builder will import the necessary FlexUnit 4 swcs so that we will not receive any compile errors while we are building out the tests and suites in the next steps.

Using the CIListener

Setting up the CIListener for continuous integration builds is similar to setting up the UIListener. The code below for runTests is pulled from the CI Sample Project and is similar to the UIListener runMe method example above. The project: CI Sample Project, can be built with Ant, Maven, and FlexMojos.

public function runTests() : void {
	var core : FlexUnitCore = new FlexUnitCore();
				
	/**If you don't need graphical test results, comment out the line below and the MXML declaring the TestRunnerBase. **/
	core.addListener(new UIListener(uiListener));
	core.addListener(new CIListener());
	
	/**If you would like to see text output in verbose mode, umcomment either of the follow listeners **/
	//core.addListener( new TraceListener() ); - For AS3 Projects
	//core.addListener( TextListener.getDefaultTextListener( LogEventLevel.DEBUG ) ); - For Flex Projects
				
        core.run(EchoPanelTest, SampleTest);
    }

When run, the CIListener will output XML to a predefined report directory for use in CI implementations. The output of the sample CIListener project is put into the target/report directory of the project once the Ant build is complete.

Previous | Next

  • No labels