1. Introduction
1.介绍
Cucumber is a BDD (Behavioral Driven Development) testing framework.
Cucumber是一个BDD(Behavioral Driven Development)测试框架。
Using the framework to write repetitive scenarios with different permutations of inputs/outputs can be quite time-consuming, difficult to maintain and of course frustrating.
使用框架编写具有不同输入/输出排列组合的重复方案可能相当耗时,难以维护,当然也会令人沮丧。
Cucumber came with a solution for reducing this effort by using the concept of Scenario Outline coupled with Examples. In the below section, we will try to take up an example and see how can we minimize this effort.
Cucumber提供了一个解决方案,通过使用Scenario Outline coupled with Examples的概念来减少这种努力。在下面的章节中,我们将尝试举一个例子,看看如何才能将这一工作降到最低。
If you want to read more about the approach and Gherkin language, have a look at this article.
如果你想阅读更多关于该方法和Gherkin语言的信息,请看这篇文章。
2. Adding Cucumber Support
2.增加黄瓜支持
To add support for Cucumber in a simple Maven project, we will need to add the following dependencies:
要在一个简单的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>
Useful links to dependencies from Maven Central: cucumber-junit, cucumber-java, hamcrest-library
来自Maven中心的有用的依赖性链接。cucumber-junit, cucumber-java, hamcrest-library
Since these are testing libraries, they don’t need to be shipped with the actual deployable – which is why they’re all test scoped.
由于这些是测试库,它们不需要与实际部署的库一起运送 – 这就是为什么它们都是test范围。
3. A Simple Example
3.一个简单的例子
Let’s demonstrate both a bloated way and a concise way of writing featured files. Let’s first define the logic we want to write a test for:
让我们来演示一下编写特色文件的臃肿方式和简明方式。让我们首先定义我们要写一个测试的逻辑。
Let’s first define the logic we want to write a test for:
让我们首先定义我们想写一个测试的逻辑。
public class Calculator {
public int add(int a, int b) {
return a + b;
}
}
4. Defining Cucumber Tests
4.定义Cucumber测试
4.1. Defining a Feature File
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
As seen here, 2 different combinations of numbers have been put to test here the addition logic. Apart from numbers, all the scenarios are exactly the same.
从这里可以看出,2个不同的数字组合已经被放在这里测试加法逻辑。除了数字之外,所有的场景都是完全一样的。
4.2. “Glue” Code
4.2.”胶水 “代码
In order to test out these scenarios, i’s essential to define each step with corresponding code, in order to translate a statement into a functional piece of code:
为了测试这些情况,我必须用相应的代码来定义每一个步骤,以便将一个声明转化为一段功能性的代码。
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. A Runner Class
4.3.跑步者班
In order to integrate features and the glue code, we can use the JUnit runners:
为了整合功能和胶水代码,我们可以使用JUnit运行器。
@RunWith(Cucumber.class)
@CucumberOptions(
features = { "classpath:features/calculator.feature" },
glue = {"com.baeldung.cucumber.calculator" })
public class CalculatorTest {}
5. Rewriting Features Using Scenario Outlines
5.使用场景大纲重写功能
We saw in Section 4.1. how defining a feature file can be a time-consuming task and more error prone. The same feature file can be reduced to mere few lines using the Scenario Outline:
我们在第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 |
When comparing a regular Scenario Definition with Scenario Outline, values no longer need to be hard-coded in step definitions. Values are replaced with parameters as <parameter_name> in step-definition itself.
当比较常规的情景定义和情景大纲时,数值不再需要在步骤定义中硬编码。在步骤定义本身中,值被替换为参数<parameter_name>。
At the end of Scenario Outline, values are defined in a pipe-delimited table format using Examples.
在情景大纲的末尾,使用Examples以管道分隔的表格格式定义数值。
A sample to define Examples is shown below:
定义Examples的样本显示如下。
Examples:
| Parameter_Name1 | Parameter_Name2 |
| Value-1 | Value-2 |
| Value-X | Value-Y |
6. Conclusion
6.结论
With this quick article, we’ve shown how scenarios can be made generic in nature. And also reduce effort in writing and maintaining these scenarios.
通过这篇简短的文章,我们已经展示了如何使场景具有通用性的性质。同时也减少了编写和维护这些方案的工作。
The complete source code of this article can be found over on GitHub.
本文的完整源代码可以在GitHub上找到over。