<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=278116885016877&amp;ev=PageView&amp;noscript=1">


May 30, 2018 | 3 Minute Read

How To Speed Things Up With Behat Commands

Table of Contents


Behat is a tool that makes behavior-driven development (BDD) possible. With BDD, users can write human-readable stories that describe the behavior of their application. These stories can then be auto-tested against the application.

There are a lot of resources out there that go into what Behat is and why you should use it. Getting good at practicing BDD means more than learning a new tool—it will change your entire development process for the better. Imagine a world where communication on your team is perfect, you always deliver exactly what your client wanted, and chocolate ice cream is free. Okay, we can’t promise all of that, but we’ll see how BDD makes development fun once again.

In this blog, I’ll cover useful Behat commands that help make execution faster. Behat comes with a powerful console utility responsible for executing Behat tests written in the Gherkin language. The utility comes with a wide array of options.

Gherkin filters

Tag filters support three logical operators:

  • Tags separated by commas will be combined using the OR operation
  • Tags separated by && will be combined using the AND operation
  • Tags preceded by ~ will be excluded using the NOT operation

For example:

$ behat --tags '@orm,@database'
This command will run only features or scenarios which have the @orm OR @database tag (or both).

$ behat --tags 'ticket,723'
This command will run only features or scenarios which have the@ticket OR @723 tag (or both).

$ behat --tags '@orm&&@fixtures'
This command will run only features or scenarios with both the @orm AND @fixtures tags (it must have both).

$ behat --tags '~@javascript'
This command will run all features or scenarios NOT tagged with @javascript (all except those tagged @javascript).

$ behat --name 'number has'
This command will run only features and scenarios that contain ‘number has’ in their title.

$behat -d ‘search string’
To search for a specific definition, users need to execute this command.

Behat Commands

$behat -dl
If you forget what step definitions you’ve already implemented, or how to spell a particular step, Behat will print all available definitions by calling them with the -dl option.

$behat —story syntax — lang fr
If you write features in a language other than English, you can view a localized example feature in your language of choice by using the --langoption.

$ behat --story-syntax
This command will print an example feature for you to understand what keywords to use and where to use them in your *.feature files.

Custom Profiles

All configuration parameters in the behat.yml file are defined under a profile name root (default: for example). A profile is just a custom name you can use to quickly switch the testing configuration by using the --profile option when executing your feature suite.

The default profile is always the default. All other profiles inherit parameters from the default profile. If you only need one profile, define all of your parameters under the default: root:

# behat.yml



To run custom profiles, use the --profile option:

behat --profile [profile-name]

behat --profile [profile-name]

Helpful Behat commands

Execute complete folder:
bin/behat features/folder

A single feature:
bin/behat features/folder/filename.feature

A single scenario within a feature:
bin/behat features/teamname/filename.feature:7
where 7 represents the first line of the scenario. Exclude the tagname if present.

All tests with a certain tag (using @ symbol):
bin/behat --tags @tagname

All tests except a certain tag (omitting @ symbol):
bin/behat --tags ~tagname

Multiple tags

# Run tests tagged with @tagname1 OR @tagname2
bin/behat --tags "@tagname1,@tagname2"

# Run tests tagged with both @tagname1 AND tagname2 (you can also use the equals sign after "--tags" if you prefer)
bin/behat --tags="@tagname1&&@tagname2"

# Run tests tagged with @tagname1 AND @tagname2 AND NOT @tagname3
bin/behat --tags "@tagname1&&@tagname2&&~@tagname3"

If you add a @javascript tag above the feature (for all the scenarios) or above a single scenario, Selenium and Firefox will be used to run the test. These defaults can be changed in the behat.yml file.

Styles available for redefinition

  • undefined - style of undefined step
  • pending - style of pending step
  • pending_param - style of param in pending step
  • failed - style of failed step
  • failed_param - style of param in failed step
  • passed - style of passed step
  • passed_param - style of param in passed step
  • skipped - style of skipped step
  • skipped_param - style of param in skipped step
  • comment - style of comment
  • tag - style of scenario/feature tag

Found this helpful? Do share your thoughts in the comments below.

About the Author
Jaspreet Singh, Senior Project Manager
About the Author

Jaspreet Singh, Senior Project Manager

Nickname: Jazz. Family man, obsessed with watching, playing, reading about cricket—if you play a round of "snooker" with him, prepare to lose.

Back to Top