自动化测试工具Cucumber的简单介绍

背景介绍

随着测试的发展,测试自动化越来越成为人们的关注点。

现在我们公司也在进行接口自动化的推广,在我看来接口自动化的价值就在于整体项目的回归,完成一些没法通过人力进行的测试,比如压力测试。

为了解决测试开发人员和功能测试人员的同步问题,选择了Cucumber框架。

Cucumber是一个能够理解用普通语言描述测试用例的行为驱动开发(BDD)的自动化测试工具。

换句话说就是学习成本比较低,并且可以方便测试开发人员和功能测试人员协同合作、开发人员进行公共方法的封装、功能测试人员进行测试用例的编写。

Cucumber组成

由Features、Step_definitions、Cucumber command组成。

Features

基于Gherkin,支持语言:# language: en (zh-CN)等;

Features文件必须以.features命名;

包含title,多个scenarios,每个scenario包含多个step。

示例如下:多组参数传参。
 


Features: test //Features关键字,测试用例集

Scenario Outline: eating //Scenario Outline关键字,测数用例

Given there are <start> cucumbers //Given关键字,进行接口请求

When I eat <eat> cucumbers //When关键字,数据准备

Then I should have <left> cucumbers //Then关键字

Examples:

| start | eat | left |

| 12 | 5 | 7 |

| 20 | 5 | 15 |

关键字详解:

  • Feature (功能):test suite (测试用例集)。
  • Scenario(情景):test case (测试用例)。
  • Scenario Outline (or Scenario Template):和examples更配。
  • Given(给定:setup(创建测试所需环境)。
  • When(当):test(触发被测事件)。
  • Then(则):assert(断言,验证结果)。
  • Background(背景):您会发现自己在一个功能的所有场景中重复相同的给定步骤,因为它在每个场景中都是重复的。

这表明这些步骤对于描述场景不是必需的,它们是附带的细节。您可以通过将这些给定的步骤分组到background部分,将它们移动到后台。

  • And(or But):如果你有连续的“给定”、“何时”或“然后”。
  • “”"(定义多行字符串):方便地将较大的文本段传递给步骤定义。
  • |(用来定义表格):数据表便于将值列表传递给步骤定义。
Step_definitions

Step定义必须以关键字Given、When、Then、And开始,根据正则匹配对应的关键字。

根据feature文件中定义的step编写对应的测试代码。

示例如下:
 


public class StepDefinition {

private String today;

private String actualAnswer;

@Given("^today is Sunday$") //和features中的Given对应

public void today_is_Sunday() {

today = "Sunday";

}

@When("^I ask whether it's Friday yet$") //和features中的When对应

public void i_ask_whether_is_s_Friday_yet() {

actualAnswer = IsItFriday.isItFriday(today);

}

@Then("^I should be told \"([^\"]*)\"$") //和features中的Then对应

public void i_should_be_told(String expectedAnswer) {

assertEquals(expectedAnswer, actualAnswer);

}

}

Cucumber command
运行*.feature文件,Cucumber会分析feature文件中定义的step,然后去step -definitions寻找相匹配的step,执行step中的代码。

运行结果以html的形式保存,fail的情况查看对应log日志。

Cucumber开发过程

1.首先使用Cucumber原型Maven插件创建一个新的项目目录。
 

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

项目目录如下:


2、在reources文件夹下,创建feature文件,包括feature、scenarios和step。


Feature: Is it Friday yet? //Features关键字,测试用例集

Scenario: Sunday isn't Friday //Scenario Outline关键字,测试用例

Given today is Sunday //Given关键字,进行接口请求

When I ask whether it's Friday yet //When关键字,数据准备

Then I should be told "Nope" //Then关键字

 3.在hellocucumber文件下创建step_definitions。


package hellocucumber;


import io.cucumber.java.en.Given;

import io.cucumber.java.en.Then;

import io.cucumber.java.en.When;

import static org.junit.Assert.*;


class IsItFriday {

static String isItFriday(String today) {

return "Nope";

}

}

public class StepDefinition {

private String today;

private String actualAnswer;

@Given("^today is Sunday$") //和features中的Given对应

public void today_is_Sunday() {

today = "Sunday";

}

@When("^I ask whether it's Friday yet$") //和features中的When对应

public void i_ask_whether_is_s_Friday_yet() {

actualAnswer = IsItFriday.isItFriday(today);

}

@Then("^I should be told \"([^\"]*)\"$") //和features中的Then对应

public void i_should_be_told(String expectedAnswer) {

assertEquals(expectedAnswer, actualAnswer);

}

}

 4.项目运行,在idea中直接运行hellocucumber文件夹下的Runcucumber.java文件即可。


import io.cucumber.junit.Cucumber;

import io.cucumber.junit.CucumberOptions;

import org.junit.runner.RunWith;


@RunWith(Cucumber.class)

@CucumberOptions(plugin = {"pretty"})

public class RunCucumberTest {


}

测试用例设计

测试用例设计时按接口文档给的标准生成数据,然后填充到如下图的examples中即可,框架会循环进行执行测试用例,生成测试结果。


Features: test //Features关键字,测试用例集

Scenario Outline: eating //Scenario Outline关键字,测试用例

Given there are <start> cucumbers //Given关键字,进行接口请求

When I eat <eat> cucumbers //When关键字,数据准备

Then I should have <left> cucumbers //Then关键字

Examples: //Examples关键字

| start | eat | left |

| 12 | 5 | 7 |

| 20 | 5 | 15 |
后期维护

后续迭代版本功能测试人员和测试开发人员分工进行,功能测试人员维护Features,也就是测试用例。

测试开发人员进行step_definitions的维护,就是一些代码逻辑和公共方法,最重要的也就是断言方法的改动比较大,接口请求就几乎是格式化的东西。

项目框架定制思路

1.测试前数据准备:类似于登录后获取请求头这种在里面进行实现。

2.测试用例数据:Features文件中存放。

3.逻辑处理,接口请求:封装到Step_definitions。

4.公共工具封装:一些数据库连接,yaml文件读取或者一些其他工具的存放地点。

5.框架配置信息:环境相关信息放置位置,不同城市、不同数据库、不同账号的切换在里面进行设置。

6.测试报告存放位置:用于测试报告的存放,接口文档的存放。

行动吧,在路上总比一直观望的要好,未来的你肯定会感 谢现在拼搏的自己!如果想学习提升找不到资料,没人答疑解惑时,请及时加入扣群:,里面有各种软件测试+开发资料和技术可以一起交流学习哦。

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值