cucumber jvm_用Cucumber JVM编写BDD测试

cucumber jvm

Cucumber JVM是编写BDD测试的出色工具。在本文中,我想对Cucumber JVM的BDD进行介绍。

让我们开始吧…

什么是BDD?



问题

简而言之,BDD试图解决“通过示例理解需求”的问题

bdd

BDD工具

有许多可用于BDD的工具,有趣的是,您可以在列表中找到很多蔬菜名称:)Cucumber,菠菜,生菜,JBehave,Twist等。在这些Cucumber中,简单易用。

CucumberJVM

Cucumber用Ruby编写,而Cucumber JVM是适用于Java,Scala,Groovy,Clojure等流行JVM语言的Cucumber的实现。

Cucumber堆栈

叠
我们使用“无处不在”语言编写功能和场景,然后使用步骤定义和支持代码来实现它们。

功能文件和小Cucumber

首先,您需要编写一个.feature文件。通常,功能文件以Feature关键字开头,后跟Scenario 。 每个方案都包含多个步骤。 Cucumber为此使用了Cucumber。 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

如您所见,特征文件更像是带有小Cucumber关键字的口语,例如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提供了更多选项,可以更好地组织您的方案。

依赖注入

通常,您可能不得不将一步创建的信息传递给另一步。 例如,您在第一步中创建一个域对象,然后在第二步中需要使用它。 做到这一点的干净方法是通过依赖注入。 Cucumber为主要的DI容器(例如Spring,Guice,Pico等)提供模块。

执行Cucumber

在IntelliJ IDE上运行Cucumber非常容易。 它也可以与您的构建系统集成。 您还可以使用其他选项控制要运行的测试。

报告选项

有许多可用于报告的插件。 例如,您可以将Master Thought插件用于报告。

参考资料

《 Cucumber for Java》一书 –这是一本非常好的书,这是您入门所需的全部。 文档 GitHub链接那就是所有的人。 希望你喜欢它。 圣诞快乐! 请享用。

翻译自: https://www.javacodegeeks.com/2015/12/writing-bdd-tests-cucumber-jvm.html

cucumber jvm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值