Feature: Fight or flight
@opponent_strategy
In order to increase the ninja survival rate,
As a ninja commander
I want my ninjas to decide whether to take on an
opponent based on their skill levels@slow
Scenario Outline: Weaker opponent
Given the ninja has a third level black-belt
When attacked by a samurai
Then the ninja should engage the opponent
@example_tag
Example:Scenario: Stronger opponent
Given the ninja has a third level black-belt
When attacked by Chuck Norris
Then the ninja should run for his life
In Behave You may also “tag” all your feature or a part of the feature file like a scenario or an example in Scenario Ouline.
then running behave --tags=slow will run just the scenarios tagged @slow. If you wish to check everything except the slow ones then you may run
behave --tags=~@slow
Another common use-case is to tag a scenario you’re working on with @wip and then behave --tags=@wip to just test that one case.
Tag selection on the command-line may be combined:
-
--tags="@wip or @slow"- This will select all the cases tagged either “wip” or “slow”.
-
--tags="@wip and @slow"- This will select all the cases tagged both “wip” and “slow”.
If a feature or scenario is tagged and then skipped because of a command-line control (Ctrl+C) then the before_ and after_ environment functions will not be called for that feature or scenario. Note that behave has additional support specifically for testing works in progress.
The tags attached to a feature and scenario are available in the environment functions via the “feature” or “scenario” object passed to them. On those objects there is an attribute called “tags” which is a list of the tag names attached, in the order they’re found in the features file.
There are also environmental controls specific to tags, so in the above example behave will attempt to invoke an environment.py function before_tag and after_tag before and after the Scenario tagged @slow, passing in the name “slow”. If multiple tags are present then the functions will be called multiple times with each tag in the order they’re defined in the feature file.
One more note is the generated scenarios from a ScenarioOutline inherit the tags from the ScenarioOutline and its Examples section:
# -- FOR scenario in scenario_outline.scenarios:
scenario.tags = scenario_outline.tags + examples.tags
Reference link: https://behave.readthedocs.io/en/latest/tutorial.html#controlling-things-with-tags