Altering hierarchical ordering of test fixture

The [Before] and [After] metadata marks methods that are run before and after each test in a given class. For more information on [Before] and [After] methods check out: flexunit.org.

The standard hierarchy for before and after methods follows a 'bubbliing' flow, as shown:

BaseClass (the base class)
SandwichClass (extends from BaseClass)
ActualTestClass (extends from SandwichClass)

The before and after methods normally run in the hierarchy as shown:

BaseClass.BeforeMethod
    SandwichClass.BeforeMethod
        ActualTestClass.BeforeMethod
            Test
        ActualTestClass.AfterMethod
    SandwichClass.AfterMethod
BaseClass.AfterMethod

It can be useful to extend test classes from another class, particularly for groups of tests that load a common set of resources. In these situations, it may be easiest to define a base class with all the necessary resources and extend from there.

Issues arise when you desire to alter the standard ordering. Because the ordering is made to follow this hierarchical model, the order attributes are ignored: For instance:

[Before(order=3)]
public function baseClassSetUp():void { ... }

[Before(order=2)]
public function sandwichClassSetUp():void { ... }

[Before(order=1)]
public function actualTestClassSetUp():void { ... }

Despite the fact that the order value has been set in opposition to the hierarchy, these will still run in the following order::

baseClassSetUp()
    sandwichClassSetUp()
        actualTestClassSetUp()

In order to alleviate this concern, FlexUnit provides a workaround for standard hierarchical sorting. This workaround must be run using the FlexUnitCore class to run your tests request. If you are using Flash Builder premium, learn how to run tests with FlexUnitCore here. You can modify your FlexUnitApplication.mxml file as follows:

private function onCreationComplete():void
{
    var core:FlexUnitCore = new FlexUnitCore();
    var req:Request = Request.classes( OrderSuite ).sortWith( new OrderArgumentSorter() );
    core.run( req );
}

If you are using the standard FlexUnitAppplication.mxml file, then onCreationComplete() will be set to run when the Application layer fires its creationComplete event.

If you wish to run this with the web results display, use the following creationComplete() method.

private function onCreationComplete():void
{
    var core:FlexUnitCore = new FlexUnitCore();
    core.addListener( new UIListener( testRunnerBase ) );

    var req:Request = Request.classes( OrderSuite ).sortWith( new OrderArgumentSorter() );
    core.run( req ); 
}

In essence, this lets FlexUnit know that the order metadata takes priority over class hierarchy. Using the above example, here is how the before methods would run following the sort.

actualTestClassSetUp()
    sandwichClassSetUp()
        baseClassSetUp()
            Test
        actualTestClassTearDown()
    sandwichClassTearDown()
baseClassTearDown()

Notice that the after methods still run in hierarchical order, because the sort has not been applied.