Parameterized test in Flex

By xngo on February 26, 2019

  1. Import package import org.flexunit.runners.Parameterized;.
  2. Add the meta data [RunWith("org.flexunit.runners.Parameterized")] at the top of the parameterized test class.
  3. Declare a static data set(i.e. myTestData).
  4. Add the meta data [Test(dataProvider="myTestData")] at the top of the test function. Make sure that the order of the function parameters(i.e. iA:int, iB:int, iExpected:int) match the order your data set.

package org.tutorial.calculator.unittest
{
  import org.flexunit.runners.Parameterized;
  import org.flexunit.asserts.assertEquals;
 
  [RunWith("org.flexunit.runners.Parameterized")]
  public class FlexParameterizedTest
  {
    public static var myTestData:Array = [
                                          // iA, iB, iExpected
                                          [2, 4, 6],
                                          [-2,4, 2],
                                          [5, 6, 14], // Will fail.
                                          [0, 1, 1],
                                         ];
 
    [Test(dataProvider="myTestData")]
    public function getSumTest(iA:int, iB:int, iExpected:int): void
    {
      assertEquals("getSum() doesn't give expected result.", iExpected, this.getSum(iA, iB));
    }
 
    /**
     * Function to be tested. It adds 2 integers.
     * Note: For convenient and completeness, I put this function 
     *  in this same test class. In practice, you shouldn't. 
     */
    public function getSum(iA:int, iB:int): int
    {
      return iA+iB;
    }
  }
}

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.