a) Multi-line Text

  在feature文件中,我们可以嵌入多行文本(multi-line string)作为参数,我们需要用一对三个双引号把我们的文本括起来。《The RSpec Book》一书中的示例如下:

feature文件:

 1 Scenario: pending implementation 2   Given a file named "example_without_block_spec.rb" with: 3     """ 4     describe "an example" do 5         it "has not yet been implemented" 6     end 7     """ 8   When I run "spec example_without_block_spec.rb" 9   Then the exit code should be 010   And the stdout should include11     """12     Pending:13     an example has not yet been implemented \(Not Yet Implemented\)14     .\/example_without_block_spec.rb:215     Finished in ([\d\.]*) seconds16     1 example, 0 failures, 1 pending17     """

  在这个scenario中,Given和And(Then)两个步骤中都使用了多行文本字符串作为它们的参数。这可以带给我们很多灵活性,因为我们可以更精确的描述输入或输出数据在一个文件中的显示情况。

  关于字符串的缩进,取决于第一个双引号的位置,所以第四行的describle和第6行的end是左对齐,第5行的it会缩进两个空格。

step_definitions:

 1 Given /^a file named "([^"]*)" with:$/ do |filename, text| 2        puts "Given:\n#{filename}" 3        puts "Given:\n#{text}" 4 end 5 When /^I run "([^"]*)"$/ do |filename| 6        puts "When:\n#{filename}" 7 end 8 Then /^the exit code should be (\d+)$/ do |number| 9        puts "Then:\n#{number}"10 end11 And /^the stdout should include$/ do |text|12         puts "And:\n#{text}"13 end

  在这个step definitions文件中,我只是简单的把参数打印出来,当然我们也可以在这里做一些复杂的操作或处理。匹配step的正则表达式不关心这些多行文本字符串,它会在在step语句的最后一个字符处结束。

  Cucumber会把多行文本作为最后一个块参数传递给step definition。如在上面的step definition文件的第1行中,filename这个块参数的值会由正则式捕捉到,而text会得到多行文本的值。

results:

 1 Given: 2 example_without_block_spec.rb 3  4 Given: 5 describe "an example" do 6   it "has not yet been implemented" 7 end 8  9 When:10 spec example_without_block_spec.rb11 12 Then:13 014 15 And:16 Pending:17 an example has not yet been implemented \(Not Yet Implemented\)18 .\/example_without_block_spec.rb:219 Finished in ([\d\.]*) seconds20 1 example, 0 failures, 1 pending

  这里只是简单的输出了多行文本,大家注意下缩进的问题,只有第6行的it空了两个空格,其它都是顶格。因为其它的每行开头都和第一个双引号在同一垂直位置。

  另外一个例子:http://asymmetrical-view.com/2011/06/02/cucumber-gherkin-and-multiline-arguments.html 

 

b) Tables in Steps

  Cucumber支持在steps中传递表格数据,这种方式常用于Given和Then这两个步骤中。看《The RSpec Book》中的示例:

feature文件:

 1 Feature: test table in steps 2  3 Scenario: three of a kind beats two pair 4   Given a hand with the following cards: 5     | rank | suit | 6     | 2     | H | 7     | 2     | S | 8     | 2     | C | 9     | 4     | D |10     | A     | H |11   And another hand with the following cards:12     | rank | suit |13     | 2     | H |14     | 2     | S |15     | 4     | C |16     | 4     | D |17     | A     | H |18   Then the first hand should beat the second hand

  在这个feature文件中的Given和And(Given)步骤中,定义了两个表格。当cucumber碰到一个step(Given or Then)的下一行是以“|”开头时,cucumber会解析它和它之后的所有以"|"开头行,并把这些单元格里面的数据存储到Cucumber::Ast::Table 对象里面。我们可以使用hashes()方法得到一个哈希数组。

  数组里的每一个哈希都使用表格中的第一行作为key,如:

1 [2     { :rank => '2', :suit => 'H' },3     { :rank => '2', :suit => 'S' },4     { :rank => '4', :suit => 'C' },5     { :rank => '4', :suit => 'D' },6     { :rank => 'A', :suit => 'H'}7 ]

  和多文本参数一样,cucumber会把Cucumber::Ast::Table 作为最后一个块参数传递给step definition。

step definitions文件:

 1 Given /^a hand with the following cards:$/ do |table| 2   # table is a Cucumber::Ast::Table 3   p table 4   p table.hashes 5 end 6 And /^another hand with the following cards:$/ do |table| 7   # table is a Cucumber::Ast::Table 8     table.hashes.each do |value| 9        puts "#{value}"10     end11 end12 Then /^the first hand should beat the second hand$/ do13   #do nothing14 end

  在这个文件里,做了些简单的操作。在Given中使用P来监视参数table和table.hashes。在And(Given)中则会用each方法输出参数。

