cucumber早在ruby环境下应用广泛,作为BDD框架的先驱,cucumber后来被移植到了多平台,简单来说cucumber是一个测试框架,就像是juint或是rspec一样,不过cucumber遵循的是BDD的原则。
BDD就是行为驱动开发,是一种软件开发流程或者说是软件开发实践,具体学术化的东西这里就不介绍了,归根到底,cucumber具有让你用自然语言编写用例的能力。
使用自然语言编写用例有很多好处,最直观的好处就是你的客户在一定的情况下是能够看懂你的测试用例的。最为项目的最核心因素,客户决定了项目该做成什么样,具有什么功能,不需要实现哪些功能。客户是需求的源泉,如果我们的测试用例很够很好的跟需求结合起来,比如说我们用自然语言写的测试用例如果能让用户认同,那么在这种情况下,测试用例基本等同于原始的需求文档了。需求文档是开发的凭据,这样一来根据测试用例来实现具体的需求就一定是客户所希望完成的需求了,毕竟这些需求是经过用户首肯的。这样一来,我们就等同于是让测试用例驱动了开发,这就是所谓的测试驱动开发的一种不太严谨的初体验了。
cucumber就是这样一种可以把需求转换为测试用例,让测试用例即需求的测试框架。话不多说,先来个demo看看
Cucumber中定义的每一个step(步骤)都需要有一个step definition对应,默认的话是使用Ruby来编写定义的脚本(现在有cucumber-js等也支持javascript、java等来编写),支持通过正则表达式从step中传递参数。Step definition的详细说明可以参考
https://github.com/cucumber/cucumber/wiki/Step-Definitions
Cucumber的步骤中会包含Given、When、then这些词组,cucumber本身在技术实现上不区分这三个词组,但是在使用上推荐按照词组的意思来使用。
Given-用例开始执行前的一个前置条件,类似与编写代码setup中的一些步骤
When-用例开始执行的一些关键操作步骤,类似点击元素等
Then-观察结果,就是平时用例中的验证步骤
And-一个步骤中如果存在多个Given操作,后面的Given可以用And替代
But-一个步骤中如果存在多个Then操作,第二个开始后面的Then可以用But替代
查看cucumber支持的语言 cucumber --i18n help
查看支持语言的关键字 cucumber –i18n zh-CN(目前中文不太会用)
先跑起来的demo,
在maven工程的test目录下创建\resources\feature\demo.feature文件
demo.feature
Feature: 验证计算器计算功能 打开计算器进行计算 @CalculatorTest Scenario: 打开计算器进行计算1+1 Given 打开计算器面板 When 已经输入1并按下+ And 输入 "1" And 按下=号 Then 等待计算结果
6.2、编写代码测试代码(部分术语参考第5大项)
Calculator(测试用例部分)
package com.cucumber.demo;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class Calculator {
@Given("^打开计算器面板$")
public void openCalculator() throws Exception {
System.out.println("打开计算器面板");
}
@When("^已经输入1并按下+")
public void alreadyInput1() {
System.out.println("已经输入1并按下+");
}
@And("^输入 \"([^\"]*)\"$")
public void input1(String num) throws Throwable {
System.out.println("输入"+num);
}
@And("^按下=")
public void pressEaualButton(){
System.out.println("按下=");
}
@Then("^等待计算结果")
public void wait_the_query_result() throws InterruptedException {
System.out.println("等待计算结果");
}
}
RunCukesTest(执行测试部分)
package com.cucumber.demo;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
features = {"src/test/resources/feature/"},
format = {"pretty", "html:target/cucumber", "json:target/cucumber.json"},
glue = {"com.cucumber"},
tags = {"@CalculatorTest"}
)
public class RunCukesTest {
}
pom文件
<?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>Cucumber_Test</groupId>
<artifactId>Cucumber_Test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
</dependency>
<dependency>
<groupId>org.picocontainer</groupId>
<artifactId>picocontainer</artifactId>
<version>2.14</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>once</forkMode>
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
7.1选中RunCukesTest,右键run test即可

7.2 maven test



用着不太习惯,正则表达式这个有点烦,测试报告应该可以在扩展,后期再去尝试改造
优点:个人觉得给人看,逼格很高,层次分明
参考链接:
https://www.cnblogs.com/dami520/p/3168864.html
https://www.cnblogs.com/nbkhic/p/4874889.html
原文:https://www.cnblogs.com/longronglang/p/10360027.html