用Cucumber JVM编写BDD测试
Cucumber JVM是編寫BDD測試的出色工具。在本文中,我想對Cucumber JVM的BDD進行介紹。
讓我們開始吧…
什么是BDD?
簡而言之,BDD試圖解決“通過示例理解需求”的問題
BDD工具
有許多用于BDD的工具,有趣的是,您可以在列表中找到很多蔬菜名稱:)黃瓜,菠菜,生菜,JBehave,Twist等。這些黃瓜中的這些簡單易用。
黃瓜JVM
Cucumber用Ruby編寫,而Cucumber JVM是適用于Java,Scala,Groovy,Clojure等流行JVM語言的Cucumber的實現。
黃瓜堆棧
我們使用“無處不在”語言編寫功能和場景,然后使用步驟定義和支持代碼來實現它們。
功能文件和小黃瓜
首先,您需要編寫一個.feature文件。通常,功能文件以Feature關鍵字開頭,后跟Scenario 。 每個方案都包含多個步驟。 黃瓜為此使用了黃瓜。 Gherkin是一種商業可讀的特定于域的語言,可讓您描述軟件的行為而無需詳細說明如何實現該行為。
例:
Feature: Placing bets Scenario: Place a bet with cash balance Given I have an account with cash balance of 100 When I place a bet of 10 on "SB_PRE_MATCH" Then the bet should be placed successfully And the remaining balance in my account should be 90如您所見,特征文件更像是帶有小黃瓜關鍵字的口語,例如Feature,Scenario,Give,When,When,And和#。
步驟定義
在完成了具有不同場景的功能文件后,下一步就是通過編寫步驟定義使場景栩栩如生。 Cucumber使用正則表達式將步驟與實際步驟定義進行映射。 可以使用您選擇的JVM語言編寫步驟定義。 映射步驟定義時,將忽略關鍵字。 因此,參考上面的示例功能,我們將必須為所有四個步驟編寫步驟定義。 使用IDE插件為您生成存根。
import cucumber.api.java.en.And; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; public class PlaceBetStepDefs { @Given("^I have an account with cash balance of (\\d+) $") public void accountWithBalance(int balance) throws Throwable { // Write code here that turns the phrase above into concrete actions //throw new PendingException(); } @When("^I place a bet of (\\d+) on \"(.*?)\"$") public void placeBet(int stake, String product) throws Throwable { // Write code here that turns the phrase above into concrete actions // throw new PendingException(); } @Then("^the bet should be placed successfully$") public void theBetShouldBePlacedSuccessfully() throws Throwable { // Write code here that turns the phrase above into concrete actions //throw new PendingException(); } @And("^the remaining balance in my account should be (\\d+)$") public void assertRemainingBalance(int remaining) throws Throwable { // Write code here that turns the phrase above into concrete actions //throw new PendingException(); } }支持代碼
下一步是使用支持代碼來支持您的步驟定義。 例如,您可以進行REST調用以執行該步驟,或者進行數據庫調用或使用諸如selenium之類的Web驅動程序。 這完全取決于實施情況。 獲得響應后,您可以使用期望的結果對其進行斷言,或者將其映射到您的域對象。 例如,您可以使用Selenium Web驅動程序來模擬登錄到站點:
protected WebDriver driver; @Before("@startbrowser") public void setup() { System.setProperty("webdriver.chrome.driver", "C:\\devel\\projects\\cucumberworkshop\\chromedriver.exe"); driver = new ChromeDriver(); } @Given("^I open google$") public void I_open_google() throws Throwable { driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.google.co.uk"); }表現場景
黃瓜提供了更多選項,可以更好地組織您的方案。
- 背景 –使用它來定義所有方案通用的步驟
- 數據表 –您可以表格式寫入輸入數據
- 方案大綱-方案的占位符,可以對稱為示例的一組數據執行。
- 標簽和子文件夾來組織您的功能-標簽更像是文檔的便箋。
依賴注入
通常,您可能不得不將一步創建的信息傳遞給另一步。 例如,您在第一步中創建一個域對象,然后在第二步中需要使用它。 做到這一點的干凈方法是通過依賴注入。 Cucumber為主要的DI容器(例如Spring,Guice,Pico等)提供模塊。
執行黃瓜
在IntelliJ IDE上運行Cucumber非常容易。 它也可以與您的構建系統集成。 您還可以使用其他選項控制要運行的測試。
報告選項
有許多可用于報告的插件。 例如,您可以將Master Thought插件用于報告。
參考文獻
《 Cucumber for Java》一書 –這是一本非常好的書,這是您入門所需的一切。 文檔 GitHub鏈接那就是所有的人。 希望你喜歡它。 圣誕快樂! 請享用。
翻譯自: https://www.javacodegeeks.com/2015/12/writing-bdd-tests-cucumber-jvm.html
總結
以上是生活随笔為你收集整理的用Cucumber JVM编写BDD测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: netbeans7.4_NetBeans
- 下一篇: 360安全卫士和qq电脑管家哪个好用(3