ChristopherSFSD's avatar

Organizing Behat test classes / traits

I have a number of Behat context classes that use traits. I'd like to be able to organize these traits into sub directories but when I attempt to use them, they are not found. None of these classes are namespaced. I'm not sure if namespacing them is the way to solve this.

Any tips would be appreciated.

Example ...

- boostrap
    - UserContext (extends BaseContext, uses UserTrait)
    - UserTrait

I'd prefer to put UserTrait in a subdirectory ...

- boostrap
    - UserContext (extends BaseContext, uses UserTrait)
    - Users
        - UserTrait

How do I get UserContext to use Users/UserTrait?

In my behat.yml file ...

  suites:
    user_features:
      paths:    [ %paths.base%/features/user ]
      contexts: [ UserContext ]
0 likes
1 reply
ChristopherSFSD's avatar
Level 4

Turns out namespacing was the key. I gave all my behat tests a namespace of MyAppName\Tests\Behat. I then added modified composer.json and dumped the autoloader ...

"autoload-dev": {
    "psr-4": {
        "MyAppName\\Tests\\Behat\\": "features/bootstrap"
    }
},

I updated my behat.yml file to include the namespace for my various contexts ...

suites:
  user_features:
    paths:    [ %paths.base%/features/user ]
    contexts: [ MyAppName\Tests\Behat\UserContext ]

And finally I had to update my various test classes and traits with use statements ...

use Carbon, Config, DB; // etc

Special thanks to @lagbox and @emgee in IRC

Please or to participate in this conversation.