cucumber

cucumber 是一个用于执行 BDD 的自动化测试工具。

用户指南

  1. 创建 Spring Boot 项目并引入依赖
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>org.zxd</groupId>
<artifactId>behavior-driven-development-in-action</artifactId>
<version>1.0.0</version>
<name>behavior-driven-development-in-action</name>
<description>behavior-driven-development-in-action</description> <properties>
<java.version>1.8</java.version>
<cucumber.version>4.2.0</cucumber.version>
</properties> <dependencies>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0-jre</version>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.2</version>
</dependency> <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency> <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
<version>${cucumber.version}</version>
</dependency> <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
</dependency> <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${cucumber.version}</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<classpathScope>test</classpathScope>
<mainClass>org.zxd.bdd.cucumber.CucumberTest.java</mainClass>
<arguments>
<argument>--plugin</argument>
<argument>pretty</argument>
<argument>--glue</argument>
<argument>src/test/resources/</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
  • 编写特性文件 TodoList.feature
#Author: zxd
#Keywords 概要
#Feature: 场景列表
#Scenario: 通过带有参数的步骤列表验证业务规则
#Given: 一些前置步骤
#When: 执行核心操作
#Then: 验证结果
#And,But: 列举更多的步骤
#Scenario Outline: 数据驱动的步骤列表
#Examples: Container for s table
#Background: 在每个场景之前运行的步骤列表
#""" (Doc Strings)
#| (Data Tables)
#@ (Tags/Labels):场景分组
#<> (placeholder)
#""
## (Comments)
#Sample Feature Definition Template
@tag
Feature: 任务特性 @tag1
Scenario Outline: 执行任务
Given 任务列表有 <total> 个任务
When 我完成了 <done> 个
Then 任务列表还剩下 <left> 个任务
Examples:
| total | done | left |
| 5 | 2 | 3 |
  • 实现该特性
@Builder
@Getter
public class TodoList {
private int total;
public void doTask(int count) {
if (count > total || count < 0) {
throw new IllegalArgumentException("count is invalid "+count);
}
total = total - count;
}
}
  • 配置测试组件
@RunWith(Cucumber.class)
/**
* features 用于指定特性文件的根目录
* plugin 用于指定报告插件
*/
@CucumberOptions(features = "src/test/resources", plugin = { "pretty", "html:target/cucumber-report/",
"json:target/cucumber-report/cucumber.json" })
public class CucumberTest {
} @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = BehaviorDrivenDevelopmentInActionApplication.class)
@WebAppConfiguration
@SpringBootTest
public class BaseDefs{}
  • 编写测试规范
public class TodoListTest extends BaseDefs {
TodoList todoList; @Given("^任务列表有 (\\d+) 个任务$")
public void given(int total) throws Throwable {
todoList = TodoList.builder().total(total).build();
} @When("^我完成了 (\\d+) 个$")
public void when(int done) throws Throwable {
todoList.doTask(done);
} @Then("^任务列表还剩下 (\\d+) 个任务$")
public void then(int left) throws Throwable {
assertEquals(left, todoList.getTotal());
}
}
  • 运行测试用例
选中 CucumberTest 运行测试并查看报告

最新文章

  1. __dbg.h
  2. 在Mongoose中使用嵌套的populate处理数据
  3. Maya 学习资料
  4. [Hibernate] - many to one
  5. Node.js的process模块
  6. 【HDOJ】1244 Max Sum Plus Plus Plus
  7. oracle 分布式数据库
  8. RelativeLayout布局下实现控件平分空间
  9. 32位程序在64位系统上获取系统安装时间(要使用KEY_WOW64_64KEY标记)
  10. MongoDB--数据库管理
  11. MMORPG战斗系统随笔(二)、浅谈场寻路Flow Field PathFinding算法
  12. javascript 实现斐波那契数列的不同姿势
  13. 818C.soft thief
  14. 最新Java技术
  15. android 手机权限管理——PermissionsDispatcher
  16. A1014. Waiting in Line
  17. 延续(continuation)
  18. Hibernate学习笔记三:常用数据库操作语句
  19. 3-23Agile Web Development,3-24(chapter: 6)
  20. cnblogs用户体验评价

热门文章

  1. 2019-11-29-git无法pull仓库refusing-to-merge-unrelated-histories
  2. 字典树——动态&amp;&amp;静态
  3. 【JAVA】Maven profiles控制多环境数据源日志打包(转载)
  4. TJOI2017DNA
  5. 【模板】AC自动机加强版
  6. public class Ex2
  7. DevExpress WinForms v19.1新版亮点:Tree List等控件性能增强
  8. 图片转base64的几种场景(网络图片,本地图片,用户上传图片)
  9. linux系统下导出MySQL文件
  10. pyqt5-动画组QAnimationGroup