Run your first unit test with FlexUnit

By xngo on February 26, 2019

The steps below show you step-by-step how to run your first unit test with FlexUnit. Don't worry if you don't understand the code. They will be explained in later tutorial.

  1. Create a Flex project using Adobe Flash Builder.
  2. Download FlexUnit from Adobe.
  3. Decompress FlexUnit into the lib folder of your project. Be careful, the zip file is compress twice. At the end, you should see lib/*.swc files. Make sure that you refresh your project after you added the files. Otherwise, Adobe Flash Builder will not see them.
  4. Suppose that you want to unit test the following ActionScript class:
    // Calculator.as
    package 
    {
      public class Calculator
      {
        // The constructor
        public function Calculator()
        {}
     
        // The implementation of the Add function.
        public function add(a:int, b:int): int
        {
          return a+b;
        }
      }
    }
     
  5. You create an ActionScript test class to test the add() function like the following:
     
    // CalculatorTestCase.as
    package
    {
      import org.flexunit.Assert;
     
      public class CalculatorTestCase
      {   
        [Test( description = "Test add()." )]
        public function AddSimpleTest():void 
        {
          var iA:int = 5;
          var iB:int = 6;
          var iExpected:int = 11;
     
          var oCalc:Calculator = new Calculator();
     
          Assert.assertEquals( iExpected, oCalc.add(iA, iB ) );
        }
     
        [Test( description = "This test is intended to fail." )]
        public function AddFailTest():void 
        {
          var iA:int = 5;
          var iB:int = 6;
          var iExpected:int = 12;
     
          var oCalc:Calculator = new Calculator();
     
          Assert.assertEquals( iExpected, oCalc.add(iA, iB ) );
        }   
      }
    }
     
  6. You have to add your test class to the Test Suite class like the following:
    // CalculatorTestSuite.as
    package
    {
      [Suite]
      [RunWith("org.flexunit.runners.Suite")]
      public class CalculatorTestSuite
      {
        // Make sure that your test cases object are declared as public. 
        //  Otherwise, FlexUnit will not be able to run it.
        public var oCalculatorTestCase:CalculatorTestCase = new CalculatorTestCase();
      }
    }  
     
  7. In your *.mxml file, to run your Test Suite, you have to add it as follow:
    <?xml version="1.0" encoding="utf-8"?>
    <!-- FlexUnitSample.mxml -->
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
             xmlns:s="library://ns.adobe.com/flex/spark" 
             xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
     
             xmlns:flexUnitUIRunner="http://www.adobe.com/2009/flexUnitUIRunner"
             creationComplete="runMe()"><!-- Add this namespace. -->
     
      <fx:Script>
        <![CDATA[
          import org.flexunit.listeners.UIListener;
          import org.flexunit.runner.FlexUnitCore;
     
          private var core:FlexUnitCore;
     
          public function runMe():void
          {
            core = new FlexUnitCore();
            core.addListener(new UIListener(uiListener));
            core.run(CalculatorTestSuite); // Add all your Test Suite classes here.
          }
        ]]>
      </fx:Script>
     
      <flexUnitUIRunner:TestRunnerBase id="uiListener" width="100%" height="100%" />
     
    </s:Application>
     
  8. Run your MXML application. You should see the FlexUnit Runner graphical interface. 2 tests are run: 1 success, 1 failed.

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.