工作原因,最近一直在研究cucumber的 语法以及它和java之间的关系。鉴于是初学者且代码基础薄弱,我开始摸索前行,感谢分享博客且也在一路前行的人儿们。

一、cucumber简介

1、介绍:

cucumber是一种可以使用文本描述语言来执行自动测试用例的工具,使用的语言叫做Gherkin.

Gherkin用于描述软件的行为而不需要了解具体的实现,使用Gherkin主要有两个目的文档和自动测试用例(我们希望能够和手工测试用例也统一)。Gherkin支持超过40种语言,包括英文、中文。Gherkin可以在任何地方新增注释,注释以#开头,每一个文件都是已.feature结尾,在feature文件中输入功能描述、场景、步骤,当执行这个功能时每一个步骤都需要编写ruby代码块来实现具体的功能,当前cucumber支持多种语言,除了ruby还可以使用java、javascript来编写具体定义层的实现。

2、关于Feature

每一个feature文件就是一个模块功能(如登录,注册:login.feature,此处为一个公共方法),Feature之后的描述作为标记整个ticket或者story也或者说是一个项目小模块的功能概述(如:这是一个增加、编辑功能),Scenario(场景),一个feature中可以有多个Scenario,每个Scenario包含一个到若干个step,步骤使用Given、When、Then、But、And这些关键词进行步骤连接。在执行测试用例时我们一般是从上到下进行执行(如:feature/Given 出错则一定会给出对应处的提示)

3、Step denfinitions 定义

在项目Feature中,每一个step(given when then)都有一个 step denfinition 来进行方法定义。一般来说我们可以通过自动的方式生成step definition,具体操作你点击Run查看 log栏下拉可看到报错信息中含有,然后拷贝好方法到指定的step definition中进行code 编写即可。

 

二、cucumber具体实现

1、基本语法为:此处举例两种区别一看即知->
1)简单一点
Scenario
Given
When
Then
2)复杂一点
Scenario
Given
When
And
And
Then
And
3)释义

Feature:用来描述我们需要测试的模块,模块1,2,3...
Scenario: 用来概述功能测试点 如:add/delete
Given:前置条件,比如用户在哪个页面进行操作?
When: 描述用户操作的执行动作,比如click/save
Then: 断言 表示执行的结果

But-一个步骤中如果存在多个Then操作,第二个开始后面的Then可以用But替代(注意是可以,也可以用Then)

 2、写测试用例范本
1)、Scenario:start new workflow step 1/3  ---一个功能,如登录,注册,类似于功能总结为步骤的第一步
Given choose a valid workflow -
-此处给出一个前提条件,和我们testcase一样的原理,可以有多个前提条件
When click"Start New" button -
-此处为动作,动作可以有多个如上第二个语法例子
Then widow prompt:Are you sure you want to start workflow for current responsible?--
-测试的预期结果,比如现象是什么样。此处在代码中可以使用断言

2)、Scenario:start new workflow step 2/3--
一个功能的总结步骤的第二步
Given start new work flow --同上
When choose "Yes" --同上
Then the workflow was started --同上

3)、Scenario:cancle new workflow step 3/3 --
一个功能总结步骤的第三步骤
Given start new work flow --同上
When choose "No" --同上
Then cancle the current window--同上
3、Scenario Outline 使用:
If there are many examples, this becomes tedious. We can simplify it with a Scenario Outline:
Scenario Outline: feeding a suckler cow
  Given the cow weighs <weight> kg
  When we calculate the feeding requirements
  Then the energy should be <energy> MJ
  And the protein should be <protein> kg

  Examples:
    | weight | energy | protein |
    |    450 |  26500 |     215 |
    |    500 |  29500 |     245 |
    |    575 |  31500 |     255 |
    |    600 |  37000 |     305 |
4、background

Occasionally you'll find yourself repeating the same Given steps in all of the scenarios in a feature file. Since it is repeated in every scenario it is an indication that those steps are not essential to describe the scenarios, they are incidental details.


You can literally move such Given steps to the background by grouping them under a Background section before the first scenario:

Background:
大意为:如果有多个案例中存在重复的given and ,可以使用backfround进行替换
Given a $100 microwave was sold on 2015-11-03 And today is 2015-11-18

5、Data Tables

Data Tables are handy for passing a list of values to a step definition:

Given the following users exist:
  | name   | email              | twitter         |
  | Aslak  | aslak@cucumber.io  | @aslak_hellesoy |
  | Julien | julien@cucumber.io | @jbpros         |
  | Matt   | matt@cucumber.io   | @mattwynne      |

Just like Doc Strings, Data Tables will be passed to the Step Definition as the last argument.

The type of this argument will be DataTable. See the API docs for more details about how to access the rows and cells.



签名:知识靠的是累积,我只是担心年纪大了,会忘事儿🙃 
posted on 2016-07-12 11:20  陌生初见  阅读(10313)  评论(8编辑  收藏  举报