Behat 3 - `FeatureContext` context class not found and can not be used

Samer picture Samer · Oct 30, 2014 · Viewed 10.3k times · Source

I have tried Behat 2.5 in the past and had no issues setting it up, but now I just downloaded Behat 3 and I am having some difficulties trying to set it up.

My problem is that, after a fresh install, if I create a behat.yml file I cannot seem to be able to define where the FeatureContext file is and I cannot run any tests.

My composer.json is like follows:

{
"require-dev": {
    "behat/behat": "~3.0.4",
    "sensiolabs/behat-page-object-extension": "2.0.*@dev"
},
"require": {
    "behat/mink": "1.6.*",
"behat/mink-goutte-driver": "*",
    "behat/mink-selenium2-driver": "*"
}

}

My project folders are structured the following way:

behat/
  bootstrap/
    FeatureContext.php
  config/
    behat.yml
  features/
    CheckHome.feature
  vendor/
  composer.json
  composer.lock

And my behat.yml file:

default:
  autoload:
    '': %paths.base%/../bootstrap
  suites:
    default:
      paths:
        - %paths.base%/../features
      contexts:
        - FeatureContext

And when I try to run the scenario inside CheckHome.feature using

vendor/bin/behat

I get the following error:

Behat\Behat\Context\Exception\ContextNotFoundException]
`FeatureContext` context class not found and can not be used.

Which is the correct way to set up autoload so it recognises my context?

Thanks

Answer

Samer picture Samer · Oct 31, 2014

I fixed it. I was supposing that the base path was the root of my directory, but it is the place were the behat.yml is stored. So, in order to work with my current config, I had to change the paths in the behat.yml file as follows:

default:
  autoload:
    '': %paths.base%/../bootstrap
  suites:
    default:
      paths:
        - %paths.base%/../features
    contexts:
        - FeatureContext