results:

 1   |     rank |     suit | 2   |     2    |     H    | 3   |     2    |     S    | 4   |     2    |     C    | 5   |     4    |     D    | 6   |     A    |     H    | 7  8 [{"rank"=>"2", "suit"=>"H"}, {"rank"=>"2", "suit"=>"S"}, {"rank"=>"2", "suit"=>"C"}, {"rank"=>"4", "suit"=>"D"}, {"rank"=>"A", "suit"=>"H"}] 9 10 {"rank"=>"2", "suit"=>"H"},11 12 {"rank"=>"2", "suit"=>"S"},13 14 {"rank"=>"4", "suit"=>"C"},15 16 {"rank"=>"4", "suit"=>"D"},17 18 {"rank"=>"A", "suit"=>"H"},

  看上面的结果:

    1.第1-6行是p table的输出结果,跟定义的一样,是一个表格形式的数据集合。

    2.第8行是p table.hashes的输出结果,是一个哈希数组。

    3.第9-18行是迭代输出哈希数组。

c) Scenario Outlines

  这个Scenario Outlines挺有用的。当我们一个feature文件里有多个scenario的时候,而且每个scenario的步骤都差不多,只是测试的数据不同。在这种情况下我们就可以使用Scenario Outlines,定义一个scenario,然后把测试数据参数化就可以了。继续看《The RSpec Book》中的示例:

feature 文件:

 1 Feature: test scenario outline 2  3 Scenario: test one 4 Given the secret code is 1234 5 When I guess 1234 6 Then the mark should be bbbb 7  8 Scenario: test two 9 Given the secret code is 123410 When I guess 123511 Then the mark should be bbb12 13 Scenario: test three14 Given the secret code is 123415 When I guess 123616 Then the mark should be bbb

  在这个feature文件里面,我们有三个scenario(可能会有更多),它们的区别仅仅是测试数据不同。这样写有很多缺点,比如不易读,违反DRY原则等等。

  Scenario outliness可以帮我们解决这个问题,让我们只定义一次scenario,然后使用占位符来代替可能不断在改就的值。然后我们可以把那些变化的值以一样表格的形式组织起来。

改进后的feature文件

 1 Scenario Outline: submit guess 2   Given the secret code is "<code>" 3   When I guess "<guess>" 4   Then the mark should be "<mark>" 5  6 Scenarios: all numbers correct 7   | code | guess | mark | 8   | 1234 | 1234 | ++++ | 9   | 1234 | 1243 | ++-- |10   | 1234 | 1423 | +--- |11   | 1234 | 4321 | ----|

  这样编写feature文件,使得我们很容易阅读,同时也给我们了一个大纲,让我们清晰的知道这个scenario的什么用。

  关键字Scenarios(Examples)定义了一个输入数据的table,这样cucumber会处理除开第一行(在这是第7行)的每第行数据,
此例中总共执行四次, 所以实际上会有四个scenario。

  这种参数化的方式也可以使用在multi-line text 和 table in steps中,继续看示例:

feature文件:

 1 Feature: Test scenario outline 2  3 Scenario Outline: 4   Given a discount of <discount> 5   When I order the following book: 6     | title | price | 7     | Healthy eating for programmers | <price> | 8   Then the statement should read: 9     """10     Statement for David11     Total due: <total>12     """13 14 Scenarios:15     | discount | price | total |16     | 10% | $29.99 | $26.99 |17     #| 15% | $29.99 | $25.49|

  在这个文件中,Given,When和Then分别使用了三种不同的方式传递参数,但都使用的参数化:

    Given:参数化

    When:table in steps + 参数化

    Then:   multi-line text + 参数化

step definition文件:

 1 Given /^a discount of (\d+)%$/ do |discount| 2       p discount 3 end 4  5 When /^I order the following book:$/ do |table| 6   # table is a Cucumber::Ast::Table 7      p table 8 end 9 Then /^the statement should read:$/ do |string|10      p string11 end

  在该文件中,只是简单的使用p输出了参数,结果如下。

results:

1 "10"2 3   |     title                          |     price  |4   |     Healthy eating for programmers |     $29.99 |5 6 "Statement for David\nTotal due: $26.99"

  占位符的地方被实际值代替后,得到上面的结果。

注:本文大部分内容来自《The RSpec Book》一书,仅共学习之用,如需要了解更多内容,请看原文。 


转载于:https://www.cnblogs.com/puresoul/archive/2012/03/06/2382135.html

