临沂网站制作平台,安徽中机诚建建设有限公司网站,移动网页设计总结,萌导航cucumber jvmCucumber JVM是编写BDD测试的出色工具。在本文中#xff0c;我想对Cucumber JVM的BDD进行介绍。 让我们开始吧… 什么是BDD#xff1f; 简而言之#xff0c;BDD试图解决“通过示例理解需求”的问题 BDD工具 有许多可用于BDD的工具#xff0c;有趣的是#… cucumber jvm Cucumber JVM是编写BDD测试的出色工具。在本文中我想对Cucumber JVM的BDD进行介绍。 让我们开始吧… 什么是BDD 简而言之BDD试图解决“通过示例理解需求”的问题 BDD工具 有许多可用于BDD的工具有趣的是您可以在列表中找到很多蔬菜名称Cucumber菠菜生菜JBehaveTwist等。在这些Cucumber中简单易用。 CucumberJVM Cucumber用Ruby编写而Cucumber JVM是适用于JavaScalaGroovyClojure等流行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关键字的口语例如FeatureScenarioGiveWhenWhenAnd和。 步骤定义 在完成了具有不同场景的功能文件后下一步就是通过编写步骤定义使场景栩栩如生。 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容器例如SpringGuicePico等提供模块。 执行Cucumber 在IntelliJ IDE上运行Cucumber非常容易。 它也可以与您的构建系统集成。 您还可以使用其他选项控制要运行的测试。 报告选项 有许多可用于报告的插件。 例如您可以将Master Thought插件用于报告。 参考资料 《 Cucumber for Java》一书 –这是一本非常好的书这是您入门所需的全部。 文档 GitHub链接那就是所有的人。 希望你喜欢它。 圣诞快乐 请享用。 翻译自: https://www.javacodegeeks.com/2015/12/writing-bdd-tests-cucumber-jvm.htmlcucumber jvm