1. Create an ActionScript class that extends TestCase
The standard convention is to name the TestCase class after the class being tested with Test added as a suffix. For example if the class to be tested was called RegExp the TestCase class would be called RegExpTest. Additionally by convention the TestCase class is placed in the same package as the class under test. For example if the class was mx.core.UITextFormat the TestCase class would be mx.core.UITextFormatTest.
Create a new ActionScript class that extends TestCase. The remainder of this recipe will use the following RegExpTest as the example:
package { import flexunit.framework.TestCase;
public class RegExpTest extends TestCase { } }
2. Create a method whose name starts with test
The FlexUnit framework uses reflection to determine the methods in a TestCase that should be run. Starting function names with test indicates to FlexUnit that the function should be included in those that are run.
Add the following method to RegExpTest:
public function testRegExp():void { }
3. Make one or more assertions
An assertion is a programmatic way of verifying a statement of fact. The most common form is comparing an expected value to the actual value returned by some operation. FlexUnit includes a number of assertion types that can be used to test different situations. The most common assertions are:
assertEquals: Compare with ==
assertTrue: Check condition is true
assertNull: Check if null
assertStrictlyEquals: Compare with ===
FlexUnit also provides various convenience assertions which test for the opposite condition such as assertFalse and assertNotNull. See the Assert API documentation included with FlexUnit for a complete list of assertions.
Each assertion function can take an optional String as the first argument which will be included in the assertion output should the assertion fail. This String will be prefixed before the default "expected X but was Y" failure message.
When writing assertions keep in mind that if an assertion fails, the rest of the test method will not be executed.
TestCase extends Assert which defines all of the assert functions. This allows subclasses of TestCase to directly call the assert functions. The following code demonstrate the various assertion methods and should be added to the testRegExp function:
var regExp:RegExp = new RegExp("a", "i"); assertFalse(regExp.test("b")); assertFalse("regExp doesn't match", regExp.test("b"));
Add new test methods to the TestCase to test other logical groups of operations. It is convention that each test method focus on testing a specific operation or task. For example when testing create, retrieve, update, and delete operations each one should be put into its own test method such as testCreate, testRetrieve, etc. This way should assertions start to fail, multiple failures will be reported helping diagnosis the issue.
It is important to keep in mind that the order that the test methods in a TestCase are run is random. Each test should create its own data and make no assumptions about another test having already run.
public class RegExpTest extends TestCase { public function testRegExp():void { var regExp:RegExp = new RegExp("a", "i"); assertFalse(regExp.test("b")); assertFalse("regExp doesn't match", regExp.test("b"));