专栏首页用户6517667的专栏cucumber自动化测试官方教程

cucumber自动化测试官方教程

来源:http://www.uml.org.cn/

安装

cucumber是一款测试工具。可用于大多数主流编程语言。比如JAVA、JS、Ruby、C++、Lua、Android、Kotlin、C#/F#、PHP、Python、Go、Groovy、Scala等等。其中JAVA、JS、Ruby的代码托管在cucumber下。官方建议选择与生产代码相同的平台或编程语言的实现。本文主要是JAVA平台下的介绍教程。使用方法非常简单,创建一个mvn工程,在pom.xml文件引入以下依赖即可。

<dependency><groupId>io.cucumber</groupId><artifactId>cucumber-java8</artifactId><version>4.2.0</version><scope>test</scope></dependency>

也可以根据骨架创建cucumber项目。

创建一个空的Cucumber项目

我们首先使用cucumber- prototype Maven插件创建一个新项目目录。打开终端,转到要创建项目的目录(比如本文是hellocucumber),运行以下命令:

mvn archetype:generate \-DarchetypeGroupId=io.cucumber \-DarchetypeArtifactId=cucumber-archetype \-DarchetypeVersion=2.3.1.2 \-DgroupId=hellocucumber \-DartifactId=hellocucumber \-Dpackage=hellocucumber \-Dversion=1.0.0-SNAPSHOT \-DinteractiveMode=false

你应该得到如下结果:

[INFO] Project created from Archetype in dir:hellocucumber/cucumber[INFO] -------------------------------------[INFO] BUILD SUCCESS[INFO] -------------------------------------

切换到刚才运行命令创建的目录:

cd hellocucumber

在IntelliJ IDEA(或者eclipse都行)中打开项目:

文件->打开…->(选择pom.xml)

选择Open as Project

现在,您已经安装了一个简单的Cucumber项目。

验证cucumber安装

mvn test

您应该看到如下内容:

------------------------------------T E S T S------------------------------------Running hellocucumber.RunCucumberTestNo features found at [classpath:hellocucumber]0 Scenarios0 Steps0m0.004sTests run: 0, Failures: 0, Errors: 0, Skipped: 0,Time elapsed: 0.541 secResults :Tests run: 0, Failures: 0, Errors: 0, Skipped: 0[INFO] -----------------------------------------[INFO] BUILD SUCCESS[INFO] -----------------------------------------

Cucumber的输出告诉我们它没有找到任何可以运行的东西。

写一个Scenario(场景)

当我们使用Cucumber进行行为驱动开发时,我们使用具体的例子来指定我们希望软件做什么。 Scenario是在生产代码之前编写的。它们以可执行规范的形式开始生命。随着生产代码的出现,场景扮演了事实文档和自动化测试的角色。

在Cucumber中,一个example称为Scenario。Scenario定义在.feature文件中,这些文件存储在src/test/resources/hellocucumber目录(或子目录)中。

一个具体的例子就是:星期天不是星期五。

创建一个名为src/test/resources/ hellocucumber/is_it_friday_yet.feature的文件, 文件包括以下内容:

Feature: Is it Friday yet?Everybody wants to know when it's FridayScenario: Sunday isn't FridayGiven today is SundayWhen I ask whether it's Friday yetThen I should be told "Nope"

这个文件的第一行以关键字'''Feature'''开始:后面跟着一个名称。最好使用与文件名类似的名称。

第二行是对该特性的简要描述。Cucumber并不执行这一行,它只是一个文档。

第4行,场景:Sunday is not Friday是一个scenario,它是说明软件应该如何工作的具体示例。

最后三行以Given开头,When和Then是我们的场景的步骤。这就是Cucumber将要执行的操作。

看一个未定义的scenario报告

现在我们有了一个场景,我们可以让Cucumber执行它:

mvn test

Cucumber告诉我们有一个undefined的场景和三个undefined的步骤。它还建议我们使用一些代码片段来define这些步骤:

-------------------------------------T E S T S-------------------------------------Running hellocucumber.RunCucumberTestFeature: Is it Friday yet?Everybody wants to know when it's FridayScenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4Given today is Sunday # nullWhen I ask whether it's Friday yet # nullThen I should be told "Nope" # null1 Scenarios (1 undefined)3 Steps (3 undefined)0m0.040sYou can implement missing steps with the snippets below:@Given("^today is Sunday$")public void today_is_Sunday() {// Write code here that turns the phrase aboveinto concrete actionsthrow new PendingException();}@When("^I ask whether it's Friday yet$")public void i_ask_whether_it_s_Friday_yet() {// Write code here that turns the phrase aboveinto concrete actionsthrow new PendingException();}@Then("^I should be told \"([^\"]*)\"$")public void i_should_be_told(String arg1) {// Write code here that turns the phrase aboveinto concrete actionsthrow new PendingException();}

