推薦一款基於業務行為驅動開發(BDD)測試框架:Cucumber!

狂师發表於2024-06-26

大家好,我是狂師。

今天給大家介紹一款行為驅動開發測試框架:Cucumber

1、介紹

Cucumber是一個行為驅動開發(BDD)工具,它結合了文字描述和自動化測試指令碼。它使用一種名為Gherkin的特定語言來描述應用程式的行為,這種語言非常接近自然語言,使得非技術人員也能夠理解和參與測試。

知識擴充套件:

Gherkin語言是一種用於描述業務行為的領域特定語言(Domain Specific Language, DSL),它允許使用者不關注具體實現細節地描述軟體系統需要執行的操作。這種語言具有類似於自然語言的易讀性,使其成為業務人員和開發人員在編寫自動化測試用例時的理想選擇。Gherkin特別適用於Behavior Driven Development(BDD)方法,因為它能夠將業務需求轉換為清晰、易於理解和維護的測試步驟。

Gherkin它使用一組特殊的關鍵字來構建結構化和有意義的測試步驟。它的設計是為了描述而非直接執行,但它與Cucumber工具相結合,從而實現自動化的測試過程,它旨在讓不同背景的人(如業務人員、開發人員和測試人員)都能夠透過同一文件理解需求並達成共識。
一個典型的Gherkin測試指令碼由多個"steps"組成,每個步驟代表一個最小的測試單元。這些步驟可以組合成"Scenarios",進而構成"Features"。Feature檔案通常以"Feature:"開頭,而每個步驟則包含一系列的條件語句(如"Given"、"When"和"Then"),以及可能的其他關鍵字。

2、優缺點、適用場景

總的來說,Cucumber是一個強大的BDD工具,適用於需要與業務人員緊密合作的專案,可以促進團隊協作,減少測試指令碼的維護成本。然而,需要權衡其學習成本和執行速度。

適用場景:

  1. 針對需要與業務人員緊密合作的專案,Cucumber可以幫助編寫易於理解的測試用例,促進開發人員、測試人員和業務人員之間的溝通和協作。
  2. 對於需要頻繁更新和變更的專案,Cucumber的特性可以減少測試指令碼的維護成本,因為測試用例是用自然語言編寫的,不需要頻繁修改。
  3. 適用於Web應用程式、移動應用程式和API的自動化測試。

優點:

  1. 促進團隊協作:Cucumber測試用例使用自然語言編寫,使得開發人員、測試人員和業務人員可以更好地理解和參與測試。
  2. 減少維護成本:由於測試用例是用自然語言編寫的,不需要頻繁修改,可以減少測試指令碼的維護成本。
  3. 支援多種程式語言:Cucumber支援多種程式語言,如Java、Ruby、Python等,可以方便團隊根據自身技術棧進行選擇。

缺點:

  1. 學習成本較高:對於新手來說,學習Cucumber和Gherkin語言可能需要一些時間。
  2. 執行速度較慢:由於Cucumber測試用例是用自然語言編寫的,執行速度可能比較慢,特別是在大型專案中。

3、如何使用

3.1 Cucumber+Java實現Web應用程式自動化測試

當使用Cucumber進行Web應用程式自動化測試時,通常會結合Selenium WebDriver來實現。下面是一個簡單的示例,演示瞭如何使用Cucumber和Selenium WebDriver來編寫自動化測試用例。

假設我們要測試一個簡單的註冊頁面,包括輸入使用者名稱、密碼和確認密碼,然後點選註冊按鈕進行註冊。我們將使用Cucumber來編寫測試用例,使用Selenium WebDriver來模擬使用者在瀏覽器中的操作。

首先,我們需要在專案中引入Cucumber和Selenium WebDriver的相關依賴,並建立一個.feature檔案來編寫測試用例。假設我們的.feature檔名為registration.feature,內容如下:

Feature: User Registration
  Scenario: User can register with valid credentials
    Given User is on the registration page
    When User enters "john_doe" as username
    And User enters "password123" as password
    And User enters "password123" as confirm password
    And User clicks on register button
    Then User should be registered successfully

接下來,我們需要建立Step Definitions來實現.feature檔案中定義的步驟。假設我們將Step Definitions定義在一個名為RegistrationStepDefs.java的檔案中:

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class RegistrationStepDefs {
    WebDriver driver;

    @Given("User is on the registration page")
    public void userIsOnRegistrationPage() {
        System.setProperty("webdriver.chrome.driver", "path_to_chrome_driver");
        driver = new ChromeDriver();
        driver.get("url_of_registration_page");
    }

    @When("User enters {string} as username")
    public void userEntersUsername(String username) {
        driver.findElement(By.id("username")).sendKeys(username);
    }

    @When("User enters {string} as password")
    public void userEntersPassword(String password) {
        driver.findElement(By.id("password")).sendKeys(password);
    }

    @When("User enters {string} as confirm password")
    public void userEntersConfirmPassword(String confirmPassword) {
        driver.findElement(By.id("confirmPassword")).sendKeys(confirmPassword);
    }

    @When("User clicks on register button")
    public void userClicksOnRegisterButton() {
        driver.findElement(By.id("registerButton")).click();
    }

    @Then("User should be registered successfully")
    public void userShouldBeRegisteredSuccessfully() {
        // Add assertions to verify successful registration
        driver.quit();
    }
}

