Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 768
Samer Avatar asked Oct 30 '14 16:10

Samer


2 Answers

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
like image 72
Samer Avatar answered Oct 17 '22 03:10

Samer


You don't need to write it quite like that. It works for me with the following:

# behat.yml
default:
    autoload: [ %paths.base%/contexts ]
    extensions:
        Behat\MinkExtension:
            base_url: http://www.google.com
            sessions:
                default:
                    selenium2: ~
        Sanpi\Behatch\Extension: ~
    suites:
        default:
            paths:    [ %paths.base%/features ]
            filters:
            contexts:
                - FeatureContext

Notice that I didn't have to put it on a new line or treat it like an associative array. I changed my contexts to autoload from the "contexts" directory in root. I find it kind of annoying that it is a subfolder under "features" and that the folder is called "bootstrap" not "contexts" by default.

I wish that Behat 3.x was better documented. You can't even find this information clearly in the code anywhere.

like image 4
Patrick Avatar answered Oct 17 '22 05:10

Patrick