1. Overview
1.概述
In this tutorial, we’ll learn three different methods for overriding the Cucumber option values. From a precedence point of view, Cucumber will parse and override options from:
在本教程中,我们将学习覆盖 Cucumber 选项值的三种不同方法。从优先级的角度来看,Cucumber 将解析并覆盖以下选项:
- system properties, environment variables, and the cucumber.properties file
- @CucumberOptions annotation
- CLI arguments
To showcase each method, we’ll run a simple feature file with two scenarios and override the Cucumber tags option.
为了展示每种方法,我们将运行一个包含两个场景的简单功能文件,并覆盖 Cucumber tags 选项。
2. Setup
2.设置
There is some initial setup we need to do before going through each method. First, let’s add the cucumber-java, cucumber-junit, cucumber-spring, and junit-vintage-engine dependencies:
在学习每种方法之前,我们需要进行一些初始设置。首先,让我们添加 cucumber-java、cucumber-junit、cucumber-spring、 和 junit-vintage-engine依赖项:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.14.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.14.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>7.14.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
Next, let’s implement a simple controller with two endpoints:
接下来,让我们实现一个带有两个端点的简单控制器:
@RestController
public class HealthCheckController {
@GetMapping(path = "/v1/status", produces = APPLICATION_JSON_VALUE)
public HttpStatus getV1Status() {
return ResponseEntity.ok().build().getStatusCode();
}
@GetMapping(path = "/v2/status", produces = APPLICATION_JSON_VALUE)
public HttpStatus getV2Status() {
return ResponseEntity.ok().build().getStatusCode();
}
}
We can now add the feature file and the two scenarios:
现在我们可以添加功能文件和两个场景:
Feature: status endpoints can be verified
@v1
Scenario: v1 status is healthy
When the client calls /v1/status
Then the client receives 200 status code
@v2
Scenario: v2 status is healthy
When the client calls /v2/status
Then the client receives 200 status code
Finally, let’s add the required Cucumber glue code:
最后,让我们添加所需的 Cucumber 胶水代码:
@When("^the client calls /v1/status")
public void checkV1Status() throws Throwable {
executeGet("http://localhost:8082/v1/status");
}
@When("^the client calls /v2/status")
public void checkV2Status() throws Throwable {
executeGet("http://localhost:8082/v2/status");
}
@Then("^the client receives (\\d+) status code$")
public void verifyStatusCode(int statusCode) throws Throwable {
final HttpStatus currentStatusCode = latestResponse.getStatusCode();
assertThat(currentStatusCode.value(), is(statusCode));
}
By default, Cucumber runs all scenarios if not specified otherwise. Let’s verify this behavior by running the tests:
默认情况下,如果没有特别指定,Cucumber 会运行所有场景。让我们通过运行测试来验证这一行为:
mvn test
As expected, both scenarios are executed:
不出所料,两个方案都得到了执行:
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
We’re now ready to go over the three different methods for overriding the Cucumber option values.
现在,我们要学习覆盖 Cucumber 选项值的三种不同方法。
3. Using the cucumber.properties File
3.使用 cucumber.properties 文件
The first method loads the Cucumber options from system properties, environment variables, and the cucumber.properties file.
第一种方法是从系统属性、环境变量和 cucumber.properties 文件中加载 Cucumber 选项。
Let’s add the cucumber.filter.tags property to the cucumber.properties file:
让我们在 cucumber.properties 文件中添加 cucumber.filter.tags 属性:
cucumber.filter.tags=@v1
Running the tests this time will only execute the @v1 scenario:
这次运行测试只会执行 @v1 场景:
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
4. Using the @CucumberOptions Annotation
4.使用 @CucumberOptions 注释
The second method uses the @CucumberOptions annotation. Let’s add the tags field:
第二种方法使用 @CucumberOptions 注解。让我们添加 tags 字段:
@CucumberOptions(tags = "@v1")
Running the tests one more time will execute only the @v1 scenario:
再运行一次测试将只执行 @v1 场景:
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
Note that this method will override any values provided using system properties, environment variables, and values from the cucumber.properties file.
注意,此方法将覆盖使用系统属性、环境变量和 cucumber.properties 文件中的值提供的任何值。
5. Using CLI Arguments
5.使用 CLI 参数
The last method uses the Cucumber CLI runner and the tags argument. Make sure to use the correct classpath so that it includes the compiled classes and resources and all dependencies (including the ones with test scope):
最后一种方法使用 Cucumber CLI runner 和 tags 参数。请确保使用正确的 classpath,以便包含已编译的类和资源以及所有依赖项(包括test作用域的依赖项):
java -cp ... io.cucumber.core.cli.Main --tags @v1
As expected, only the @v1 scenario is executed:
不出所料,只有 @v1 方案被执行:
1 Scenarios (1 passed)
2 Steps (2 passed)
Note that this method will override any values provided through the cucumber.properties file or the @CucumberOptions annotation.
注意,此方法将覆盖通过 cucumber.properties 文件或 @CucumberOptions 注解提供的任何值。
6. Conclusion
6.结论
In this article, we’ve learned three methods to override the Cucumber option values.
在本文中,我们学习了三种覆盖 Cucumber 选项值的方法。
The first method considers options provided as system properties, environment variables, and values in the cucumber.properties file. The second method considers options provided as fields in the @CucumberOptions annotation and overrides any options provided by the first method. The last method uses CLI arguments and overrides the options provided using any of the previous methods.
第一种方法会考虑作为系统属性、环境变量和 cucumber.properties 文件中的值提供的选项。第二种方法会考虑作为 @CucumberOptions 注解中的字段提供的选项,并覆盖第一种方法提供的任何选项。最后一种方法使用 CLI 参数,并覆盖使用前面任何一种方法提供的选项。
As always, the complete code can be found over on GitHub.