096-BDD测试框架Behave
安装Installation
Using pip (or …)
Category: Stable version
Precondition: pip (or setuptools) is installed
Execute the following command to install behave with pip:
pip install behave
To update an already installed behave version, use:
pip install -U behave
现在来写一个小例子
Now make a directory called “features”. In that directory create a file called “tutorial.feature” containing:
Feature: showing off behave
Scenario: run a simple test
Given we have behave installed
When we implement a test
Then behave will test it for us!
Make a new directory called “features/steps”. In that directory create a file called “tutorial.py” containing:
from behave import *
@given('we have behave installed')
def step_impl(context):
pass
@when('we implement a test')
def step_impl(context):
assert True is not False
@then('behave will test it for us!')
def step_impl(context):
assert context.failed is False
然后运行behave
% behave
Feature: showing off behave # features/tutorial.feature:1
Scenario: run a simple test # features/tutorial.feature:3
Given we have behave installed # features/steps/tutorial.py:3
When we implement a test # features/steps/tutorial.py:7
Then behave will test it for us! # features/steps/tutorial.py:11
1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined
Features
最低要求是
The minimum requirement for a features directory is:
features/
features/everything.feature
features/steps/
features/steps/steps.py
更完整更复杂的结构是:
features/
features/signup.feature
features/login.feature
features/account_details.feature
features/environment.py
features/steps/
features/steps/website.py
features/steps/utils.py
Feature File
一个Feature File
A feature file has a natural language format describing a feature or part of a feature with representative examples of expected outcomes. They’re plain-text (encoded in UTF-8) and look something like:
Feature: Fight or flight
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
Scenario: Weaker opponent
Given the ninja has a third level black-belt
When attacked by a samurai
Then the ninja should engage the opponent
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
The “Given”, “When” and “Then” parts of this prose form the actual steps that will be taken by behave in testing your system. These map to Python step implementations. As a general guide:
Given we put the system in a known state before the user (or external system) starts interacting with the system (in the When steps). Avoid talking about user interaction in givens.
When we take key actions the user (or external system) performs. This is the interaction with your system which should (or perhaps should not) cause some state to change.
Then we observe outcomes.
You may also include “And” or “But” as a step - these are renamed by behave to take the name of their preceding step, so:
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
And fall off a cliff
In this case behave will look for a step definition for "Then fall off a cliff"
.
Sometimes a scenario should be run with a number of variables giving a set of known states, actions to take and expected outcomes, all using the same basic actions. You may use a Scenario Outline to achieve this:
Scenario Outline: Blenders
Given I put in a blender,
when I switch the blender on
then it should transform into
Examples: Amphibians
| thing | other thing |
| Red Tree Frog | mush |
Examples: Consumer Electronics
| thing | other thing |
| iPhone | toxic waste |
| Galaxy Nexus | toxic waste |
behave will run the scenario once for each (non-heading) line appearing in the example data tables.