点击查看代码
1. Maven 项目配置 (pom.xml) ```<groupId>com.example</groupId>
<artifactId>cucumber-junit5-demo</artifactId>
<version>1.0-SNAPSHOT</version><properties><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><cucumber.version>7.18.0</cucumber.version><junit.version>5.10.0</junit.version>
</properties><dependencies><!-- Cucumber Dependencies --><dependency><groupId>io.cucumber</groupId><artifactId>cucumber-java</artifactId><version>${cucumber.version}</version><scope>test</scope></dependency><dependency><groupId>io.cucumber</groupId><artifactId>cucumber-junit-platform-engine</artifactId><version>${cucumber.version}</version><scope>test</scope></dependency><!-- JUnit 5 Dependencies --><dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-suite</artifactId><version>1.10.0</version><scope>test</scope></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>${junit.version}</version><scope>test</scope></dependency><!-- AssertJ for better assertions --><dependency><groupId>org.assertj</groupId><artifactId>assertj-core</artifactId><version>3.24.2</version><scope>test</scope></dependency>
</dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.1.2</version></plugin></plugins>
</build>
src/
├── main/
│ └── java/
│ └── com/example/
│ └── calculator/
│ └── Calculator.java
└── test/
├── java/
│ └── com/example/
│ ├── runner/
│ │ └── CucumberTestSuite.java
│ └── stepdefinitions/
│ └── CalculatorSteps.java
└── resources/
└── features/
└── calculator.feature
- Feature 文件
src/test/resources/features/calculator.feature
点击查看代码
Feature: CalculatorScenario: Add two numbersGiven I have a calculatorWhen I add 5 and 3Then the result should be 8Scenario: Subtract two numbersGiven I have a calculatorWhen I subtract 10 from 15Then the result should be 5Scenario Outline: Multiply two numbersGiven I have a calculatorWhen I multiply <a> and <b>Then the result should be <result>Examples:| a | b | result || 2 | 3 | 6 || 4 | 5 | 20 || 6 | 7 | 42 |
- 被测试的应用类
src/main/java/com/example/calculator/Calculator.java
点击查看代码
package com.example.calculator;public class Calculator {private int result;public void add(int a, int b) {result = a + b;}public void subtract(int a, int b) {result = a - b;}public void multiply(int a, int b) {result = a * b;}public int getResult() {return result;}
}
点击查看代码
package com.example.stepdefinitions;import com.example.calculator.Calculator;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import static org.assertj.core.api.Assertions.assertThat;public class CalculatorSteps {private Calculator calculator;@Given("I have a calculator")public void i_have_a_calculator() {calculator = new Calculator();}@When("I add {int} and {int}")public void i_add_and(Integer a, Integer b) {calculator.add(a, b);}@When("I subtract {int} from {int}")public void i_subtract_from(Integer a, Integer b) {calculator.subtract(b, a);}@When("I multiply {int} and {int}")public void i_multiply_and(Integer a, Integer b) {calculator.multiply(a, b);}@Then("the result should be {int}")public void the_result_should_be(Integer expectedResult) {assertThat(calculator.getResult()).isEqualTo(expectedResult);}
}
- 测试套件类
src/test/java/com/example/runner/CucumberTestSuite.java
点击查看代码
package com.example.runner;import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;import static io.cucumber.junit.platform.engine.Constants.FEATURES_PROPERTY_NAME;
import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;
import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME;@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameter(key = FEATURES_PROPERTY_NAME, value = "classpath:features")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.example.stepdefinitions")
@ConfigurationParameter(key = "cucumber.filter.tags", value = "@lmy")
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty, html:target/cucumber-reports.html, json:target/cucumber-reports.json")
public class CucumberTestSuite {
}