Sharing step definitions across test suites (BDD)

Last edited on

It is possible share BDD step definitions across test suites via collectStepDefinitions() .

For this the file test.py/.js/.rb./etc. of the BDD test cases must be edited, to add an argument to the existing collectStepDefinitions() call.

By default it only looks for step definitions locally:

source(findFile('scripts', 'python/bdd.py'))

setupHooks('../shared/scripts/bdd_hooks.py')

collectStepDefinitions('./steps', '../shared/steps')

def main():
    testSettings.throwOnFailure = True
    runFeatureFile('test.feature')
test.py of a BDD test case (default)

collectStepDefinitions() accepts multiple folder paths as arguments. To include a shared steps folder, add its path to the existing call in every test.py that needs access to it. For example, if your shared steps file lives in a global_steps/ folder at the project root:

my_project/
├── global_steps/
│   └── steps.py          ← shared step definitions
├── suite_A/
│   ├── tst_Example/
│   │   └── test.py
│   └── ...
├── suite_B/
│   ├── tst_Example/
│   │   └── test.py
│   └── ...
source(findFile('scripts', 'python/bdd.py'))

setupHooks('../shared/scripts/bdd_hooks.py')

# extra argument added to the end:
collectStepDefinitions('./steps', '../shared/steps', '../../global_steps')

def main():
    testSettings.throwOnFailure = True
    runFeatureFile('test.feature')
test.py of a BDD test case (updated)

This change must be applied to the test.py of every BDD test case that needs access to the shared steps. After making the change, run one test case first to confirm the path resolves correctly before updating the rest.

NOTE: In addition, the Object Map entries that are used in these steps must also be shared between the test suites: Sharing an Object Map across test suites