How to control the order in which your tests run

The Basics

Your tests need to act independently of each other, so the point of ordering your tests in a custom way is not to ensure that test A sets up some state that test B needs. If this is the reason you are reading this section, please reconsider. Tests need to be independent of each other and generally independent of order.

Why then would we include the capability to manipulate order? Well, because there are other valid reasons to do so. If you have many tests of increasing complexity, it makes sense to order these so that, in the event a simple test fails, you don’t waste time running the more complicated versions.

You may also have hundreds of test methods in your [TestCase] or hundreds of TestCases and you wish to decide when to run these in a way that we have not even begun to consider.

FlexUnit 4 provides the ability to manipulate both the order and selection criteria for all tests using filters and sorts.

How to Order Items Inside Your Test Cases

Inside of a Test Case, you can use the order attribute on metadata. The specific metadata tags that support ordering are: BeforeClass, AfterClass, Before, After, Rule and Test.

e.g.

[BeforeClass (order=1)]

Keeping in mind that each group of metadata tags is run separately from each other, so the order value assigned to each tag is internal to each type of tag. It is important to understand that any metadata tag without an order set will be assigned an order value of zero, which unless you have negative order values, will run first. So it is best practice to associate an order value to all methods that you intend to sort otherwise you may have unexpected results.

For example in the following scenario, you'll notice the test function set with an order of one and the other without any order, where as the beforeClass functions are set with an order of two and three. Remember, as previously noted, each group of tags is run independent of each other and will only take into account the order of similar tags. So myBeforeClass1 will run first, followed by myBeforeClass2 (ordered from lowest to highest) and when those are completed, then myTest (with an implied order of zero) will run followed by myTest1.

[Test]
public function myTest() : void
{
}

[Test(order=1)]
public function myTest1() : void
{
}


[BeforeClass(order=2)]
public static function myBeforeClass1() : void
{
}


[BeforeClass(order=3)]
public static function myBeforeClass2() : void
{
}

<br>
As the test cases are run through, each group of metadata is ordered according to the number in the order metadata attribute. There are more advanced methods of ordering using custom sorting methods, which are covered in more detail in the advanced terms section.

How to Order Test Cases and Test Suites

The (order = #) metadata will apply to TestCases and Test Suites as well. Suite order will take preference over TestCase order which takes preference over TestMethod order.


[Suite(order=2)]
[RunWith("org.flexunit.runners.Suite")]
public class Suite1
{
        public var test1:TestCase1;
     public var test2:TestCase2;        
}

[Suite(order=1)]
[RunWith("org.flexunit.runners.Suite")]
public class Suite2
{
        public var test3:TestCase3;          
}

[TestCase(order=2)]
public class TestCase1
{         
     [Test]
     public function testFnc():void
     {
        }
}

[TestCase(order=1)]
public class TestCase2
{         
     [Test]
     public function testFnc2():void
     {
        }
}

[TestCase(order=1)]
public class TestCase3
{         
     [Test]
     public function testFnc3():void
     {

        }
}

Order of Execution

Suite2 --> TestCase3 --> Suite1 --> TestCase2 --> TestCase1

Previous

  • No labels