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 运行测试并查看报告

BDD Cucumber 实战的更多相关文章

  1. cucumber 使用资料

    1.cucumber reporting github:https://github.com/damianszczepanik/cucumber-reporting 配置:详细参考上述地址描述 a.添 ...

  2. 测试人员,今天再不懂BDD就晚了!

    首先,测试人员应该参与软件开发的全流程,这一点已经是软件行业的共识了. 其次,新技术.新框架.新思路不断涌现的今天,测试人员除了传统的功能测试,也要不断与时俱进,主动承担起自动化测试.性能测试等.除了 ...

  3. junit学习笔记

    junit编程规范 测试方法上必须使用@Test进行修饰 测试方法必须使用public void 进行修饰,不能带任何的参数 新建一个源代码目录 测试类的包应该和被测试类保持一致 测试单元中的每个方法 ...

  4. Frontend Development

    原文链接: https://github.com/dypsilon/frontend-dev-bookmarks Frontend Development Looking for something ...

  5. 一名全栈工程师Node.js之路-转

    Node.js 全球现状 虽然 Node.js 在国内没有盛行,但据 StackOverflow 2016 年开发者调查,其中 node.js .全栈.JavaScript 相关的技术在多个领域(包括 ...

  6. Android开发免费类库和工具集合

    用于Android开发的免费类库和工具集合,按目录分类. Action Bars ActionBarSherlock Extended ActionBar FadingActionBar GlassA ...

  7. NodeJS&&前端思考

    做大型软件(工程化): 1.测试相关 tdd / bdd 测试覆盖率 2.规范化 standard.各种 lint.hint 3.构建相关 gulp.grunt.webpack,大量插件 4.生成器 ...

  8. Top11 构建和测试API的工具

    立刻像专业人士一样构建API 组织正在改变他们已经在软件应用项目中成功的微服务架构模型,这就是大多数微服务项目使用API(应用程序接口)的原因. 我们要为微服务喝彩,因为它相对于其他的模型有各种先进的 ...

  9. BDD敏捷开发入门与实战

    BDD敏捷开发入门与实战 1.BDD的来由 2003年,Dan North首先提出了BDD的概念,并在随后开发出了JBehave框架.在Dan North博客上介绍BDD的文章中,说到了BDD的想法是 ...

随机推荐

  1. markdown的日常使用

    # POI前端接口 ``` 描述: 项目poi接口 作者: X-Wolf 时间: -- ``` ------ [TOC] ------ ##声明 ### 请求地址 ``` DOMAIN/strateg ...

  2. 搜索框focus 搜索面板显示 点击别处消失 从浏览器别的页面回来消失

    开始是设置了回到页面使display:none(离开页面操作失效),但是发现回到页面,面板显示,dom获取却为null,于是做了个延时的处理 currentPage: function() { var ...

  3. 一个线程oom,进程里其他线程还能运行吗?

    线程之间互相不影响:守护线程生活周期相同 引言 这题是一个网友@大脸猫爱吃鱼给我的提问,出自今年校招美团三面的一个真题.大致如下 一个进程有3个线程,如果一个线程抛出oom,其他两个线程还能运行么? ...

  4. linux 查看io

    有时系统卡,可能是io值使用满了导致 1.安装iostat yum install sysstat 2.查看io使用情况 iostat -dx 3.安装iotop yum install iotop ...

  5. httprunner如何提取数据串联上下游接口

    httprunner进行接口测试时,从上一个接口提取参数传递给下游接口,如何获取数据里最后一个值? 突然被学员问道一个httprunner的问题,惭愧的是大猫之前没有是通过httprunner,又不好 ...

  6. Monty 大厅问题(Monty Hall Problem)也称作三门问题,出自美国大型游戏节目 Let's Make a Deal。

    Monty 大厅的问题陈述十分简单,但是它的答案看上去却是有悖常理.该问题不仅引起过很多争议,也经常出现在各种考试题中. Monty 大厅的游戏规则是这样的,如果你来参加这个节目,那么 (1)Mont ...

  7. python 字符串方法及列表,元组,字典(一)

    字符串 str 注: 若想要保持单引号和双引号为字符串的一部分 1)单双引号交替使用, 2)使用转义字符\ 3)成对三个引号被存在变量里 二.字符串详细用法 字符串的单个取值例 p_1=”hello” ...

  8. Pythoy修炼之路

    路漫漫其修远兮,吾将上下而求索... 编程语言众多,徘徊在C.C#.Java.PHP等十字路口,看到哪个领域大牛的成功,便想打开那扇窗,反反复复,犹豫不决,终无所获. 个人认为选择一门语言,要根据自身 ...

  9. 【30分钟学完】canvas动画|游戏基础(7):动量守恒与多物体碰撞

    前言 一路沿着本系列教程学习的朋友可能会发现,前面教程中都尽量避免提及质量的概念,很多运动概念也时刻提醒大家这不是真实的物体运动.因为真实的物体运动其实跟质量都是密不可分的,而且质量的引入自然必须提及 ...

  10. 【ZJOJ1321】灯

    题目 贝希和她的闺密们在她们的牛棚中玩游戏.但是天不从人愿,突然,牛棚的电源跳闸了,所有的灯都被关闭了.贝希是一个很胆小的女生,在伸手不见拇指的无尽的黑暗中,她感到惊恐,痛苦与绝望.她希望您能够帮帮她 ...