Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

Why use Before and After Metadata Tags

Some test cases perform assertions on a specific set of classes. In order to perform an assertion on a class, it needs to be first initialized. Onces the assertions are complete the instances of the class should be destroyed for garbage collection. One way to implement this is to initialize and destroy the instances of the classes that need to be tested in the test method its self. This approach however, leads to a great deal of duplicate code. Instead, special methods marked with [Before] and [After] metadata tags should be used to create and destroy instances of the classes used in testing. Methods marked with [Before] metadata are called before every test method is executed. These methods are ideal for initializing classes. Methods marked with [After] metadata on the other hand are called after each method is executed. These methods are ideal for destroying instances of classes for garbage collection.

Why use BeforeClass and AfterClass Metadata Tags

There are instances where setup is necessary before executing any test method. For example it may be required that each test connects to a database before its assertions are tested. Instead of writing the same code in each test method to connect to a database, it would be a better idea to connect once and maintain a reference to the connection before any of the tests are run. This is done by defining a method that is decorated with the [BeforeClass] metadata. Once all of the tests have been executed, it may be necessary to close the open connection. In this case a method decorated with the [AfterClass] metadata tag would be used. This method, like the [BeforeClass] method is called once after all of the tests have been executed.

Before, After, BeforeClass and AfterClass Metadata Tags Defined

The [Before],[After], [BeforeClass] and [AfterClass] metadata tags are pieces of metadata which control the flow of the test execution when used. FlexUnit4 then organizes these pieces and runs them in Sequences. Sequences are the way that FlexUnit4 controls the flow of the running of tests.

Working through an example

For an example of the [Before], [After], [BeforeClass] and [AfterClass] metadata tags see sequences.