Behat: BeforeSuite and AfterSuite Hooks

Profile picture for user devraj

Suite hooks are run outside of the scenario context. It means that your context class (e.g. FeatureContext) is not instantiated yet and the only way Behat can execute code in it is through the static calls. That is why suite hooks must be defined as static methods in the context class.

BeforeSuite: The BeforeSuite hook is run before any feature in the suite runs. For example, you could use this to set up the project you are testing. This hook receives an optional argument with an instance of the Behat\Testwork\Hook\Scope\BeforeSuiteScope class.

AfterSuite: The AfterSuite hook is run after all features in the suite have run. This hooks is useful to dump or print some kind of statistics or tear down your application after testing. This hook receives an optional argument with an instance of the Behat\Testwork\Hook\Scope\AfterSuiteScope class.

/** @BeforeSuite */
public static function setup(BeforeSuiteScope $scope)
{
    echo "Before Suite Executed";
}


/** @AfterSuite */
public static function teardown(AfterSuiteScope $scope)
{
    echo "After Suite Executed";
}
Tags