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

11,054

Solution 1

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

Solution 2

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.

Share:
11,054
Samer
Author by

Samer

Updated on June 23, 2022

Comments

  • Samer
    Samer almost 2 years

    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