在這個示例中,我們使用了Cucumber的註解來定義測試步驟,並使用Selenium WebDriver來模擬使用者在瀏覽器中的操作。

最後,我們可以使用JUnit或TestNG來執行Cucumber測試用例。在Maven專案中,可以使用Maven Surefire外掛來執行Cucumber測試。

這只是一個簡單的示例,實際專案中可能會有更多複雜的測試場景和操作。但是,透過這個示例,你可以瞭解如何使用Cucumber和Selenium WebDriver來實現Web應用程式的自動化測試。

3.2 Cucumber+Python 實現Web應用程式自動化測試示例

當使用Cucumber和Python進行Web應用程式自動化測試時,我們通常會使用Behave作為BDD框架,結合Selenium WebDriver來實現。下面是一個簡單的示例,演示瞭如何使用Behave和Selenium WebDriver來編寫自動化測試用例。

首先,我們需要安裝必要的庫。在Python中,我們可以使用pip來安裝Behave和Selenium WebDriver:

pip install behave
pip install selenium

接下來,我們建立一個.feature檔案來編寫測試用例。假設我們的.feature檔名為registration.feature,內容如下:

Feature: User Registration
  Scenario: User can register with valid credentials
    Given User is on the registration page
    When User enters "john_doe" as username
    And User enters "password123" as password
    And User enters "password123" as confirm password
    And User clicks on register button
    Then User should be registered successfully

然後,我們需要建立Step Definitions來實現.feature檔案中定義的步驟。我們將Step Definitions定義在一個名為registration_steps.py的檔案中:

from behave import given, when, then
from selenium import webdriver

@given('User is on the registration page')
def user_is_on_registration_page(context):
    context.driver = webdriver.Chrome()
    context.driver.get('url_of_registration_page')

@when('User enters "{text}" as username')
def user_enters_username(context, text):
    username_input = context.driver.find_element_by_id('username')
    username_input.send_keys(text)

@when('User enters "{text}" as password')
def user_enters_password(context, text):
    password_input = context.driver.find_element_by_id('password')
    password_input.send_keys(text)

@when('User enters "{text}" as confirm password')
def user_enters_confirm_password(context, text):
    confirm_password_input = context.driver.find_element_by_id('confirmPassword')
    confirm_password_input.send_keys(text)

@when('User clicks on register button')
def user_clicks_on_register_button(context):
    register_button = context.driver.find_element_by_id('registerButton')
    register_button.click()

@then('User should be registered successfully')
def user_should_be_registered_successfully(context):
    # Add assertions to verify successful registration
    context.driver.quit()

在這個示例中,我們使用了Behave的註解來定義測試步驟,並使用Selenium WebDriver來模擬使用者在瀏覽器中的操作。

最後,我們可以使用命令列來執行Behave測試:

behave

這將執行我們編寫的測試用例,並輸出測試結果。

3.3 Cucumber+Python 實現API介面自動化測試示例

當使用Cucumber和Python進行API介面自動化測試時,我們通常會使用Behave作為BDD框架,結合requests庫來實現。下面是一個簡單的示例,演示瞭如何使用Behave和requests庫來編寫自動化測試用例。

首先,我們需要安裝必要的庫。在Python中,我們可以使用pip來安裝Behave和requests庫:

pip install behave
pip install requests

接下來,我們建立一個.feature檔案來編寫測試用例。假設我們的.feature檔名為api_test.feature,內容如下:

Feature: API Test
  Scenario: Verify API response
    Given API endpoint is "https://api.example.com/users"
    When User sends a GET request to the API
    Then API should respond with status code 200
    And API response should contain user data

然後,我們需要建立Step Definitions來實現.feature檔案中定義的步驟。我們將Step Definitions定義在一個名為api_test_steps.py的檔案中:

from behave import given, when, then
import requests

@given('API endpoint is "{url}"')
def set_api_endpoint(context, url):
    context.api_url = url

@when('User sends a GET request to the API')
def send_get_request(context):
    context.response = requests.get(context.api_url)

@then('API should respond with status code {status_code}')
def verify_status_code(context, status_code):
    assert context.response.status_code == int(status_code)

@then('API response should contain user data')
def verify_user_data_in_response(context):
    # Add assertions to verify user data in API response
    # For example, check if certain fields are present in the response
    pass

在這個示例中,我們使用了Behave的註解來定義測試步驟,並使用requests庫來傳送API請求並驗證API響應。

最後,我們可以使用命令列來執行Behave測試:

behave

這將執行我們編寫的測試用例,並輸出測試結果。

透過上述你可以瞭解如何使用Behave和requests庫來實現API介面的自動化測試,實際專案中可能會有更多複雜的測試場景和操作,具體可自行探究。

相關文章