本人學習的時候基本上是按照behave的tutorial教程一步步學習的,這篇文章就當Behave教程的翻譯版吧(*^__^*) 嘻嘻……。
1 安裝behave
安裝好python后,使用 pip install behave命令安裝behave
------ behave的官方網站: http://pythonhosted.org/behave/
2 一個簡單的實例
新建下面幾個文件,文件結構如下
firstCase
firstCase/wordcheck.feature
firstCase/steps
firstCase/steps/wordcheck.py
wordcheck.feature的內容如下:
Feature: word check
Scenario: Check letters
Given I have a letter y
When I input letter y
Then the inputed letter is Equal with y
wordcheck.py內容如下:
__author__ = 'helen'
from behave import *
@Given('I have a letter')
def step_impl(context):
pass
@When('I input letter {letter}')
def step_impl(context,letter):
context.letter = letter
@Then('the inputed letter is Equal with {TargetLetter}')
def step_impl(context,TargetLetter):
assert context.letter is context.TargetLetter
完成這兩個文件,我們的第一個case就完成了,下面通過cmd命令來執行它。
第一次運行,結果如下,說沒有TargetLetter參數。
檢查了wordcheck.py,發現@Then的部份忘記給TargetLetter賦值了,加上紅框部份。
第二次運行,執行通過,如下圖所示:
就這樣,一個簡單的實例就完成了,是不是好簡單(*^__^*) 嘻嘻……
3 features文件夾
我上面寫的例子中的 “firstCase” 文件夾就相當於現在說的features文件夾。
下面簡單說一下.feature和.py文件:
1) .feature文件是用於編寫測試場,你可以把各種場景和數據寫在里面,支持中文;
2) .py文件就是根據你寫的測試場景和數據來執行測試。
3) 最好.feature文件與.py文件一一對應。
如官網教程所說,簡單點的項目文件結構如下:
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
4 .feature文件介紹
一個.feature文件里可以寫多個Scenario,如下所示:
Feature: comparison
Scenario: comparison two words
Given I have two words
When input a and a
Then two words Equal
Scenario: comparison two numbers
Given I have two numbers
When I input number1 3 and number2 2
Then the first number big then the last number
下面對這些關鍵詞做一些簡單的解析:
1) Feature:功能名稱
2) Scenario:場景名稱
3) Given:給出測試前提條件;
4) when:相當我們的測試步驟;
5) Then:給出期望結果
有一個值得注意的是,你可以把“And”或“But”加到這些步驟中,如下:
Feature: word check
Scenario: Check letters
Given I have a letter
When I input letter y
But I just want to show the key word "But"
Then the inputed letter is Equal with y
And continue to next case
但是要注意,你在.py中要寫入相應的步驟處理方法,如下圖所示,你要加入“When I just want to show the key word "But"”和“Then continue to next case”相對應的方法,
4.1 Scenario Outlines
在測試同一個場景時,很多時候我們需要輸入各種各樣的數據來驗證不同的結果輸出,這時我們用Scenario Outlines就可以實現了。如下圖分別輸大小寫字母來驗證場景
Feature: word check
Scenario Outline: Check letters
Given I have a letter
When I input letter <keyword>
Then the inputed letter is Equal with <targetword>
Examples: Lowercase letters
|keyword|targetword|
|a |a |
|b |b |
Examples: Capital letters
|keyword|targetword|
|F |F |
|D |D |
執行結果如下:
4.2 在場景中添加注釋
在場景中,你可以通過"""來輸出更多注釋信息,這些注釋會成為Context的“.text”屬性值,如下圖分別在Given\When\Then中加入注釋:
Feature: word check
Scenario : show notes
Given I want to show some notes
"""
This is Given Notes
"""
When just want to show some notes
"""
This is When notes
"""
Then there is a word "Then" in the attribute ".text"
"""
This is Then notes
"""
然后在py文件中,我們最后判斷"Then"是否存在Context.text中,如下代碼:
__author__ = 'helen'
from behave import *
@Given('I want to show some notes')
def step_impl(context):
pass
@When('just want to show some notes')
def step_impl(context):
pass
@Then('there is a word "Then" in the attribute ".text"')
def step_impl(context):
assert context.text.find("Then")
最后執行通過,執行結果如下,在結果中輸出了注釋:
4.3 使用table
跟上面的context.text一樣,在場景中可以一個表格作為context.table屬性值,如下圖在Given中加入表格:
Feature: show the table
Scenario: some scenario
Given a set of specific users
| name | department |
| Barry | Beer Cans |
| Pudey | Silly Walks |
| Two-Lumps | Silly Walks |
When we count the number of people in each department
Then we will find two people in "Silly Walks"
But we will find one person in "Beer Cans"
6 Environment.py
Environment.py是個非常重要的文件,放在feature文件夾下,與.feature文件並列。下面是Environment.py中定義的一些方法:
before_step(context, step), after_step(context, step)
These run before and after every step.
before_scenario(context, scenario), after_scenario(context, scenario)
These run before and after each scenario is run.
before_feature(context, feature), after_feature(context, feature)
These run before and after each feature file is exercised.
before_tag(context, tag), after_tag(context, tag)
These run before and after a section tagged with the given name. They are invoked for each tag encountered in the order they’re found in the feature file. See controlling things with tags.
before_all(context), after_all(context)
These run before and after the whole shooting match.
下面是一個簡單的例子,大家可以根據自己的需要定義方法:
# coding:utf-8
__author__ = 'helen'
import sys
from behave import *
from selenium import webdriver
# 開始測試前,定義系統編碼為utf-8
def before_all(context):
reload(sys)
sys.setdefaultencoding('utf-8')
def before_feature(context):
context.driver = webdriver.Firefox()
def after_feature(context):
context.driver.quit()
7 通過標簽tag來控制測試執行
標簽以@開頭,如下示例:
Feature: find a look
@valid
Scenario: look up a book
Given I search for a valid book
Then the result page will include "success"
@invalid
Scenario: look up an invalid book
Given I search for a invalid book
Then the result page will include "failure"
執行的時候在behave 后面加上tag 標簽即可,如我只測試“valid”這個場景,那么就輸入“behave --tags=valid”,執行如下圖所示,一個場景跳過忽略:
如果你想執行若干個不同標簽的場景,你可以這么寫“behave --tags=valid,invalid”;
如果你想執行除了@invalid外的所有場景,你可以這么寫“behave --tags=-invalid”;
如果你要執行標簽包含了 “valid”和“invalid”兩個簽標的場景,你可以這么寫“behave --tags=valid --tags=invalid”
當然,tags在py文件中也起作用,例如
def before_feature(context):
if 'browser' in feature.tags:
context.driver = webdriver.Firefox()
def after_feature(context):
if 'browser' in feature.tags:
context.driver.quit()