1. Introduction
1.绪论
Cucumber is a test automation tool that supports Behavior-Driven Development (BDD). It runs specifications written in plain text Gherkin syntax that describes the system behavior.
Cucumber是一个测试自动化工具,支持行为驱动开发(BDD)。它可以运行以纯文本Gherkin语法编写的、描述系统行为的规范。
In this tutorial, we’ll see a few ways to integrate Cucumber with Gradle in order to run BDD specifications as part of the project build.
在本教程中,我们将看到几种将Cucumber与Gradle集成的方法,以便作为项目构建的一部分运行BDD规范。
2. Setup
2.设置
First, let’s set up a Gradle project, using Gradle Wrapper.
首先,让我们设置一个Gradle项目,使用Gradle Wrapper。
Next, we’ll add the cucumber-java dependency to build.gradle:
接下来,我们将把cucumber-java 依赖性添加到build.gradle。
testImplementation 'io.cucumber:cucumber-java:6.10.4'
This adds the official Cucumber Java implementation to our project.
这就为我们的项目添加了Cucumber的官方Java实现。
3. Running Using Custom Task
3.使用自定义任务运行
In order to run our specifications using Gradle, we’ll create a task that uses the Command-Line Interface Runner (CLI) from Cucumber.
为了使用Gradle运行我们的规范,我们将创建一个任务,使用Cucumber的命令行界面运行器(CLI)。
3.1. Configuration
3.1 配置
Let’s start by adding the required configuration to the project’s build.gradle file:
让我们先在项目的build.gradle文件中加入所需的配置。
configurations {
cucumberRuntime {
extendsFrom testImplementation
}
}
Next, we’ll create the custom cucumberCli task:
接下来,我们将创建自定义的cucumberCli任务。
task cucumberCli() {
dependsOn assemble, testClasses
doLast {
javaexec {
main = "io.cucumber.core.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = [
'--plugin', 'pretty',
'--plugin', 'html:target/cucumber-report.html',
'--glue', 'com.baeldung.cucumber',
'src/test/resources']
}
}
}
This task is configured to run all the test scenarios found in .feature files under the src/test/resources directory.
该任务被配置为运行.feature文件中发现的所有测试方案,这些文件在src/test/resources目录下。
The –glue option to the Main class specifies the location of the step definition files required for running the scenarios.
Main类的-glue选项指定了运行场景所需的步骤定义文件的位置。
The –plugin option specifies the format and location of the test reports. We can combine several values to generate the report(s) in the required format(s), such as pretty and HTML, as in our example.
-plugin选项指定了测试报告的格式和位置。我们可以结合几个值,以所需的格式生成报告,如pretty和HTML,如我们的例子。
There are several other options available. For example, there are options to filter tests based on names and tags.
还有其他几个选项可用。例如,有一些选项可以根据名称和标签来过滤测试。
3.2. Scenario
3.2.场景
Now, let’s create a simple scenario for our application in the src/test/resources/features/account_credited.feature file:
现在,让我们在src/test/resources/features/account_credited.feature文件中为我们的应用程序创建一个简单的场景。
Feature: Account is credited with amount
Scenario: Credit amount
Given account balance is 0.0
When the account is credited with 10.0
Then account should have a balance of 10.0
Next, we’ll implement the corresponding step definitions — the glue— required for running the scenario:
接下来,我们将实现相应的步骤定义–运行该场景所需的glue。
public class StepDefinitions {
@Given("account balance is {double}")
public void givenAccountBalance(Double initialBalance) {
account = new Account(initialBalance);
}
// other step definitions
}
3.3. Run the Task
3.3.运行任务
Finally, let’s run our cucumberCli task from the command line:
最后,让我们从命令行运行我们的cucumberCli任务。
>> ./gradlew cucumberCli
> Task :cucumberCli
Scenario: Credit amount # src/test/resources/features/account_credited.feature:3
Given account balance is 0.0 # com.baeldung.cucumber.StepDefinitions.account_balance_is(java.lang.Double)
When the account is credited with 10.0 # com.baeldung.cucumber.StepDefinitions.the_account_is_credited_with(java.lang.Double)
Then account should have a balance of 10.0 # com.baeldung.cucumber.StepDefinitions.account_should_have_a_balance_of(java.lang.Double)
1 Scenarios (1 passed)
3 Steps (3 passed)
0m0.381s
As we can see, our specification has been integrated with Gradle, runs successfully, and the output is shown on the console. Also, the HTML test report is available in the specified location.
正如我们所看到的,我们的规范已经与Gradle集成,运行成功,并且输出显示在控制台。另外,HTML测试报告也可以在指定的位置找到。
4. Running Using JUnit
4.使用JUnit运行
Instead of creating the custom task in Gradle, we can use JUnit to run the cucumber scenarios.
我们可以使用JUnit来运行cucumber场景,而不是在Gradle中创建自定义任务。
Let’s start by including the cucumber-junit dependency:
让我们从包括cucumber-junit依赖性开始。
testImplementation 'io.cucumber:cucumber-junit:6.10.4'
As we’re using JUnit 5, we also need to add the junit-vintage-engine dependency:
由于我们使用JUnit 5,我们还需要添加junit-vage-engine依赖。
testImplementation 'org.junit.vintage:junit-vintage-engine:5.7.2'
Next, we’ll create an empty runner class in the test sources location:
接下来,我们将在测试源位置创建一个空的运行器类。
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty", "html:target/cucumber-report.html"},
features = {"src/test/resources"}
)
public class RunCucumberTest {
}
Here, we’ve used the JUnit Cucumber runner in the @RunWith annotation. Furthermore, all the CLI runner options, such as features and plugin, are available via the @CucumberOptions annotation.
在这里,我们在@RunWith注解中使用了JUnit的Cucumber运行器。此外,所有的CLI运行器选项,如features和plugin,都可以通过@CucumberOptions注解获得。
Now, executing the standard Gradle test task will find and run all the feature tests, in addition to any other unit tests:
现在,执行标准Gradle test任务将找到并运行所有功能测试,此外还有其他单元测试。
>> ./gradlew test
> Task :test
RunCucumberTest > Credit amount PASSED
BUILD SUCCESSFUL in 2s
5. Running Using Plugin
5.使用插件运行
The last approach is to use a third-party plugin that provides the ability to run specifications from the Gradle build.
最后一种方法是使用第三方插件,提供从Gradle构建中运行规范的能力。
In our example, we’ll use the gradle-cucumber-runner plugin for running Cucumber JVM. Under the hood, this forwards all calls to the CLI runner that we used earlier. Let’s include it in our project:
在我们的例子中,我们将使用gradle-cucumber-runner插件来运行Cucumber JVM。在引擎盖下,它将所有调用转发给我们之前使用的CLI runner。让我们把它包含在我们的项目中。
plugins {
id "se.thinkcode.cucumber-runner" version "0.0.8"
}
This adds a cucumber task to our build, and now we can run it with default settings:
这就为我们的构建添加了一个cucumber任务,现在我们可以用默认设置运行它。
>> ./gradlew cucumber
It’s worth noting that this is not an official Cucumber plugin, and there are others also available that provide similar functionality.
值得注意的是,这不是一个官方的Cucumber插件,还有其他的插件也可以提供类似的功能。
6. Conclusion
6.结语
In this article, we demonstrated several ways to configure and run BDD specifications using Gradle.
在这篇文章中,我们演示了使用Gradle配置和运行BDD规范的几种方法。
Initially, we looked at how to create a custom task utilizing the CLI runner. Then, we looked at using the Cucumber JUnit runner to execute the specifications using the existing Gradle task. Finally, we used a third-party plugin to run Cucumber without creating our own custom tasks.
最初,我们研究了如何利用CLI运行器创建一个自定义任务。然后,我们研究了使用Cucumber JUnit运行器来执行现有Gradle任务的规范。最后,我们使用一个第三方插件来运行Cucumber,而不创建我们自己的自定义任务。
As always, the full source can be found over on GitHub.
一如既往,完整的源代码可以在GitHub上找到,。