In behave, how do you run a scenario only?

42.8k views Asked by At

I have a 'behave' feature that has a lot of tests on it.

I only need to run a specific scenario for development needs.

How do I do it?

(preferably on the command line)

5

There are 5 answers

0
Janos On

A very powerful trick in behave:

behave --wip

And tag your test-under-development with @wip for the time being. This would have been my #1 answer besides the other mentioned ways to select tests (--name, --tags, --include), but is quite much hidden yet in the answer by @Cynic.

0
mud On

Also you might well be interested in this beautiful post describing how to run just a single test from a scenario outline in behave.

e.g.

#This is Gherkin
Feature: Running a single test from a scenario outline

@scenarioGroupName @scenarioGroupName<scenarioName>
Scenario Outline: test running one of many scenarios, iteration <n> / <scenarioName>
  Given test is index <n> for <scenarioName> 
  When we do something with <scenarioParameter>  
  Then all the checks pass


Examples: Test scenarios
 |n | scenarioName  | scenarioParameter
 |1 | Able          | first
 |2 | Baker         | second 
 |3 | Charlie       | third
 #etc

To run this with just data for scenario 2/baker, one might do

# This is shell
behave -k --tags=@scenarioGroupNameBaker

Note how scenarioGroupNameBaker tag is built. For each example row, the outline/template tag scenarioGroupName<scenarioName> has <scenarionName> replaced with the value from the example row to give the tag name per scenario-row :D

1
Louis On

To run only a single scenario you can use -n with the name of the scenario:

$ behave -n 'clicking the button "foo" should bar the baz'

I'm using single quotes above to keep the name of the scenario as one argument for -n. Otherwise, the shell will pass each word of the scenario name as a separate argument.

0
shufflingb On

Tags provide a couple of options ...

1) Tag the slow ones and then avoid by invoking with the inverse e.g.

behave -t '~@slow_tag_name'  

2) However for the most flexibility I'd personally recommend tagging each Scenario with a unique ID. e.g. I use a @YYYY_MM_DD_HHmm_Initials tag scheme since, this is unique enough and the traceability is useful/interesting. Then you can always simply invoke with the tag and get it to run the Scenario, .e.g

behave @2015_01_03_0936_jh 
0
Cynic On

If you want to run a single test for that feature, use the -n or --name flag which seems to want the text after Scenario:

behave -n 'This is a scenario name'

You can run a feature file by using -i or --include flags and then the name of the feature file.

behave -i file_name.feature

or:

behave --include file_name

You can also exclude with the --exclude flag:

behave -e file_name

For more information check the documentation for command line arguments. There's a lot of useful information hidden in their appendix section.


NOTE: At the time I'm writing this it won't work with Python 3.6 and Behave 1.2.5, due to this issue. (UPDATE: 1.2.6 is out and fixes this, but if you're on python 3.4 that version won't be available from pip so you can workaround this with pip3 install git+https://github.com/behave/behave#1.2.6rc ).

It also seems like you should be able to pass in the text after Feature: for the -i flag but currently that doesn't work. Somebody remind me to updated if it works again. I also encourage people to check out the wip flag, which allows you to add @wip to a test, then -wip will not only run the test but also allow print/logging statements for debugging.