Behat 3.0 features folder/path
Asked Answered
A

2

8

I am trying to setup Behat 3.0. I want to change the path of where my features go.

Currently, my behat.yml config looks like this:

default:
    autoload:
        '': app/tests/acceptance

Running behat --init wil create the acceptance/FeatureContext.php in the app/tests directory.

However, it will create the features folder in the root of my project. I would instead want this features folder to be placed in the app/tests/acceptance folder.

How can I do this?

Animated answered 12/1, 2015 at 15:41 Comment(0)
H
17

Behat 3 has support for suites and profiles.

Only thing you have to do is to add custom paths to the default profile:

default:
  autoload:
    '': %paths.base%/app/tests/acceptance
  suites:
    default:
      paths: [ %paths.base%/app/tests/acceptance/features ]

Tip 1

Always use the %paths.base% variable to be able to run your Behat tests from a different directory.

Tip 2

Depending on Behat's PSR-0 autoload mechanism can be problematic when you'll have more contexts implemented.

Good practice is to use the composer's PSR-4 autoload mechanism to be able to run namespaced Behat features.

After you have setup Behat as in the example above you need to delete the autoload section in the bahat.yml and add contexts to the default profile:

default:
  suites:
    default:
      paths: [ %paths.base%/app/tests/acceptance/features ]
      contexts: [ MyApp\Tests\Acceptance\FeatureContext ]

Add autoloading configuration to composer.json:

{
    [...]
    "autoload-dev": {
        "psr-4": {
            "MyApp\\Tests\\Acceptance\\": "app/tests/acceptance"
        }
    }
    [...]
}

And then simply dump the autoloader with composer dump-autoload.

Heaver answered 12/1, 2015 at 21:39 Comment(0)
O
1

I suffered a lot with it, so here is my (simple) solution, in the hope it will save time to someone oneday. Using behat 3.8.1.

My files layout:

behat.yml
test/acceptance/bootstrap/FeatureContext.php
test/acceptance/features/myAcceptanceTest.feature

My behat.yml file:

default:
  autoload: [ '%paths.base%/test/acceptance/bootstrap' ]
  suites:
    default:
      paths: [ '%paths.base%/test/acceptance/features' ]
      contexts: [ test\acceptance\bootstrap\FeatureContext ]

Unlike everything I read, including the documentation, I had to add quotes to be able to use the %paths.base% variable.

The autoload parameter tells Behat where to find your context php file(s).

The paths parameter tells Behat where to find your features file(s).

The tricky part is the contexts parameter: it gives the namespace path of your FeatureContext class. You need to make sure that it matches what you have at the top of your FeatureContext.php file:

<?php

namespace test\acceptance\bootstrap;
...
class FeatureContext implements Context
{
...
}

With all these planets properly aligned, it works beautifully. From the project folder:

$ vendor/bin/behat

HTH

Outoftheway answered 6/7, 2022 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.