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

发表于:2021-9-17 09:38

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:恋空    来源:51Testing软件测试网原创

  背景介绍
  随着测试的发展,测试自动化越来越成为人们的关注点。
  现在我们公司也在进行接口自动化的推广,在我看来接口自动化的价值就在于整体项目的回归,完成一些没法通过人力进行的测试,比如压力测试
  为了解决测试开发人员和功能测试人员的同步问题,选择了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编写对应的测试代码。
  示例如下:
```java
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插件创建一个新的项目目录。
```powershell
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。

```java
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文件即可。
```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.测试报告存放位置:用于测试报告的存放,接口文档的存放。

  版权声明:本文出自51Testing会员投稿,51Testing软件测试网及相关内容提供者拥有内容的全部版权,未经明确的书面许可,任何人或单位不得对本网站内容复制、转载或进行镜像,否则将追究法律责任。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号