自动化测试工具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 cucumbers //Given关键字,进行接口请求

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

Then I should have 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 cucumbers //Given关键字,进行接口请求

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

Then I should have 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.测试报告存放位置:用于测试报告的存放,接口文档的存放。

最后: 可以在公众号:伤心的辣条 ! 自行领取一份216页软件测试工程师面试宝典文档资料【免费的】。以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。

学习技术千万不要孤军奋战,最好是能抱团取暖,相互成就一起成长,群众效应的效果是非常强大的,大家一起学习,一起打卡,会更有学习动力,也更能坚持下去。你可以加入我们的测试技术交流扣扣群:914172719(里面有各种软件测试资源和技术讨论)

喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一键三连哦!


好文推荐

转行面试,跳槽面试,软件测试人员都必须知道的这几种面试技巧!

面试经:一线城市搬砖!又面软件测试岗,5000就知足了…

面试官:工作三年,还来面初级测试?恐怕你的软件测试工程师的头衔要加双引号…

什么样的人适合从事软件测试工作?

那个准点下班的人,比我先升职了…

测试岗反复跳槽,跳着跳着就跳没了…

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Book Description: You can test just about anything with Cucumber. We certainly have, and in Cucumber Recipes we’ll show you how to apply our hard-won field experience to your own projects. Once you’ve mastered the basics, this book will show you how to get the most out of Cucumber–from specific situations to advanced test-writing advice. With over forty practical recipes, you’ll test desktop, web, mobile, and server applications across a variety of platforms. This book gives you tools that you can use today to automate any system that you encounter, and do it well. The Cucumber Book showed you how your team can work together to write executable specifications–documents that tell a clear story and also happen to be working test code. We’ll arm you with ready-rolled solutions to real-world problems: your tests will run faster, read more clearly, and work in any environment. Our first tips will help you fit Cucumber into your workflow. Powerful filters will tame tables full of test data, transforming them into the format your application needs. Custom output formatters will generate reports for any occasion. Continuous Integration servers will run your Cucumber tests every time the code changes. Next, you’ll find recipes tailored to the platform you’re running on. Ever wanted to know how to test a Grails app from Cucumber? Need to put a Windows program through its paces? How about a mobile app running on Android or iOS? We’ll show you how to do all of these. Throughout the book, you’ll see how to make Cucumber sing as you interoperate with different platforms, languages, and environments. From embedded circuits to Python and PHP web apps, Cucumber has something for you.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值