網站首頁 編程語言 正文
behave是python語言的行為驅動開發,全稱:Behavior-driven development,簡稱BDD。
BDD框架
BDD即行為驅動開發(Behavior Driven Development),其特點為:
- 通過自然語言來定義系統行為
- 從功能使用者的角度,編寫需求場景
- 鼓勵軟件項目中的開發者、非技術人員以及商業參與者之間的協作。協作的核心是通過活文檔或者說場景文檔來協作,該文檔既是測試用例文檔,也是需求定義文檔
- 常見的BDD框架有Behave,Cucumber等
它是一種敏捷軟件開發技術,它鼓勵軟件項目中的開發人員、QA和非技術或業務參與者之間進行協作。
python behave的官方網址:
https://behave.readthedocs.io/en/latest/gherkin.html#gherkin-feature-testing-language
最初由Dan North命名,并于2009年對BDD給出了如下定義:
“BDD是第二代、由外而內、基于拉動、多利益相關者、多規模、高度自動化、敏捷的方法。
它描述了一個與定義明確的輸出交互的循環,從而交付了重要的工作、測試軟件。”
BDD并不會描述或定義軟件怎么做,而是能做了什么。最終通過python代碼進行驗證。
首先用pycharm創建項目Python-Behave,python環境選擇Virtualenv,接著安裝behave包。
在項目Python-Behave下創建一個名為“features”的目錄(這個目錄名稱是隨意的),可以在這個目錄下定義所有behave的文件結構。
在features目錄下創建一個“.feature”文件,這是一種叫作“Gherkin”的語言。它對非技術人員比較友好,可以使用自然語言編寫。
“.feature”文件有兩個用途:文檔和自動化測試。一句話,在“.feature”里編寫測試場景。
很多文章提到Gherkin語言必須用pycharm專業版才能編寫,但是我親測用pycharm社區版也是可以編寫的。
“.feature”文件的結構:
主體由多個場景Scenario組成,可以選用Background和tag進行約束。
feature文件的一個基本的結構為:
Feature: feature name
Scenario: some scenario
Given some condition
When some operation
Then some result is expected
- Feature是功能名稱
- Scenario是場景描述
- Given是此場景下的前提條件
- When是此場景下的操作步驟
- Then是此場景下的預期結果
如果有多個測試場景呢,就再加一個Scenario。如果Scenario下的Given/When/Then有多個呢?
可以用And或But表示。
所以
Scenario: Multiple Givens
Given one thing
Given another thing
Given yet another thing
When I open my eyes
Then I see something
Then I don't see something else
也可以這樣寫作
Scenario: Multiple Givens
Given one thing
And another thing
And yet another thing
When I open my eyes
Then I see something
But I don't see something else
這種方式閱讀會更流暢
當然,上面只是一個簡單的feature結構,更復雜一點的,比如說這樣:
@tags @tag
Feature: feature name
description
further description
Background: some requirement of this test
Given some setup condition
And some other setup action
Scenario: some scenario
Given some condition
When some action is taken
Then some result is expected.
Scenario: some other scenario
Given some other condition
When some action is taken
Then some other result is expected.
Scenario: ...
Background由一系列類似于Scenario的步驟組成,它的目的是為Scenario添加上下文,在Scenario執行之前執行Background,用于設置Scenario的前提條件。
Scenario由一系列步驟組成,它描述了Feature的一種場景。如果一種場景有多種情況呢?
比如登錄這個Scenario,不同的登錄名和密碼,登錄的結果不同。這種情況可以不需要寫多個Scenario描述,可以使用Scenario Outline和Examples來完成。
Scenario Outline: Blenders
Given I put <thing> in a blender,
when I switch the blender on
then it should transform into <other thing>
Examples: Amphibians
| thing | other thing |
| Red Tree Frog | mush |
Examples: Consumer Electronics
| thing | other thing |
| iPhone | toxic waste |
| Galaxy Nexus | toxic waste |
在上面這個例子中,用Scenario Outline描述“Blenders”場景,用多個Examples表示場景的多種類型,每個Examples下可以包含多種情況。不同情況的列舉在Scenario Outline用符號“<key>”表示,在Examples中用key列舉
Scenario由一系列步驟組成,步驟由關鍵字“Given”、“When”、“Then”、“And”、“But”為開頭。Python Behave實際運行的也是這些步驟。
具體實現是通過此項目下的steps目錄里的“.py”文件實現所有的Scenario的步驟。這里要注意,steps目錄名是確定的不能改變的,但是里面的py文件名是隨意的。
python behave項目的執行方式也并不是通過運行steps目錄里的py文件,而是通過命名behave調用“.feature”文件,映射到py文件里的步驟下的函數,執行這些函數。
步驟描述要盡量簡潔,但有時會附帶一些文本text或表格table。如果python代碼需要使用這些text或table,則可以通過訪問屬性“context.text”或”context.table“來使用。
Text:
Scenario: some scenario
Given a sample text loaded into the frobulator
"""
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.
"""
When we activate the frobulator
Then we will find it similar to English
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"
Python中訪問:
@given('a set of specific users')
def step_impl(context):
for row in context.table:
model.add_user(name=row['name'], department=row['department'])
這個表格在python中是這樣的數據類型:
[{"name":"Barry", "department":"Beer Cans"},{"name":"Pudey", "department":"Silly Walks"},{"name":"Two-Lumps", "department":"Silly Walks"}]
認真體會下!!!
Tags:
tags用于標記Feature、Scenario或Scenario Outlook,可以選擇性的只執行被標記的。
例如:
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
@slow
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
如果只想執行Scenario: Weaker opponent,可以對它進行標記為@slow,然后運行“behave --tags=slow”。
如果想執行除標記@slow外的其他場景,可以運行“behave --tags=“not slow””。
組合使用tags標簽:
–tags=“wip or slow”,選擇所有標記為wip或slow的case
–tags=“wip and slow”,選擇所有標記為wip和slow的case
以上講的是如何用“.feature”文件編寫所有測試場景。雖然是通過命令behave xxx.feature觸發,但實際的執行操作是在steps目錄下的“.py”實現。
所以,要如何把“.feature”里的所有步驟映射到“.py”中,還必須按照順序不能出錯,這就成為了一個關鍵。
假設給定一個Senario:
Scenario: Search for an account
Given I search for a valid account
Then I will see the account details
記住,只對所有的步驟按順序實現,并不會對Scenario進行映射。
在python中實現如下:
@given('I search for a valid account')
def step_impl(context):
context.browser.get('http://localhost:8000/index')
form = get_element(context.browser, tag='form')
get_element(form, name="msisdn").send_keys('61415551234')
form.submit()
@then('I will see the account details')
def step_impl(context):
elements = find_elements(context.browser, id='no-account')
eq_(elements, [], 'account not found')
h = get_element(context.browser, id='account-head')
ok_(h.text.startswith("Account 61415551234"),
'Heading %r has wrong text' % h.text)
按照“.feature”中的步驟的順序,將步驟的前面的關鍵字,在python中用裝飾器匹配(@give、@when、@then),()里面是步驟描述。
如果是And或But,在python中用它們被重命名以前的關鍵字。也就是說,在python中,不可能有@and或@but。
然后在裝飾器下方定義函數,函數名隨意。函數體就是步驟的具體實現。
如果你想在某個步驟里執行另一個步驟,只需要用context對象調用execute_steps()函數,里面傳入被調用步驟。
@when('I do the same thing as before')
def step_impl(context):
context.execute_steps('''
when I press the big red button
and I duck
''')
如果你想把“.feature”的步驟上的信息傳遞到“.py”中,可以在py文件中用中括號加關鍵字表示“{key}”。
比如訪問百度首頁:
Scenario: 訪問百度網頁
Given: 打開網頁
When: 輸入網址www.baidu.com
Then:顯示百度首頁
關于其中輸入百度網址,在python中就可以這樣實現:
@when(輸入網址{baidu})
def step_imp1(context, baidu):
context.driver.get(baidu)
Context:
聰明的你一定注意到了,在py文件中每個步驟下的函數內第一個參數是context,它是Feature或Scenario的實例化,可以用來傳遞信息。
如何傳遞呢,這里我們暫且按下不表,先思考一個問題。
如果我有一個場景,打開百度網頁,輸入關鍵字搜索,然后查看搜索結果。
在feature文件中如何描述,這個我們應該已經學會了。在py文件中如何實現,我們也不陌生了。
Scenario: 打開百度網頁并輸入關鍵字
Given: 打開百度網頁http://www.baidu.com
When: 輸入關鍵字大美女
Then:驗證返回的搜索結果標題是大美女_百度搜索
@given(打開百度網頁{baidu})
def step_imp1(context, baidu):
context.driver.get(baidu)
@when(輸入關鍵字{keyword})
def step_imp1(context, keyword):
context.driver.find_element_by_xpath('//*[@id="kw"]').send_keys(keyword)
...
后面我就不寫了。現在只看given的步驟,你應該就能發現問題,context.driver哪來的,從已知的上下文中并沒有相關信息。
如果你再仔細的思考一下,就會發現這里少了一步,我們在打開百度網站前,是不是應該先打開瀏覽器,然后才是輸入百度網址。
如果你有多個關于網頁搜索的場景,你是不是應該每次執行Scenario前都要打開瀏覽器,執行完畢關閉瀏覽器。這個操作類似于python unittester中的setup()和teardown()的用法。
那python behave框架中,對于操作前和操作后的前置條件和后置條件,是放在了“environment.py”文件中定義。
它有以下幾種:
- before_step(context, step)和after_step(context, step),每一步之前和之后運行
- before_scenario(context, scenario)和after_scenario(context, scenario),每個場景運行之前和之后運行
- before_feature(context, feature)和after_feature(context, feature),每個feature文件執行之前和之后運行
- before_tag(context, tag)和after_tag(context, tag),每個標簽調用之前和之后運行
- before_all(context)和after_all(context),整個behave之前和之后運行
那上面的例子中,在environment.py中實現打開和關閉瀏覽器的操作,要這樣實現:
# environment.py
from selenium import webdriver
def before_scenario(context, scenario):
context.driver = webdriver.Chrome(r"/usr/local/bin/chromedriver")
def after_scenario(context, scenario):
context.driver.close()
前置條件和后置條件在environment.py中用上面列舉的函數直接定義,函數名和形參必須符合規范。
可以看出,如果environment.py中的信息、變量或對象需要在steps中的py文件中被使用,可以用context來存儲。
這里就把打開的瀏覽器對象賦值給了context下定義的driver屬性,然后在py文件中直接可以使用context.driver,就相當于使用這個瀏覽器了。
注意注意,environment.py文件在python behave項目中的位置是哪里?是steps目錄中嗎?
不是的,environment.py文件是和“.feature”文件、steps目錄并列在同一目錄下的。而且它的名稱必須是environment.py。
現在回頭看下一個完整的python behave項目的最低結構要求:
+--features/
| +--steps/ # -- Steps directory
| | +-- *.py # -- Step implementation or use step-library python files.
| +-- *.feature # -- Feature files.
更復雜的目錄為:
+-- features/
| +-- steps/
| | +-- website_steps.py
| | +-- utils.py
| |
| +-- environment.py # -- Environment file with behave hooks, etc.
| +-- signup.feature
| +-- login.feature
| +-- account_details.feature
最后,如何執行behave的程序?
不是執行的steps目錄里的py文件,而是通過cmd打開命令行窗口,執行:“behave xxx.feature”。
這樣也很麻煩,有沒有一種方式可以在py文件的主入口里執行"behave xxx.feature",這樣可以封裝成exe程序,雙擊運行exe,即可運行behave。
在python behave項目目錄下創建一個py文件(也就是和feature文件、steps目錄文件在同一級下),命名為main.py,然后寫一個主入口程序:
# main.py
from behave.__main__ import main as behave_main
import os
if __name__ == "__main__":
# 獲取main.py的當前目錄
featurefile_dir = os.path.dirname(os.path.realpath(__file__))
# 獲取feature文件的絕對路徑
featurefile_path = os.path.join(featurefile_dir, "Behave.feature")
# 用behave.__main__下的main函數,傳入feature文件路徑,實現behave xxx.feature相同的效果
# 但是這里有個主要點,傳入feature文件的路徑不能是以這樣“\”或這樣“\\”的表示方式,必須要改成這種“/”
featurefile_path = featurefile_path.replace("\\", "/")
behave_main(featurefile_path)
# 或者你不轉成“\”,那么你就不能直接傳入feature文件路徑了,必須把文件路徑放入一個list中,然后把list傳入
# 如果你傳入list,相當于傳入多個參數,這時候除了文件路徑,還可以傳入tags參數,以執行標記的功能或場景
cmd_order = []
cmd_order.append(featurefile_path) # 把feature文件路徑添加為list的第一個元素
cmd_order.append("-t @slow") # 把tag標簽添加為list的第二個元素
behave_main(cmd_order) # 傳入參數列表
原文鏈接:https://blog.csdn.net/wjz110201/article/details/125737953
相關推薦
- 2022-09-24 一文詳解Golang協程調度器scheduler_Golang
- 2022-08-24 K8S之StatefulSet有狀態服務詳解_云其它
- 2023-02-09 MongoDB中aggregate()方法實例詳解_MongoDB
- 2023-10-17 npm 下載依賴項顯示proxy代理錯誤network ‘proxy‘ config is set
- 2022-06-04 Python數據處理的三個實用技巧分享_python
- 2022-05-23 C#中using語句的用法_C#教程
- 2022-04-08 WPF布局及布局容器介紹_基礎應用
- 2022-08-01 Flask-SQLALchemy基本使用方法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支