小组Java

Cucumber 是一个能够理解用普通语言 描述的测试用例的支持行为驱动开发(BDD)的自动化测试工具,用Ruby编写,支持Java和·Net等多种开发语言。

使用Cucmber可以很容易的把你的测试用例转化成java测试代码,这里主要介绍如何用在eclipse下使用Cucumber。

  • Eclipse下安装Cucumber plugin

运行Eclipse IDE ,点击 Help 菜单下的 “Install New Software”.

点击add 按钮,然后输入如下内容:

Name: cucumber

Location:http://cucumber.github.com/cucumber-eclipse/update-site

 

  •  创建一个cucumber sample

 1. 创建一个maven project

2. POM添加如下dependencies

<dependencies>

<dependency>

<groupId>info.cukes</groupId>

<artifactId>cucumber-java</artifactId>

<version>1.2.4</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>info.cukes</groupId>

<artifactId>cucumber-junit</artifactId>

<version>1.2.4</version>

<scope>test</scope>

</dependency>

</dependencies>

 

3. 在当前的project下创建Feature目录,并创建sample.feature 文件

Feature: Belly

 

Scenario Outline: a few cukes

Given I have <cukes> cukes in my belly

When I wait <hour> hour

Then my belly should <sound>

 

Examples:

| cukes  | hour   | sound    |

|    30    |  1         |  silent    |

|    42    |  2         |  growl   |

4.  Build maven file, 下载lib包

5.  在/src/test/java下面创建一个class文件

import org.junit.runner.RunWith;

 

import cucumber.api.CucumberOptions;

import cucumber.api.junit.Cucumber;

 

 

@RunWith(Cucumber.class)

@CucumberOptions(

features = “Feature”

,glue={“stepDefinition”}

)

public class TestRunner {

 

}

6. 运行这个test class,邮件点击run as->junit application,你会得到如下运行结果:

2 Scenarios (_[33m2 undefined_[0m)

6 Steps (_[33m6 undefined_[0m)

0m0.000s

 

 

You can implement missing steps with the snippets below:

 

@Given(“^I have (\d+) cukes in my belly$”)

public void i_have_cukes_in_my_belly(int arg1) throws Throwable {

// Write code here that turns the phrase above into concrete actions

throw new PendingException();

}

 

@When(“^I wait (\d+) hour$”)

public void i_wait_hour(int arg1) throws Throwable {

// Write code here that turns the phrase above into concrete actions

throw new PendingException();

}

 

@Then(“^my belly should silent$”)

public void my_belly_should_silent() throws Throwable {

// Write code here that turns the phrase above into concrete actions

throw new PendingException();

}

 

@Then(“^my belly should growl$”)

public void my_belly_should_growl() throws Throwable {

// Write code here that turns the phrase above into concrete actions

throw new PendingException();

}

7. 创建你的implement class, scr/main/java 下创建class,Belly

public class Belly {

 

private int cukes;

 

public void eat(int cukes) {

this.cukes = cukes;

}

 

public String getSound(int waitingTime) {

if (cukes > 41 && waitingTime >= 1) {

return “growl”;

} else {

return “silent”;

}

}

8. 你在/src/test/java下创建包“stepDefinition”,然后在这个包下面创建一个class,并把上次运行的junit里面提示的内容拷贝到这个class里面

package stepDefinition;

 

import Belly;

import cucumber.api.PendingException;

import cucumber.api.java.en.Given;

import cucumber.api.java.en.Then;

import cucumber.api.java.en.When;

import org.hamcrest.core.Is;

import org.junit.Assert;

 

public class Test_Steps {

 

private Belly belly;

private int waitingTime;

 

@Given(“^I have (\d+) cukes in my belly$”)

public void i_have_cukes_in_my_belly(int cukes) throws Throwable {

belly = new Belly();

belly.eat(cukes);

}

 

 

@When(“^I wait (\d+) hour$”)

public void i_wait_hour(int arg1) throws Throwable {

this.waitingTime =arg1;

}

 

@Then(“^my belly should (.*)$”)

public void my_belly_should_growl(String expectedSound) throws Throwable {

String actualSound = belly.getSound(waitingTime);

Assert.assertThat(actualSound, Is.is(expectedSound));

}

}

9.  然后再运行下TestRunner class,你会看到如下正确的执行结果。

2 收藏


直接登录