Cucumber入门之_argument相关推荐

  1. Cucumber入门之_World

    1. World: World可以看做是Cucumber在运行每个场景之前所要创建的对象的实例,它不仅使得每一个Step Definition可以调用该实例的方法,而且使得为该项目定义的Ruby类是也 ...

  2. Cucumber 入门一

    (转自:http://www.cnblogs.com/jarodzz/archive/2012/07/02/2573014.html) 第一次看到Cucumber和BDD(Behavior Drive ...

  3. cucumber入门

    1.Cucumber是什么       Cucumber是一个在敏捷团队十分流行的自动化的功能测试工具,但是其不仅仅是一个测试工具,它能够为我们建立一个易读的,可执行的特性文档. 2.Cucumber ...

  4. cucumber 入门笔记

    参考文章: https://www.jianshu.com/p/3857f2c3a8d4 Feature: Is it friday yet?this is a descriptionsEverybo ...

  5. hue集成mysql报错_hue集成hive访问报database is locked

    这个问题这应该是hue默认的SQLite数据库出现错误,你可以使用mysql postgresql等来替换 hue默认使用sqlite作为元数据库,不推荐在生产环境中使用.会经常出现database ...

  6. cucumber java从入门到精通_cucumber java从入门到精通(4)Scenario Outline及数据驱动...

    cucumber java从入门到精通(4)Scenario Outline及数据驱动 到目前为止,我们的TodoList类工作良好,不过离我们的预期--任务清单系统还是有不少差距,究其原因不过如下: ...

  7. cucumber java 实例_cucumber java从入门到精通(3)简单实现及断言

    cucumber java从入门到精通(3)简单实现及断言 上一节里我们定义了step的java代码实现文件,step就是测试步骤及断言的集合,我们先定义出来,以后可以驱动开发以及在持续集成时重用. ...

  8. 自动化测试框架cucumber_BDD测试框架之Cucumber使用入门

    ▼ 关注测试局| 会上瘾 1什么是Cucumber cucumber早在ruby环境下应用广泛,作为BDD框架的先驱,cucumber后来被移植到了多平台,简单来说cucumber是一个测试框架,就像 ...

  9. cucumber java hooks_Cucumber入门之_HooksBackground

    Hooks&  Background Hooks 在很多情况下,我们需要在每个scenario之前(before)和之后(after)执行某些相同的操作.比如说在测试完成后要关闭浏览器.在Cu ...

最新文章

  1. android调试更换模拟器,在模拟器上调试 Android 磨损
  2. Android Studio开发RecyclerView遇到的各种问题以及解决
  3. 【Android NDK 开发】NDK 交叉编译 ( NDK 函数库目录 | Linux 交叉编译环境搭建 | 指定头文件目录 | 指定函数库目录 | 编译 Android 命令行可执行文件 )
  4. 深入理解C程序内存布局
  5. 65%的家庭有人“啃老”,数据解读国内版巨婴是如何炼成的?
  6. DenyHosts教程:防暴力破解SSH密码
  7. python中下划线开头的命名_Python标识符规则 行与缩进 注释
  8. 最便宜、最快和最可靠不可兼得
  9. Tile:一个崭新出炉的机器学习语言
  10. 查询数据进行排名,一样的并列
  11. Git(4):提交代码时忽略不必要的文件或文件夹
  12. ubuntu下Makefile:xxx: recipe for target ‘xxx‘ failed
  13. 中国企业家:TD-SCDMA的坎坷商业路
  14. 云原生时代的镜像分发工具——Dragonfly简介
  15. webbug靶场-渗透基础
  16. angular directive 入门
  17. JAVA常用工具类-【6】邮箱发送
  18. 4、乐趣国学—“满招损,谦受益。”
  19. PTA-C理论B类题库6-3使用函数求最大公约数(辗转相除法的实现)
  20. Visual Studio 2008 项目安装和部署

热门文章

  1. 每天一道LeetCode-----计算最长的元素连续序列长度
  2. 每天一道LeetCode-----计算一个直方图空隙的容量(如果装水能装多少)
  3. 数据结构-----二叉树,树,森林之间的转换
  4. php json返回sql,php – 如何从我的特定SQL查询中返回json?
  5. 编译错误 无法打开包括文件:“SDKDDKVer.h”: No such file or directory
  6. linux中断的上半部和下半部
  7. pixhawk commander.cpp的飞行模式切换解读
  8. 信息熵与信息增益的理解
  9. CF-527E(Data Center Drama) 欧拉图+构造
  10. 4.如果容器中包含了通过new操作创建的指针,切记在容器对象析构前将指针delete掉