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\BeforeAllCspray\Labrador\AsyncUnit\Attribute\BeforeEachCspray\Labrador\AsyncUnit\Attribute\AfterEachCspray\Labrador\AsyncUnit\Attribute\AfterAllCspray\Labrador\AsyncUnit\Attribute\BeforeEachTest*Cspray\Labrador\AsyncUnit\Attribute\AfterEachTest*
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.
BeforeAllmethods defined are executed when a givenTestCasehas started processing. These methods will only be invoked 1 time perTestCase, even if multiple tests are defined. This should be a static class method.BeforeEachmethods defined on theTestCaseare executed when a given test has started processing. These methods will be invoked 1 time, with a freshTestCaseinstance, for every single test.The test currently being processed will be executed.
AfterEachmethods defined on theTestCaseare executed when a given test has finished processing. These methods will be invoked 1 time, with a freshTestCaseinstance, for every single test.AfterAllmethods defined on theTestCaseare 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
BeforeAllmethods defined on theTestSuiteare executed when a given suite has started processing. These methods will only be invoked 1 timer perTestSuite. Only oneTestSuiteinstance will be created for each suite processed so there is no need for this to be a static method.BeforeEachmethods defined on theTestSuiteare executed before a givenTestCasehas started processing. These methods will be invoked 1 time for everyTestCaseassociated to the suite.BeforeAllmethods defined on theTestCaseare executed. Follows the same rules for the implicit use case.BeforeEachTestmethods defined on theTestSuiteare executed before a given test has started. These methods will be invoked 1 time for every single test.BeforeEachmethods defined on theTestCaseare executed. Follows the same rules for the implicit use case.The test currently being processed will be executed.
AfterEachmethods defined on theTestCaseare executed. Follows the same rules for the implicit use case.AfterEachTestmethods defined on theTestSuiteare executed after a given test has finished. These methods will be invoked 1 time for every single test.AfterAllmethods defined on theTestCaseare executed . Follows the same rules for the implicit use case.AfterEachmethods defined on theTestSuiteare executed after a givenTestCasehas finished processing. These methods will be invoked 1 time for everyTestCaseassociated to the suite.AfterAllmethods defined on theTestSuiteare executed when a given suite has finished processing. These methods will only be invoked 1 time perTestSuite. Only oneTestSuiteinstance will be created for each suite processed so there is no need for this to be a static method.
Last updated
Was this helpful?