Search code examples
phptestingbehat

How to get tags in Behat FeatureContext


Is there a way how to get tags for a scenario in Behat FeatureContext in which the method is being run in?

my.feature

  @SPRF1
  Scenario: My scenario
    Given something is done

FeatureContext

class FeatureContext implements \Behat\Behat\Context\Context
{
    /**
     * @Then something is done
     */
    public function somethingIsDone()
    {
        $tags = $this->getScenarioTags(); // this doesn't exist
    }
}

Solution

  • You should use a BeforeScenarioScope hook. Try something like this:

        /**
         * @BeforeScenario
         */
        public function getTestId(BeforeScenarioScope $scope)
        {
            $tags = $scope->getScenario()->getTags();
        }

    Don't forget to add use Behat\Behat\Hook\Scope\BeforeScenarioScope;