Stanislav Zorjan - Stasha - Full Stack Software Engineer and Game Development Hobbyist - Prague


In this post I will show you very basic example how to use FlexUnit framework in both Flash Builder Basic and Professional.

First you'll have to download FlexUnit framework.
You can download it from: http://www.flexunit.org/?page_id=14.
(In this example I am using FlexUnit 4.1)

When you download FlexUnit, extract it and find libraries:

flexunit-4.1.0-8-flex_4.1.0.16076.swc
flexunit-uilistener-4.1.0-8-4.1.0.16076.swc
(you may have different versions) 

Create new flex project "App", create "application" package and move "App.mxml" to "application" package. Then copy two "FlexUnit" files to your project "libs" directory.

Inside "src" folder, create another package "applicationTest". This is main package for holding everything regarding FlexUnit. 

Now inside "applicationTest" package, create package "_suites_".  I named it "_suites_" just to be at the first place in "package explorer", but you are free to name it whatever you like. In "_suites_" package we are going to store "test suites" and test runner application. All other packages and files, except "_suites_" package, are going to reflect "application" package structure.

Now create "RunTests.mxml" application and "AppTestSuite.as" files and put them inside "applicationTest->_suites_" package.
"RunTests" application is main application for running tests, so whenever you need to run tests, you will run "RunTests" application.
"AppTestSuite" is collection (organization unit) of tests thus it will hold our simple "AppTest test".

Because our tests are reflecting "application" package structure, create "AppTest.as" class and put it inside "ApplicationTest" package.
"AppTest.as" is test class thus it should hold tests for testing all "units" methods presented in "App.mxml"  
You can name your test classes whatever you want, but if your test class has completely different name from class you are testing, than I hope you know what you are doing.

 

At this point your project structure should look like this:

 

 

Now, after successfull structure creation, here is source code for each file:

App.mxml:



	
	
		
	
	
	
		
	


 

RunTests.mxml:



	
	
		
	
	
	
	
	
	
		
	


 

AppTestSuite.as:

package applicationTest._suites_
{
	import applicationTest.AppTest;
	
	[Suite]
	[RunWith("org.flexunit.runners.Suite")]	
	public class AppTestSuite {
		
		public var appTest:AppTest;
		
		
	}
}

 

AppTest.as:

package applicationTest {
	
	import application.App;
	import flexunit.framework.Assert;
	import org.flexunit.async.Async;
	
	
	public class AppTest {
		
		public static var app:App;
		
		public function AppTest() { }
		
		
		[BeforeClass]
		public static function construct():void{
			app = new App();
		}
		
		[AfterClass]
		public static function destroy():void{
			app = null;
		}
		
		[Before]
		public function setUp():void {
			
		}
		
		[After]
		public function tearDown():void {
			
		}
		
		[Test]
		public function appCreated():void{
			Assert.assertFalse(app.testProperty);
			app.testProperty = true;
			Assert.assertTrue(app.testProperty);
			Assert.assertEquals(app.testProperty, true);
		}
	}
}

 

When you run "RunTests" application, you should see in your browser something like this:

 

 

and if tests fail, you will see:

 

 

Now that you know how to run simple test, you are ready to push it to a higher level.

You can learn more about FlexUnit at: http://docs.flexunit.org/index.php?title=Main_Page