Python+Selenium-BDD框架之behave


(查看behave具體教程可以訪問官網: http://pythonhosted.org/behave/

 

1.安裝behave

 安裝好python后,使用 pip install behave命令安裝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 
    When I input letter y 
    Then the inputed letter is Equal with y

 wordcheck.py內容如下:

__author__ = 'Amy' 
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): 
context.TargetLetter = TargetLetter     assert context.letter is context.TargetLetter

 

3.在cmd里面運行

 

4.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         |

結果如下:

 

5.使用table

在場景中可以一個表格作為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 | 
 """
This is a table,and this line will be displayed in result,it is context.text attribute
"""  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.

before_all(context), after_all(context)

These run before and after the whole shooting match.

下面是一個簡單的例子,大家可以根據自己的需要定義方法:

# coding:utf-8 
__author__ = 'Amy' 
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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM