不寫程式就完成BDD (Behavior Driven Development)自動化測試工具 – bddfire

不寫程式就完成BDD (Behavior Driven Development)自動化測試工具 – bddfire

 

Image result for BDD

這篇文章主要介紹如何不用寫程式就完成 BDD的自動化測試,

開始介紹如何運用開源工具之前, 先介紹BDD測試的基本概念與執行時的困難點

這樣會更容易理解BDD測試所帶來的好處與該開源工具的優勢

 

什麼是BDD的測試方式?

簡單來說就是將每一個測試個案與測試用例依照 User Story的方式來書寫

  • Given : 某個條件下
  • When: 當使用者執行某些動作
  • Then: 預期的執行結果

如此一來整個測試用例的執行結果就會更趨近於原本客戶的訴求

達到 Acceptance Testing, 最終客戶為觀點的驗收測試

Image result for BDD

BDD困難點

問題來了, 為了要將測試個案寫成這樣的形式,畢竟還是需要對應的程式碼才能執行

  • Given : 某個條件下
  • When: 當使用者執行某些動作
  • Then: 預期的執行結果

因此會有下列過程:

  1.  寫下 user story ( 用英文描述的方式 Given, When, Then…)
  2. 編寫相對應的程式碼

 

不寫程式如何完成 BDD?

原則上BDD 有上述兩大步驟,

  1. 寫下 user story ( 用英文描述的方式 Given, When, Then…)
  2. 編寫相對應的程式碼

因此, 我們會利用一個開源框架 BddFire, 該開源框架已經定義好許多 user story與相對應的程式碼

我們只要將依據測試場景的需求增新修改必要的參數即可

舉例來說, 對於瀏覽器的操作該開源框架提供下列操作

我們只要針對 “” 的內容加以修改符合該測試場景

Browser Actions

   When I navigate to another page ""
   When I go forward using browser
   When I go back using browser
   When browser is refreshed
   When I have switched to another window ""
   When browser window is resized to width "" and height ""
   When browser window is maximized
   When I closed browser window
   When page is zoomed in
   When page is zoomed out
   When I scrolled to element with selector ""
   When element with selector is hovered over

User Actions

   Given  I am on “url”
   When I fill in “element” with the text “text”
   When I select the “link” link
   When I select the "button" button
   When I click on link having text “text"
   When I check “checkbox”
   When I uncheck “checkbox”
   When I choose “radio button”
   When I select option “option” from the dropdown “dropdown”
   When I attach the file “file_path" to the field “locator”
   When I hover over the element “locator"

Assertions

   Then the page title is  “title"
   Then I see the element “element” with the text “text”
   Then page contains “element”
   Then I see element “element” “count” times
   Then page contains the link "link"
   Then page contains the button  “button”
   Then page contains the field “field”
   Then page contains the table “table”
   Then the element “element” is checked
   Then the element “element” is unchecked
   Then the page contains the following content "content"

 

除此之外, BddFire還提供API測試的相關操作與驗證

Given I send a GET request for "<your_uri>"

   Given I send a GET/PUT/POST/DELETE request for "<your_uri>" with the following: 
    """
   <your_payload>
    """

   Then the response should be JSON

   Then the response status code should be '200'

   Then the JSON response should have '<number_of_properties>' properties

   Then the JSON response has a "<id_of_property>" property

   Then the JSON "<id_of_property>" property equals "<value_of_property>" 

   Then the JSON response header has key "<header_name>" with value of "<header_name>" 

   Then /^the JSON response should be:
    """
     <your_json>
    """

 

當要考慮將測試個案採用 BDD user story 的方式時, 不訪可以參考BddFire https://github.com/Shashikant86/bddfire

範例程式

安裝好BDDfire 相關套件之後, 例如下列範例程式,將會執行下列步驟

1. 啟動瀏覽器瀏覽  www.google.com

2. Google搜尋頁面中找到搜尋輸入框 q,  輸入 bddfire google Search文字

3. 搜尋結果頁面會出現 Sign in的文字

[pastacode lang=”python” manual=”Feature%3A%20Google%20Search%20to%20explore%20BDDfire%0A%0A%0AScenario%3A%20View%20home%20page%0A%20%20Given%20I%20am%20on%20%22http%3A%2F%2Fwww.google.com%22%0A%20%20When%20I%20fill%20in%20%22q%22%20with%20the%20text%20%22bddfire%20Google%20Search%22%0A%20%20Then%20I%20should%20see%20%22Sign%20in%22″ message=”” highlight=”” provider=”manual”/]

 

參考

https://www.qatestingtools.com/compare-bdd-testing-tools

http://jbehave.org/

 

Leave a Reply

Your email address will not be published. Required fields are marked *