官术网_书友最值得收藏!

Cucumber

Cucumber was originally a Ruby BDD framework. These days it supports several languages including Java. It provides functionality that is very similar to JBehave.

Let's see the same examples written in Cucumber.

The same as any other dependency we have used until now, Cucumber needs to be added to build.gradle before we can start using it:

dependencies { 
    testCompile 'info.cukes:cucumber-java:1.2.2' 
    testCompile 'info.cukes:cucumber-junit:1.2.2' 
} 

We will create the same steps as we did with JBehave, using the Cucumber way:

@Given("^I go to Wikipedia homepage$") 
public void goToWikiPage() { 
  open("http://en.wikipedia.org/wiki/Main_Page"); 
} 
 
@When("^I enter the value (.*) on a field named (.*)$") 
public void enterValueOnFieldByName(String value, 
String fieldName) { $(By.name(fieldName)).setValue(value); } @When("^I click the button (.*)$") public void clickButonByName(String buttonName) { $(By.name(buttonName)).click(); } @Then("^the page title contains (.*)$") public void pageTitleIs(String title) { assertThat(title(), containsString(title)); }

The only noticeable difference between these two frameworks is the way Cucumber defines steps text. It uses regular expressions to match variables types, unlike JBehave which deduces them from a method signature.

The steps code can be found in the WebSteps class in the https://bitbucket.org/vfarcic/tdd-java-ch02-example-web repository:

Let's see how the story looks when written using the Cucumber syntax:

Feature: Wikipedia Search 
 
  Scenario: TDD search on wikipedia 
    Given I go to Wikipedia homepage 
    When I enter the value Test-driven development on a field named search 
    When I click the button go 
    Then the page title contains Test-driven development 

Note that there are almost no differences. This story can be found in the wikipediaSearch.feature file in the https://bitbucket.org/vfarcic/tdd-java-ch02-example-web repository.

As you might have guessed, to run a Cucumber story, all you need to do is run the following Gradle task:

$> gradle testCucumber

The result reports are located in the build/reports/cucumber-report directory. This is the report for the preceding story:

Cucumber story execution report

The full code example can be found in the https://bitbucket.org/vfarcic/tdd-java-ch02-example-web repository.


For a list of languages supported by Cucumber or for any other details, visit  https://cukes.info/.

Since both JBehave and Cucumber offer a similar set of features, we decided to use JBehave throughout the rest of this book. There is a whole chapter dedicated to BDD and JBehave.

主站蜘蛛池模板: 垫江县| 临朐县| 肃宁县| 福泉市| 高碑店市| 渝中区| 江安县| 红安县| 上杭县| 大悟县| 乐亭县| 墨竹工卡县| 陇川县| 鹤壁市| 临城县| 聊城市| 油尖旺区| 兴业县| 奈曼旗| 从化市| 乌审旗| 涟水县| 黄龙县| 东乡县| 象州县| 隆子县| 唐海县| 黄山市| 怀仁县| 阿城市| 龙游县| 石首市| 海宁市| 苍溪县| 福泉市| 汕尾市| 兴山县| 磴口县| 宁蒗| 察隅县| 宜宾县|