当前位置: 首页 > news >正文

cucumber7+junit5

点击查看代码 1. Maven 项目配置 (pom.xml) ```4.0.0
<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>
```
`` 2. 项目目录结构

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: 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     |
  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;}
}
5. 步骤定义类 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 {
}
http://www.hskmm.com/?act=detail&tid=32978

相关文章:

  • 剪映VIP全功能永久解锁后,我的剪辑效率直接翻倍!
  • 零碳园区建设指南:MyEMS 如何用数字化破解能耗与碳排放协同管理难题?
  • 误删 Stash 后的数据恢复实践
  • mysql开启binlog日志,完全配置指南
  • 2025年10月固定资产管理系统推荐榜单:基于全生命周期功能对比与行业适配度评测
  • Linux MegaCli RAID 控制管理工具详解
  • 2025年10月重庆保洁公司推荐对比榜:用数据还原真实服务能力
  • 2025年10月重庆保洁公司推荐排名:聚焦服务细节与合规风险的避坑手册
  • 2025 房屋改造公司最新推荐榜:聚焦老房 / 局部 / 小户型需求的口碑深度测评,花小钱住好家必看
  • 2025年10月床垫品牌推荐榜:十强对比与中立评测助你安心选购
  • uni-app x商城,商品列表组件封装以及使用
  • 深入解析:【Proteus8.17仿真】 STM32仿真 0.96OLED 屏幕显示ds1302实时时间
  • 2025年10月床垫品牌推荐榜:围绕环保认证与试睡政策的系统化评析
  • 贪心策略总结
  • 2025年10月上海装修公司推荐榜:极家家居设计标准与施工节点全维度对比
  • 完整教程:在鸿蒙NEXT中使用WebSocket实现实时网络通信
  • Atcoder Regular Contest 做题记录
  • Linux sas3ircu RAID 控制管理工具详解
  • Linux StorCLI RAID 控制管理工具详解
  • 2025年浓缩机厂家权威推荐榜:高效浓缩机/尾矿浓缩机/污泥浓缩机/新型浓缩机/矿用浓缩机/浓密机/中心转动浓缩机/真空浓缩机/污泥脱水机
  • 新手学AI算法/嵌入式 “知其然不知其所以然”?华清远见虚拟仿真工具拆分算法组件 + 动态调参,过程感拉满
  • http1.0,http2.0,http3.0各个协议的特点和区别
  • Clip Studio Paint 4.0.3下载地址与安装教程
  • ​​示波器探头的正确选择与使用指南​
  • C# Avalonia 16- Animation- KeySplineAnimation
  • 2025年工厂维保服务厂家权威推荐榜:机电维修、应急维修、设备安装维修、运维服务全方位解析
  • windows 11 或 Windows 10 注册表修改企业版为专业版
  • 低代码平台核心概念与设计理念
  • C# Avalonia 16- Animation- ExpandElement2
  • 2025年10月洗碗机品牌榜单推荐:五强性能全解析