Specifying the Location of Step Definitions

Have you ever wondered how Cucumber finds your step definitions? There is nothing in Cucumber that expects them to be under features/step_definitions. In fact, if you renamed these two directories to jolly/jumper and ran cucumber jolly, things would work just as well.

Cucumber recursively scans some directories for .rb files to load, and if there are step definitions in them (or hooks or transforms), they get loaded. It’s important to understand what directories Cucumber scans. Once you understand this, you will save yourself from a common source of confusion and frustration. Let’s illustrate that with an example. Assume you have organized your features and step definitions as follows:

 
features
 
├── billing
 
│   └── credit_card.feature
 
├── scoring
 
│   ├── multi_player.feature
 
│   └── single_player.feature
 
└── step_definitions
 
├── billing_steps.rb
 
└── scoring_steps.rb

If you run all of the features with cucumber features or just cucumber, everything will work great. Then, one day you add a new scenario to your credit_card.feature file to support a new credit card. Then you run it:

 
$ ​cucumber features/billing/credit_card.feature:104 -f progress
 
UUUU

All of your steps show up as undefined, even though your new scenarios were reusing some existing step definitions. The directories scanned for step definitions and support code are determined by the feature files (or directories) you give it. If you give it a feature file, it will look underneath its directory. If you give it a directory, it will look under that directory. To see where Cucumber is looking for code, you can pass the --verbose option.

The solution to this common problem is to use --require to tell Cucumber explicitly where to load code from. The correct way to run the previous feature would be as follows:

 
$ ​cucumber features/billing/credit_card.feature:104 -f progress -r features
 
...F

Our code is now found and loaded, and we can carry on as we had intended, working on our new, failing scenario. You can configure this to happen by default using a custom profile, which we’ll show you in a few pages.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.93.64