Hooks
A comprehensive look at the entire Hook lifecycle for a defined TestSuite and TestCase.
Hooks are methods that allow you to perform scaffolding operations, free resources, or perform some other operation before or after a test is processed. The hooks that can be defined in AsyncUnit are:
Cspray\Labrador\AsyncUnit\Attribute\BeforeAll
Cspray\Labrador\AsyncUnit\Attribute\BeforeEach
Cspray\Labrador\AsyncUnit\Attribute\AfterEach
Cspray\Labrador\AsyncUnit\Attribute\AfterAll
Cspray\Labrador\AsyncUnit\Attribute\BeforeEachTest
*Cspray\Labrador\AsyncUnit\Attribute\AfterEachTest
*
Methods marked with a * can only be defined on a TestSuite
. Defining them on a TestCase
is currently ignored and in future versions will result in a compilation error. All other attributes can be defined on methods belonging to either a TestSuite
or a TestCase
.
Based on whether you have defined an explicit TestSuite
your hook lifecycle will have 2 "effective" orders. The order of hooks for the explicit TestSuite
is followed for all suites, even the ImplicitTestSuite
, however the implicit suite will never have a hook defined and thus is effectively always skipping the TestSuite
specific hooks.
Hook order for ImplicitTestSuite
If you are using the ImplicitTestSuite
then the hook order is straightforward and, for the most part, aligns with what you'd be used to with PHPUnit or other testing frameworks. The following hook lifecycle applies to each TestCase
.
BeforeAll
methods defined are executed when a givenTestCase
has started processing. These methods will only be invoked 1 time perTestCase
, even if multiple tests are defined. This should be a static class method.BeforeEach
methods defined on theTestCase
are executed when a given test has started processing. These methods will be invoked 1 time, with a freshTestCase
instance, for every single test.The test currently being processed will be executed.
AfterEach
methods defined on theTestCase
are executed when a given test has finished processing. These methods will be invoked 1 time, with a freshTestCase
instance, for every single test.AfterAll
methods defined on theTestCase
are executed when all tests are finished executing for the givenTestCase
. These methods will only be invoked 1 time perTestCase
, even if multiple tests are defined. This should be a static class method.
Hook order for explicit TestSuite
If you have defined an explicit TestSuite
then your hook order starts to become more advanced, and with advance functionality comes an increase in complexity. It is important before you start defining your own hooks on TestSuite
you understand how they operate and interact with your tests. The following hook lifecycle applies to each TestSuite
BeforeAll
methods defined on theTestSuite
are executed when a given suite has started processing. These methods will only be invoked 1 timer perTestSuite
. Only oneTestSuite
instance will be created for each suite processed so there is no need for this to be a static method.BeforeEach
methods defined on theTestSuite
are executed before a givenTestCase
has started processing. These methods will be invoked 1 time for everyTestCase
associated to the suite.BeforeAll
methods defined on theTestCase
are executed. Follows the same rules for the implicit use case.BeforeEachTest
methods defined on theTestSuite
are executed before a given test has started. These methods will be invoked 1 time for every single test.BeforeEach
methods defined on theTestCase
are executed. Follows the same rules for the implicit use case.The test currently being processed will be executed.
AfterEach
methods defined on theTestCase
are executed. Follows the same rules for the implicit use case.AfterEachTest
methods defined on theTestSuite
are executed after a given test has finished. These methods will be invoked 1 time for every single test.AfterAll
methods defined on theTestCase
are executed . Follows the same rules for the implicit use case.AfterEach
methods defined on theTestSuite
are executed after a givenTestCase
has finished processing. These methods will be invoked 1 time for everyTestCase
associated to the suite.AfterAll
methods defined on theTestSuite
are executed when a given suite has finished processing. These methods will only be invoked 1 time perTestSuite
. Only oneTestSuite
instance will be created for each suite processed so there is no need for this to be a static method.
Last updated