You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Creating a simple TestSuite, TestCase and TestMethod''

Creating a Test Method

  • Open the TestCase1 file in your sampleSuite.tests package.
  • Add the following to TestCase1 as a new method called simpleAdd() that looks like this:
[Test( description = "This tests addition" )]
public function simpleAdd():void 
{
     var x:int = 5 + 3;
     Assert.assertEquals( 8, x );
}
  • While ridiculously simple, this test shows that the ‘+’ operator in ActionScript works correctly. You assert that the result of this math operation should be equal to 8. The description descriptor is ''optional'' but is recommended to easily remember what your test does. Make special note of the call to assertEquals as a static method of the Assert class. This is the recommended approach to making ''any'' assertions.
  • Keep in mind you'll also need to import Assert using import org.flexunit.Assert. You may also notice there are two Assert classes one in org.flexunit and the other in flexunit.framework. There is some code similarity across FlexUnit 4 and FlexUnit 1 to maintain backwards compatiblity. Unless you are specifically using FlexUnit 1, you will want to use all classes from the org.flexunit package.

Creating a Test Suite

  • Right click on your src folder and select New -> Test Suite Class name this class "SampleSuite" and change the package to sampleSuite click finish.
  • Open the SampleSuite.as file
  • Import the TestCase1 class you just created with the following code:
    import sampleSuite.tests.TestCase1;
    
  • Declare variables for the test case classes, to register your test cases in this suite. Your code should look like this:
    [Suite]
    [RunWith("org.flexunit.runners.Suite")]
    public class SampleSuite
    {
         public var t1:TestCase1;    
    }
    
  • [Suite]\ notifies FlexUnit 4 that this class is a suite and to expect:

  • [[RunWith|[RunWith("org.flexunit.runners.Suite")]]] which specifies what runner to use for this test class. Most of the time this is the runner you will want to use. However, FlexUnit 4 allows the use of custom runners. [[RunWith|[RunWith("")]]] is where this custom runner would be specified.
    '''Note:''' When declaring your test classes, ''always'' declare the variable as public otherwise FlexUnit 4 will not be able to run them!

Running your tests

You may of course have your unit tests run several different ways if you prefer. Any number of listeners may be added to your test run. Also, any number of suites can be added to your Runner and run consecutively. For this tutorial we will discuss how to go about using Flash Builder's internal process of running tests and suites. If you wish to learn about the other options, please see the [Test Runner] section.

Suggested Directory Organization

The FlexUnit 4 framework doesn’t prescribe a directory structure for testing files; however, the following suggestions work well:

  • Each test suite |FlexUnit TestSuite] should have its own directory.
  • The suite definition should reside in this directory.
  • Within the directory for the test suite, create a tests directory.
  • Inside of this directory store each of your test cases for this test suite .
  • Any additional classes needed to accomplish testing can be stored in further subdirectories.

Ideally, the directory structure looks something like this

src/
     testSuite1/ 
          TestSuite1.as 
          tests/ 
               TestCase1.as 
               TestCase2.as 
               TestCase3.as 
     testSuite2/ 
          TestSuite2.as  
          tests/ 
               TestCase1.as 
               TestCase2.as 
               TestCase3.as 

[Previous ] | [Next ]

  • No labels