BDD簡介
BDD(Behavior Driven Development),即行為驅動開發。BDD是ATDD驗收測試驅動開發的一種升級版,根據明確的預期行為(一組場景)來進行開發和測試。
這種預期行為使用一種特定規的范格式進行描述,旨在消除需求從客戶,到產品經理,再到開發/測試時的信息失真問題。
如一個后台登錄功能,可以描述如下。
Feature: 登錄功能
Scenario: 正常登錄
Given 用戶名 admin 密碼 123456
When 打開后台頁面
And 輸入用戶名
And 輸入密碼
And 點擊登錄按鈕
Then 頁面中應不包含 您輸入的帳號信息不正確
Scenario: 賬號為空登錄
Given 用戶名為空 密碼 123456
When 打開后台頁面
And 輸入用戶名
And 輸入密碼
And 點擊登錄按鈕
Then 頁面中應包含 您輸入的帳號信息不正確
Scenario: 密碼為空登錄
Given 用戶名 admin 密碼為空
When 打開后台頁面
And 輸入用戶名
And 輸入密碼
And 點擊登錄按鈕
Then 頁面中應包含 您輸入的帳號信息不正確
產品整理完用戶需求,梳理出如上明確的行為場景,開發根據這些行為場景進行開發,測試根據這些場景進行測試,也可以擴展更多的場景。
在BDD中,常用的概念如下
- Epic: 史詩,一般指一個版本或一批功能更新
- Feature: 特性,一般指一個功能點,如登錄,添加商品,查詢商品等,在測試中對應一個測試套件
- Scenario:場景,即Story,一個明確的場景,對應一個測試用例
- Step: 步驟,測試步驟有Given/When/Then三種
- Given: 假設,給定數據或前置條件,對應測試中的setup
- When: 當...時,對應一個測試步驟
- Then: 然后,即期望結果,對應一個測試斷言
- And: 同上,可以用於Given/When/Then后
行為驅動多用於UI層的測試。因此BDD框架的自動化一般結合Selenium使用。
behave基本使用
常用的BDD框架有
最出名的BDD框架應該是Cucumber,使用Ruby語言,Python中常用的有behave、pytest-bdd和lettuce,其中lettuce不支持Python3。
behave是比較易用的一款BDD測試框架,github地址為:https://github.com/behave/behave
使用BDD進行測試的基本步驟為:
使用方法如下,以百度搜索為例。
安裝behave和selenium
- 使用PyCharm新建一個空白項目behave_bdd
- 安裝behave和selenium:
pip install behave
pip install selenium
編寫場景文件
- 在目錄中新建一個名為features的目錄,咋features中新建一個baidu.feature的文件,內容如下。
Feature: 百度搜索
Scenario: 搜索關鍵詞
Given 關鍵詞 behave
When 打開百度頁面
And 輸入關鍵詞
And 點擊百度一下按鈕
Then 頁面標題中應包含關鍵詞
實現場景步驟
在features中新建steps目錄,在steps目錄中新建baidu_steps.py
- 從behave中導入given,when,then等關鍵字
- 對應場景的每個步驟編寫一個step_impl(context)函數,上方有對應的關鍵字裝飾器匹配對應的場景步驟,如
from behave import given, when, then
@given('關鍵詞 {keyword}') # 對應步驟 Given 關鍵詞 behave, 參數放在{}中
def step_impl(context, keyword): # context是上下文對象,有參數的話,加上對應參數
context.keyword = keyword # 將參數綁定上下文對象,以便其他步驟使用
其他步驟一一實現即可,完整代碼如下
from behave import given, when, then
from selenium import webdriver
from time import sleep
@given('關鍵詞 {keyword}') # 對應步驟 Given 關鍵詞 behave, 參數放在{}中
def step_impl(context, keyword): # context是上下文對象,有參數的話,加上對應參數
context.keyword = keyword # 將參數綁定上下文對象,以便其他步驟使用
@when('打開百度頁面')
def step_impl(context):
context.driver = driver = webdriver.Chrome() # 同樣綁定上下文對象
driver.implicitly_wait(10)
driver.get('https://www.baidu.com')
@when('輸入關鍵詞')
def step_impl(context):
context.driver.find_element('id', 'kw').send_keys(context.keyword)
@when('點擊百度一下按鈕')
def step_impl(context):
context.driver.find_element('id', 'su').click()
sleep(0.5)
@then('頁面標題中應包含關鍵詞')
def step_impl(context):
assert context.keyword in context.driver.title
測試場景
打開命令行,cd到項目所在目錄,輸入behave
運行即可,項目結構及效果如下。
結合allure生成報告
安裝allure-behave:pip install allure-behave
behave -f allure_behave.formatter:AllureFormatter -o allure_data
其中
-o
是指定allure報告數據輸出目錄
allure_data中生成的是json格式的報告數據,需要使用allure命令行工具才能轉為html報告,
allure命令行工具,mac系統可以使用brew安裝
allure命令行工具依賴JAVA,需要配置JAVA_HOME
brew tap qameta/allure
brew install allure
Windows系統可以下載allure-commandline源碼,v2.13.6下載鏈接
解壓后,將其中的bin目錄配置到環境變量Path中即可。
allure命令行工具安裝或配置好后,使用以下命令生成html報告。
allure generate allure_data -o allure_html
參考:
- Behave GitHub: https://github.com/behave/behave
- Allure官方文檔:https://docs.qameta.io/allure/#_behave