行爲驅動測試(1)

行爲驅動(1)
一、簡介
BDD:Behavior Driven Development
本質:用中文、英文或其餘語言編寫測試用例,而後去執行。每個語言經過裝飾器對應到一個測試用例步驟的執行。python

關鍵字:
(1)Feature:特性,將多個測試用例集合到一塊兒,對應於unittest中的test suite(測試用例集)
(2)Scenario:情景,用於描述一個用例,對應於unittest中的test case(測試用例)
(3)Given:若是,用例開始執行前的一個前置條件,相似於unittest中setup方法中的一些步驟
(4)When:當,用例開始執行時的一好些關鍵操做步驟,相似於unittest中的以test開頭的方法,好比執行一個單擊元素的操做
(5)Then:那麼,驗證結果,就是平時用例中中驗證步驟,好比assert方法
(6)And:和,一個步驟中若是存在多個Given操做,後面的Given能夠用And代替
(7)But:一個步驟中若是存在多個Then操做,第二個開始後面的Then能夠用But代替正則表達式

二、環境準備:
(1)安裝lettuce 命令行使命令:pip install lettuce 驗證是否安裝成功---進入python交互模式,導入lettuce:import lettuce
(2)導入時出現警報:安裝python-Levenshtein pip install python-Levenshtein
(3)安裝python-Levenshtein時報錯,須要安裝VCForPython27.msi(Visual C++環境)ide

三、第一個英文語言行爲驅動測試
測試邏輯:
(1)從lettuce全局變量命名空間world中取得一個整數
(2)計算該整數的階乘
(3)斷言計算結果的正確性函數

BDD實施步驟:
(1)Pycharm工具建立以下所示的目錄結構及文件:
lettuce\MyFirstBDD\features\-zero.feature -steps.py
前兩層目錄lettuce和MyFirstBDD不是必需的,並且名字能夠自定義;但features目錄是必須存在的,而且目錄名不能更改,執行行爲驅動腳本時,lettuce首先尋找的就是這個名字的目錄;features目錄下放的是執行場景文件(擴展名爲.feature的文件)和描述行爲的腳本文件(擴展名爲.py的文件)
(2)zero.feature用於完成lettuce工做流程的第一步,描述測試場景的行爲,具體內容以下:工具

Feature: Compute factorial
  In order to play with Lettuce
  As beginners
  We'll implement factorial

  Scenario: Factorial of 0
    Given I have the number 0
    When I compute its factorial
    Then I see the number 1

  Scenario: Factorial of 1
    Given I have the number 1
    When I compute its factorial
    Then I see the number 1

  Scenario: Factorial of 2
    Given I have the number 2
    When I compute its factorial
    Then I see the number 2

  Scenario: Factorial of 3
    Given I have the number 3
    When I compute its factorial
    Then I see the number 6

(3)steps.py裏面使用Python語言編寫行爲步驟,而且提供檢測執行結果代碼,具體內容以下:測試

#encoding=utf-8
from lettuce import *

#用於計算整數的階乘函數
def factorial(number):
    number = int(number)
    if number == 0 or number == 1:
        return 1
    else:
        return reduce(lambda x,y: x * y,range(1,number+1))

@step("I have the number (\d+)")
def have_the_number(step,number):
    #將經過正則表達式匹配的數字在於全局變量world中
    world.number = int(number)

@step("I compute its factorial")
def compute_its_factorial(step):
    #從全局變量world中取出匹配的數字
    #計算其階乘,並將結果再存回world中
    world.number = factorial(world.number)

@step("I see the number (\d+)")
def check_number(step,expected):
    #經過正則匹配到預期數字
    expected = int(expected)
    #斷言計算階乘結果是否等於指望
    assert world.number == expected,"Got %d" % world.number

(4)在PyCharm工具的Terminal(終端)中,將當前工做目錄切換到features目錄所在的目錄(操做方法如同Windows的CMD),而後執行命令「lettuce」啓動行爲驅動測試,也能夠直接在cmd中執行「lettuce」ui

四、經過類模式實現英文行爲驅動
測試邏輯:
(1)將測試步驟封裝到類中,並從全局變量中獲取須要的計算階乘的整數
(2)計算該整數的階乘
(3)斷言計算結果的正確性spa

BDD實施步驟:
(1)Pycharm工具建立以下所示的目錄結構及文件:
lettuce\ClassBDD\features\-zero.feature -steps.py
(2)zero.feature文件具體內容以下:命令行

Feature: Compute factorial
  In order to play with Lettuce
  As beginners
  We'll implement factorial

  Scenario: Factorial of 0
    Given I have the number 0
    When I compute its factorial
    Then I see the number 1

  Scenario: Factorial of 1
    Given I have the number 1
    When I compute its factorial
    Then I see the number 1

  Scenario: Factorial of 2
    Given I have the number 2
    When I compute its factorial
    Then I see the number 2

  Scenario: Factorial of 3
    Given I have the number 3
    When I compute its factorial
    Then I see the number 6

(3)steps.py文件具體內容以下:code

#encoding=utf-8
from lettuce import *

def factorial(number):
    number = int(number)
    if number == 0 or number == 1:
        return 1
    else:
        return reduce(lambda x,y: x * y,range(1,number+1))
@steps
class FactorialSteps(object):
    """Methods in exclude or starting with _ will not be considered as step"""

    exclude = ["set_number",'get_number']

    def __init__(self,environs):
        #初始全局變量
       self.environs = environs

    def set_number(self,value):
        #設置全局變量中的number變量的值
        self.environs.number = int(value)

    def get_number(self):
        #從全局變量中取出number的值
        return self.environs.number

    def _assert_number_is(self,expected,msg = "Got %d"):
        number = self.get_number()
        #斷言
        assert number == expected,msg % number

    def have_the_number(self,step,number):
        """I have the number (\d+)"""
        #上面的三引號引發的代碼必須寫,而且必須是三引號引發
        #表示從場景步驟中獲取須要的數據
        #並將獲取的數據存到全局變量number中
        self.set_number(number)

    def i_compute_its_factorial(self,step):
        number = self.get_number()
        #調用factorial方法進行階乘計算
        #並將結果在於全局變量中的number中
        self.set_number(factorial(number))

    def check_number(self,step,expected):
        """I see the number (\d+)"""
        #上面的三引號引發的代碼必須寫,而且必須是三引號引發
        #表示從場景步驟中獲取須要的數據以便斷言測試結果
        self._assert_number_is(int(expected))

FactorialSteps(world)
相關文章
相關標籤/搜索