复制以上未定义步骤的三个代码片段:

@Given("^today is Sunday$")public void today_is_Sunday() {// Write code here that turns the phrase aboveinto concrete actionsthrow new PendingException();}@When("^I ask whether it's Friday yet$")public void i_ask_whether_it_s_Friday_yet() {// Write code here that turns the phrase aboveinto concrete actionsthrow new PendingException();}@Then("^I should be told \"([^\"]*)\"$")public void i_should_be_told(String arg1) {// Write code here that turns the phrase aboveinto concrete actionsthrow new PendingException();}

并将它们粘贴到src/test/java/hellocucumber/steps.java中。

看一个pending的scenario报告

再次运行Cucumber: mvn test。这次的输出略有不同:

------------------------------------T E S T S------------------------------------Running hellocucumber.RunCucumberTestFeature: Is it Friday yet?Everybody wants to know when it's FridayScenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4Given today is Sunday # Stepdefs.today_is_Sunday()cucumber.api.PendingException: TODO: implement meat hellocucumber.Stepdefs.today_is_Sunday(Stepdefs.java:12)at ?.today is Sunday(hellocucumber/is_it_friday_yet.feature:5)When I ask whether it's Friday yet # Stepdefs.i_ask_whether_it_s_Friday_yet()Then I should be told "Nope" #Stepdefs.i_should_be_told(String)1 Scenarios (1 pending)3 Steps (2 skipped, 1 pending)0m0.188scucumber.api.PendingException:TODO: implement meat hellocucumber.Stepdefs.today_is_Sunday(Stepdefs.java:12)at ?.today is Sunday(hellocucumber/is_it_friday_yet.feature:5)

Cucumber找到我们的步骤定义并执行它们。它们当前被标记为pending,这意味着我们需要让它们做一些有用的事情。

看一个falling的scenario报告

下一步是按照步骤定义中的注释所告诉我们的去做:

Write code here that turns thephrase above into concrete actions

尝试在代码中使用与步骤中相同的单词。

将步骤定义代码更改为:

package hellocucumber;import cucumber.api.java.en.Given;import cucumber.api.java.en.When;import cucumber.api.java.en.Then;import static org.junit.Assert.*;class IsItFriday {static String isItFriday(String today) {return null;}}public class Stepdefs {private String today;private String actualAnswer;@Given("^today is Sunday$")public void today_is_Sunday() {today = "Sunday";}@When("^I ask whether it's Friday yet$")public void i_ask_whether_is_s_Friday_yet() {actualAnswer = IsItFriday.isItFriday(today);}@Then("^I should be told \"([^\"]*)\"$")public void i_should_be_told(String expectedAnswer) {assertEquals(expectedAnswer, actualAnswer);}}

再次运行mvn test :

-------------------------------------T E S T S-------------------------------------Running hellocucumber.RunCucumberTestFeature: Is it Friday yet?Everybody wants to know when it's FridayScenario: Sunday isn't Friday # hellocucumber/is_it_friday_yet.feature:4Given today is Sunday # Stepdefs.today_is_Sunday()When I ask whether it's Friday yet # Stepdefs.i_ask_whether_is_s_Friday_yet()Then I should be told "Nope" # Stepdefs.i_should_be_told(String)java.lang.AssertionError: expected:<Nope> but was:<null>at org.junit.Assert.fail(Assert.java:88)at org.junit.Assert.failNotEquals(Assert.java:834)at org.junit.Assert.assertEquals(Assert.java:118)at org.junit.Assert.assertEquals(Assert.java:144)at hellocucumber.Stepdefs.i_should_be_told(Stepdefs.java:30)at ?.I should be told "Nope"(hellocucumber/is_it_friday_yet.feature:7)Failed scenarios:hellocucumber/is_it_friday_yet.feature:4# Sunday isn't Friday1 Scenarios (1 failed)3 Steps (1 failed, 2 passed)0m0.404s

这就是进步! 但前两步已经passing,最后一步却failing了。

看一个passing的scenario报告

让我们做最简单的事情来让场景通过。在本例中,这只是为了让我们的方法返回Nope :

static String isItFriday(String today) {return "Nope";}

再次运行mvn test:

-------------------------------------T E S T S-------------------------------------Running hellocucumber.RunCucumberTestFeature: Is it Friday yet?Everybody wants to know when it's FridayScenario: Sunday isn't Friday #hellocucumber/is_it_friday_yet.feature:4Given today is Sunday # Stepdefs.today_is_Sunday()When I ask whether it's Friday yet # Stepdefs.i_ask_whether_is_s_Friday_yet()Then I should be told "Nope"# Stepdefs.i_should_be_told(String)1 Scenarios (1 passed)3 Steps (3 passed)0m0.255s

恭喜你!这是第一个成功(passing)的Cucumber Scenario。

添加另一个失败的测试

下一件要测试的事情是,我们也会在周五得到正确的结果。

更新is-it-friday-yet.feature文件:

Feature: Is it Friday yet?Everybody wants to know when it's FridayScenario: Sunday isn't FridayGiven today is SundayWhen I ask whether it's Friday yetThen I should be told "Nope"Scenario: Friday is FridayGiven today is FridayWhen I ask whether it's Friday yetThen I should be told "TGIF"

我们需要添加一个步骤定义,将today设置为“Friday”:

@Given("^today is Friday$")public void today_is_Friday() {this.today = "Friday";}

当我们运行这个测试时,它将失败。

Running hellocucumber.RunCucumberTestFeature: Is it Friday yet?Everybody wants to know when it's FridayScenario: Sunday isn't Friday# hellocucumber/isitfriday.feature:4Given today is "Sunday" # Stepdefs.today_is(String)When I ask whether is's Friday yet # Stepdefs.i_ask_whether_is_s_Friday_yet()Then I should be told "Nope"# Stepdefs.i_should_be_told(String)Scenario: Friday is Friday# hellocucumber/is_it_friday.feature:9Given today is "Friday"# Stepdefs.today_is(String)When I ask whether is's Friday yet # Stepdefs.i_ask_whether_is_s_Friday_yet()Then I should be told "TGIF"# Stepdefs.i_should_be_told(String)org.junit.ComparisonFailure:expected:<[TGIF]> but was:<[Nope]>at org.junit.Assert.assertEquals(Assert.java:115)at org.junit.Assert.assertEquals(Assert.java:144)at hellocucumber.Stepdefs.i_should_be_told(Stepdefs.java:26)at ?.I should be told "TGIF"(hellocucumber/is_it_friday.feature:12)org.junit.ComparisonFailure:Expected :TGIFActual :Nope<Click to see difference>at org.junit.Assert.assertEquals(Assert.java:115)at org.junit.Assert.assertEquals(Assert.java:144)at hellocucumber.Stepdefs.i_should_be_told(Stepdefs.java:26)at ?.I should be told "TGIF"(hellocucumber/is_it_friday.feature:12)

那是因为我们还没有实现逻辑!我们接着做。

让它通过

我们应该更新我们的语句来实际评估Today是否等于“Friday”。

static String isItFriday(String today) {if (today.equals("Friday")) {return "TGIF";}return "Nope";}

再次运行mvn test:

-------------------------------------T E S T S-------------------------------------Running hellocucumber.RunCucumberTestFeature: Is it Friday yet?Everybody wants to know when it's FridayScenario: Friday is Friday# hellocucumber/is_it_friday_yet.feature:4Given today is Friday # Stepdefs.today_is_Sunday()When I ask whether it's Friday yet # Stepdefs.i_ask_whether_is_s_Friday_yet()Then I should be told "TGIF"# Stepdefs.i_should_be_told(String)Scenario: Sunday isn't Friday# hellocucumber/is_it_friday_yet.feature:4Given today is Sunday # Stepdefs.today_is_Sunday()When I ask whether it's Friday yet # Stepdefs.i_ask_whether_is_s_Friday_yet()Then I should be told "Nope"# Stepdefs.i_should_be_told(String)2 scenarios (2 passed)6 steps (6 passed)0m0.255s

使用变量和实例

所以,我们都知道一周中不止周日和周五。让我们更新我们的scenario以使用变量并评估更多的可能性。我们将使用变量和示例来计算星期五、星期天和其他任何时间! 更新is-it-friday-yet.feature文件。注意,当我们开始使用多个Examples时,我们是如何从一个Scenario切换到Scenario Outline的。

Feature: Is it Friday yet?Everybody wants to know when it's FridayScenario Outline: Today is or is not FridayGiven today is "<day>"When I ask whether it's Friday yetThen I should be told "<answer>"Examples:| day | answer || Friday | TGIF || Sunday | Nope || anything else! | Nope |

我们需要用一个以为字符串的步骤定义来替换today is Sunday和today is Friday的步骤定义。更新stepdefs.java文件如下:

package hellocucumber;import cucumber.api.java.en.Given;import cucumber.api.java.en.When;import cucumber.api.java.en.Then;import static org.junit.Assert.*;class IsItFriday {static String isItFriday(String today) {if (today.equals("Friday")) {return "TGIF";}return "Nope";}}public class Stepdefs {private String today;private String actualAnswer;@Given("^today is \"([^\"]*)\"$")public void today_is(String today) {this.today = today;}@When("^I ask whether it's Friday yet$")public void i_ask_whether_is_s_Friday_yet() {this.actualAnswer = IsItFriday.isItFriday(today);}@Then("^I should be told \"([^\"]*)\"$")public void i_should_be_told(String expectedAnswer) {assertEquals(expectedAnswer, actualAnswer);}}

再次执行mvn test:

-------------------------------------T E S T S-------------------------------------Running hellocucumber.RunCucumberTestFeature: Is it Friday yet?Everybody wants to know when it's FridayScenario Outline: Today is or is not Friday # hellocucumber/is_it_friday_yet.feature:4Given today is <day># hellocucumber/is_it_friday_yet.feature:5When I ask whether it's Friday yet # hellocucumber/is_it_friday_yet.feature:6Then I should be told <answer># hellocucumber/is_it_friday_yet.feature:7Scenario: Sunday isn't Friday# hellocucumber/is_it_friday_yet.feature:4Given today is Sunday # Stepdefs.today_is_Sunday()When I ask whether it's Friday yet # Stepdefs.i_ask_whether_is_s_Friday_yet()Then I should be told "Nope"# Stepdefs.i_should_be_told(String)Examples:| day | answer || "Friday" | "TGIF" || "Sunday" | "Nope" || "anything else!" | "Nope" |3 scenarios (3 passed)9 steps (9 passed)0m0.255s

重构

现在我们有了工作代码,我们应该做一些重构:

我们应该将isItFriday方法从测试代码移到生产代码中。 我们可以在某个时候从步骤定义中提取helper方法,用于我们在几个地方使用的方法。

星云测试

http://www.teststars.cc

奇林软件

http://www.kylinpet.com

联合通测

http://www.quicktesting.net

本文分享自微信公众号 - 软件测试培训(iTestTrain)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-11-14

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Java网络编程与NIO详解8:浅析mmap和Direct Buffer

    本文转自:https://www.cnblogs.com/huxiao-tee/p/4660352.html

    Java技术江湖
  • 一文详尽系列之CatBoost

    CatBoost是俄罗斯的搜索巨头Y andex在2017年开源的机器学习库,也是Boosting族算法的一种,同前面介绍过的XGBoost和LightGBM类...

    Datawhale
  • Java递归实现字符串的排列和组合

    我们在笔试中经常会遇到需要对字符串进行排列或者组合的题目。本篇文章对字符串的排列和组合进行递归版本的实现。

    乔戈里
  • Find 查找命令时过滤掉某些文件或目录 以及 -maxdepth、-mindepth的用法

    1)find过滤目录 使用find命令在linux系统中查找文件时,有时需要忽略某些目录,可以使用"-path 过滤的目录路径 -prune -o"参数来进行过...

    洗尽了浮华
  • Python进阶之Matplotlib入门(七)

    Matplotlib是Python的画图领域使用最广泛的绘图库,它能让使用者很轻松地将数据图形化以及利用它可以画出许多高质量的图像,是用Python画图的必备技...

    HuangWeiAI
  • 面试必备:HashMap、Hashtable、ConcurrentHashMap的原理与区别

    Hashtable和HashMap都实现了Map接口,但是Hashtable的实现是基于Dictionary抽象类的。Java5提供了ConcurrentHas...

    用户6182664
  • 【从零学习OpenCV 4】图像像素统计

    经过几个月的努力,小白终于完成了市面上第一本OpenCV 4入门书籍《从零学习OpenCV 4》。为了更让小伙伴更早的了解最新版的OpenCV 4,小白与出版社...

    小白学视觉
  • 通过 20 个棘手的ES6面试问题来提高咱们的 JS 技能

    ECMAScript 6(以下简称ES6)是 JS 语言的下一代标准,已经在2015年6月正式发布了。它的目标,是使得 JS 语言可以用来编写复杂的大型应用程序...

    前端达人
  • Springboot自动装配整理

    再模仿Spring Cloud Feign源码解析 中的@EnableFeignClients代码写一个我们自己的标签

    算法之名
  • 手把手教你定制标准 Spring Boot starter

    我们每次构建一个 Spring 应用程序时,我们都不希望从头开始实现具有「横切关注点」的内容;相反,我们希望一次性实现这些功能,并根据需要将它们包含到任何我们要...

    黄泽杰

扫码关注云+社区

领取腾讯云代金券