cucumber7+junit5

  1. Maven 项目配置 (pom.xml)
点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <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>
</project>
  1. 项目目录结构

src/
├── main/
│ └── java/
│ └── com/example/
│ └── calculator/
│ └── Calculator.java
└── test/
├── java/
│ └── com/example/
│ ├── runner/
│ │ └── CucumberTestSuite.java
│ └── stepdefinitions/
│ └── CalculatorSteps.java
└── resources/
└── features/
└── calculator.feature

  1. Feature 文件
    src/test/resources/features/calculator.feature
点击查看代码
Feature: Calculator

  Scenario: Add two numbers
    Given I have a calculator
    When I add 5 and 3
    Then the result should be 8

  Scenario: Subtract two numbers
    Given I have a calculator
    When I subtract 10 from 15
    Then the result should be 5

  Scenario Outline: Multiply two numbers
    Given I have a calculator
    When I multiply <a> and <b>
    Then the result should be <result>
    
    Examples:
      | a  | b  | result |
      | 2  | 3  | 6      |
      | 4  | 5  | 20     |
      | 6  | 7  | 42     |
  1. 被测试的应用类
    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;
    }
}
  1. 步骤定义类
    src/test/java/com/example/stepdefinitions/CalculatorSteps.java
点击查看代码
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);
    }
}
  1. 测试套件类
    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 {
}
posted @ 2025-10-17 14:08  糖果粒  阅读(3)  评论(0)    收藏  举报