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

I have tried Behat 2.5 in the past and have had no setup issues, but now I just downloaded Behat 3 and am having some difficulty getting it set up.

My problem is that after a fresh install, if I create the behat.yml file, I cannot determine where the FeatureContext file is located, and I cannot run any tests.

My composer .json looks like this:

{
"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 like this:

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 script inside CheckHome.feature using

vendor/bin/behat

      

I am getting the following error:

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

      

What is the correct way to set up autoloading so that it recognizes my context?

thank

+3


source to share


2 answers


I fixed it. I assumed the base path was the root of my directory, but that location was recorded in the behat.yml file. So, in order to work with my current configuration, 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

      

+1


source


You don't have to write it that way. 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

      



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

I want Behat 3.x to be better documented. You can't even find this information in your code anywhere.

+3


source







All Articles