1
什么是Cucumber
cucumber早在ruby环境下应用广泛,作为BDD框架的先驱,cucumber后来被移植到了多平台,简单来说cucumber是一个测试框架,就像是juint或是rspec一样,不过cucumber遵循的是BDD的原则。
2
何为BDD?
BDD就是行为驱动开发,是一种软件开发流程或者说是软件开发实践,具体学术化的东西这里就不介绍了,归根到底,cucumber具有让你用自然语言编写用例的能力。
使用自然语言编写用例有很多好处,最直观的好处就是你的客户在一定的情况下是能够看懂你的测试用例的。最为项目的最核心因素,客户决定了项目该做成什么样,具有什么功能,不需要实现哪些功能。
客户是需求的源泉,如果我们的测试用例很够很好的跟需求结合起来,比如说我们用自然语言写的测试用例如果能让用户认同,那么在这种情况下,测试用例基本等同于原始的需求文档了。需求文档是开发的凭据,这样一来根据测试用例来实现具体的需求就一定是客户所希望完成的需求了,毕竟这些需求是经过用户首肯的。这样一来,我们就等同于是让测试用例驱动了开发,这就是所谓的测试驱动开发的一种不太严谨的初体验了。
cucumber就是这样一种可以把需求转换为测试用例,让测试用例即需求的测试框架。
3
Features介绍
feature(功能),每一个feature文件都要 开始于Feature(功能),Feature之后的描述可以随便写,知道出现Scenario(场景),一个feature中可以有多个 Scenario,每个Scenario包含(step)步骤列表,步骤使用Given、When、Then、But、And这些关键 词,cucumber对这些关键词的处理是一样的,但是我们在使用的时候需要按照场景区分。
4
Step denfinitions介绍
Cucumber中定义的每一个step(步骤)都需要有一个step definition对应,默认的话是使用Ruby来编写定义的脚本(现在有cucumber-js等也支持javascript、java等来编写),支持通过正则表达式从step中传递参数。
Step definition的详细说明可以参考
https://github.com/cucumber/cucumber/wiki/Step-Definitions
5
Given When then(假如 当 那么)
Cucumber的步骤中会包含Given、When、then这些词组,cucumber本身在技术实现上不区分这三个词组,但是在使用上推荐按照词组的意思来使用。
6
Cucumber使用
查看cucumber支持的语言 cucumber --i18n help,查看支持语言的关键字 cucumber –i18n zh-CN(目前中文不太会用)。
先跑起来个demo吧
先编写feature文件(即需求,要做的事)
在maven工程的test目录下创建\resources\feature\demo.feature文件
Feature: 验证计算器计算功能 打开计算器进行计算 @CalculatorTest Scenario: 打开计算器进行计算1+1 Given 打开计算器面板 When 已经输入1并按下+ And 输入 "1" And 按下=号 Then 等待计算结果
编写代码测试代码(部分术语参考第5大项)
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("等待计算结果"); }
}
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 {}
<?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
如何执行
选中RunCukesTest,右键run test即可
maven test
8
运行结果
9
测试报告
10
感受
用着不太习惯,正则表达式这个有点烦,测试报告应该可以在扩展,后期再去尝试改造。
优点:个人觉得给人看,逼格很高,层次分明。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有