A TestMethod is the smallest unit of the testing framework. A test method executes code and checks an outcome. At the end of the test method we generally make an assertion, stating our beliefs about the state of this particular test method if all worked as planned. We might expect a value to be true or false, null or not null, perhaps even equal to another variable, or simply not in a list of acceptable values. If the assertion is valid, the test passes. If the assertion does not represent reality (we specify it should be false but it is really true), the test fails.

Though a single test method may setup several conditions, exercise several pieces of code, or even wait for an asynchronous event, it should ultimately make a minimum number of assertions. When we make assertions in a test case, if any assertion fails, the entire test fails. Therefore, the fewer number of assertions we make in a single test method, the more granular the method and hence the easier to determine what actually failed.

Example TestMethods

[Test( description="This tests addition" )]
public function simpleAdd() : void
{
     var x:int = 5 + 3;
     Assert.assertEquals( 8, x );
}

[Test( description="This tests subtraction" )]
public function simpleSubtract() : void
{
     var x:int = 5 - 3;
     Assert.assertEquals( 2, x );
}
  • No labels