Contents

Cucumber 简介

1. 简介

Cucumber 是一个 BDD(行为驱动开发)测试框架。

使用该框架编写具有不同输入/输出排列的重复场景可能非常耗时,难以维护,当然也令人沮丧。

Cucumber 提供了一个解决方案,通过使用Scenario Outline 和 Examples的概念来减少这种工作量。在下面的部分中,我们将尝试举一个例子,看看我们如何才能最大限度地减少这种努力。

如果您想了解更多有关该方法和 Gherkin 语言的信息,请查看这篇文章

2. 添加支持

要在一个简单的 Maven 项目中添加对 Cucumber 的支持,我们需要添加以下依赖项:

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>

来自 Maven Central 的有用链接:cucumber-junitcucumber-javahamcrest-library 由于这些是测试库,它们不需要与实际可部署的库一起提供——这就是为什么它们都是test范围的。

3. 一个简单的例子

让我们演示一种臃肿的方式和一种简洁的方式来编写特色文件。让我们首先定义我们要为其编写测试的逻辑:

让我们首先定义我们要为其编写测试的逻辑:

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

4. 定义 Cucumber 测试

4.1.定义特征文件

Feature: Calculator
  As a user
  I want to use a calculator to add numbers
  So that I don't need to add myself
  Scenario: Add two numbers -2 & 3
    Given I have a calculator
    When I add -2 and 3
    Then the result should be 1
   
  Scenario: Add two numbers 10 & 15
    Given I have a calculator
    When I add 10 and 15
    Then the result should be 25

如此处所见,这里有 2 种不同的数字组合来测试加法逻辑。除了数字之外,所有场景都完全相同。

4.2. “glue”代码

为了测试这些场景,我必须使用相应的代码定义每个步骤,以便将语句转换为功能代码:

public class CalculatorRunSteps {
    private int total;
    private Calculator calculator;
    @Before
    private void init() {
        total = -999;
    }
    @Given("^I have a calculator$")
    public void initializeCalculator() throws Throwable {
        calculator = new Calculator();
    }
    @When("^I add (-?\\d+) and (-?\\d+)$")
    public void testAdd(int num1, int num2) throws Throwable {
        total = calculator.add(num1, num2);
    }
    @Then("^the result should be (-?\\d+)$")
    public void validateResult(int result) throws Throwable {
        Assert.assertThat(total, Matchers.equalTo(result));
    }
}

4.3. 运行器类

为了集成特性和glue代码,我们可以使用 JUnit 运行器:

@RunWith(Cucumber.class)
@CucumberOptions(
  features = { "classpath:features/calculator.feature" },
  glue = {"com.blogdemo.cucumber.calculator" })
public class CalculatorTest {}

5. 使用场景大纲重写功能

我们在 4.1 节看到了。定义功能文件如何可能是一项耗时且更容易出错的任务。使用Scenario Outline可以将相同的功能文件减少到几行:

Feature: Calculator
  As a user
  I want to use a calculator to add numbers
  So that I don't need to add myself
  Scenario Outline: Add two numbers <num1> & <num2>
    Given I have a calculator
    When I add <num1> and <num2>
    Then the result should be <total>
  Examples: 
    | num1 | num2 | total |
    | -2 | 3 | 1 |
    | 10 | 15 | 25 |
    | 99 | -99 | 0 |
    | -1 | -10 | -11 |

将常规Scenario DefinitionScenario Outline进行比较时,不再需要在步骤定义中对值进行硬编码。在步骤定义本身中,值被参数替换为<parameter_name> 。

在 Scenario Outline 的末尾,使用示例*以管道分隔的表格格式定义值。

定义Examples的示例如下所示:

Examples:
  | Parameter_Name1 | Parameter_Name2 |
  | Value-1 | Value-2 |
  | Value-X | Value-Y |