
Intro:
FlexUnit is an open source framework created by Adobe for unit testing in Flex. It is based on the widely used JUnit testing framework for Java. FlexUnit provides a low level automated testing system that is your first line of defense for catching bugs in your application.
What is Unit testing?
To understand unit testing, you must first understand what a “unit” is. A unit is the smallest piece of code that is testable. This doesn’t mean each line of code, but a piece of code that performs a specific task. In Flex, this means a function, or more appropriately a method since Flex/ActionScript is an object oriented language.
Steps to build FlexUnit Sample:
- Open the new project in Flex Builder (LoginSample.mxml).
- Write the sample code (login functionality - Components in separate mxml file & method should be in script file).
- Copy the FlexUnit.swc to lib folder.
- Create a new mxml application file (in the same project folder -loginTestSuite.mxml).
- Create a new script file to write a test case for unit testing.
- Run the loginTestSuite.mxml for unit testing, If any errors the UI listed the line number.
Create a Test Case :
Open the script file LoginAccountTest.as and Our unit tests will be contained in a new class file (LoginAccountTest.as) that extends the base TestCase class. This class will contain methods with our unit tests in them. Typically need to create a new method for each unit . In this case we testing the login methods of the user. so my test class method names will be testLoginComponent(). The method names for our test methods must start with the word test. Later on when we add the unit tests to the test suite it automatically looks for method names that begin with the word test. If your method names do not begin with the word test they will not be run.
Ex:
- testLoginComponent()
- testHttpConnection()
- testConnectionFail()
A unit test is made up of logical assertions. If an assertion is true, the unit test passes. If an assertion is false then the unit test fails.There are several assertion methods like :
- assertTrue
- assertFalse
- assertNull
- assertEquals.
Ex:
- assertTrue("Login Status ", loginScript.getdetailsLogin() == true );
- assertEquals("Username is FlexUnit", "FlexUnit" ,loginScript.getUsername());
The below displayed is the test case class file, refered from the TestSuite Component.
LoginAccountTest.as
/** * Test Login functionality */
public function testLoginComponent():void{
var loginScript:scripts = new scripts();
loginScript.checkLoginData("FlexUnit", "FlexCoder");
assertTrue("Login Check ", loginScript.getdetailsLogin() == true );
}
/** * Test Username functionality */
public function testUserName():void {
var loginScript:scripts = new scripts();
loginScript.checkLoginData("FlexUnit", "FlexCoder");
assertEquals("Username is FlexUnit", "FlexUnit" ,loginScript.getUsername());
}
Calling the Test Cases:
Create a simple Flex application (loginTestSuite.mxml) which only has one component, TestRunnerBase, which is part of the FlexUnit library. Make sure you include the flexunit name space flexunit.flexui.* in the application tag, so you can access the GUI components from the library. Next we must add functions to set up the test suite and start the testing. We create a createSuite() method to create a new TestSuite object and adds all of our TestCase classes to it using the addTestSuite() method. The addTestSuite() method uses reflection to find all the methods in the class that start with the name test and adds them to list of tests to run. We only have one TestCase class (the LoginAccountTest.as we created in the loginmodel folder). Finally we create an onCreationCompleteHandler() method that is called on the creationComplete event of our TestSuite application. This function sets the unit test property of the TestRunnerBase object to our test suite that is returned by the createSuite() method. Now we start the testing by calling the startTest() method on the TestRunnerBase component.
The code should look like:
Component:
Methods:
/**
* onCreationCompleteHandler - To start the Test Suite UI and Testing
*/
public function onCreationCompleteHandler():void
{
testRunnerComp.test = createTestSuite();
testRunnerComp.startTest();
}
/**
* createTestSuite - To start the Unit Testing by calling the LoginAccountTest.as file
*/
public function createTestSuite():TestSuite
{
var testSuite:TestSuite = new TestSuite();
testSuite.addTestSuite(LoginAccountTest);
return testSuite ;
}