Cucumber命令行接口

 

1. cucumber的命令行选项html

首先查看命令行选项。和其它命令行工具同样,cucumber提供了—help选项。下面是cucumber帮助的一个缩减版本:ios

$ cucumber --helpjson

-r,      --require LIBRARY|DIR    Require files before executing the features.浏览器

          --i18n LANG   List keywords for in a particular language.Run with "--i18n help" to  see all languages.ruby

-f, --format FORMAT    How to format features (Default: pretty).服务器

-o, --out [FILE|DIR]   Write output to a file/directory instead of STDOUT.工具

-t, --tags TAG_EXPRESSION     Only execute the features or scenarios with tags matching TAG_EXPRESSION.ui

-n, --name NAME    Only execute the feature elements which match part of the given  name.命令行

-e, --exclude PATTERN      Don't run feature files or require ruby files matching PATTERNorm

-p, --profile PROFILE     Pull command line arguments from cucumber.yml.

-P, --no-profile     Disables all profile loading to avoid using the 'default' profile.

-c, --[no-]color    Whether or not to use ANSI color in the output.

-d, --dry-run      Invokes formatters without executing the steps.

-a, --autoformat DIR    Reformats (pretty prints) feature files and writes them to DIR.

-m, --no-multiline   Don't print multiline strings and tables under steps.

-s, --no-source     Don't print the file and line of the step definition with the steps.

-i, --no-snippets     Don't print snippets for pending steps.

-q, --quiet      Alias for --no-snippets --no-source.

-b, --backtrace                                                 -S, --strict        Fail if there are any undefined or pending steps.

-w, --wip         Fail if there are any passing scenarios.

-v, --verbose      Show the files and features loaded.

-g, --guess    Guess best match for Ambiguous steps.

-l, --lines LINES        Run given line numbers. Equivalent to FILE:LINE syntax

-x, --expand       Expand Scenario Outline Tables in output.

      --drb         Run features against a DRb server. (i.e. with the spork gem)

      --port PORT      Specify DRb port.Ignored without --drb

      --version      Show version.

-h, --help            You're looking at it.

 

2. 运行一部分场景

当场景和feature逐渐增加时,咱们会常常运行仅仅一个(或多个)场景,这样会更快的反馈。当咱们在一个新场景中工做时颇有用。让咱们看看如何这样工做:

使用Tag表达式过滤

使用tag选择最简单的方法是给它一个单独的tag,例如:这个命令会让cucumber运行使用@focus标志的场景

$ cucumber –tags @focus 须要运行有@focus和@email标志的全部场景

$ cucumber –tags @focus,@email

应用逻辑AND来代表咱们想要运行由@fast而且由@focus或者@email标志的场景:

$ cucumber –tags @fast –tags @focus,@email

这里咱们使用—tags两次。这两个会使用AND逻辑连在一块儿。 少数场景标志@slow是更有意义的。若是想要运行全部没有标志@slow的场景,使用波浪符号:

$ cucumber –gets ~@slow –tags @focus,@email

下面介绍运行一部分场景的另外一种方法。

在line上过滤 Cucumber提供一个方便的方法指定你要运行的场景的行号。例如:

$ cucumber features/something.feature –line 45

若是文件超过一个场景,它就仅仅运行在45行的那个场景。

你可能注意到cucumber打印feature的位置和行号。

features/something.feature:45

若是要从新运行,可使用以下命令:

$cucumber features/something.feature:45

这个命令和使用—line参数效果相同。冒号容许你指定多个行号,容许你指定多个场景来运行:

$ cucumber features/something.feature:45:89:107

若是你喜欢,你也可使用—lines参数来实现这个功能。Cucumber不关心你使用line仍是lines。

使用名称过滤

若是tag和line过滤都不能知足你的要求,你可使用名称来过滤。例如,你要运行名称中包含logout的场景,这些场景分布在多个feature文件中,而且没有一个特殊的tag来指定他们。使用下面的方法运行他们:

$ cucumber –name logout

你也可使用—exclude指定一个名称来排除场景。

3. 改变cucumber的输出

Cucumber的默认行为是输出和Gherkin源文件相似的结果—附带一些颜色、step定义位置和参数高亮的信息。这并非输出结果仅有的方式。Cucumber容许你使用一个不一样的输出格式。例如:你可使用最小的报告,每一个step只包含一个字符,就是progress格式:

$ cucumber --format progress

..U--..F..

每一个字符表示每一个step的状态:

l          .意味着经过

l          U意味着未定义

l          -意味着忽略

l          F意味着失败

Cucumber也有一些内置的格式:html,json和junit。当你在一个持续集成环境中运行cucumber时,junit是很方便的,由于大多数持续集成服务器知道如何解释JUnit报告。下面更详细。

特殊的格式器

Cucumber绑定了一些格式,能够建立输出,但这些输出并不真是用来做为运行报告的。这个格式更可能是来帮助开发的。

Usage格式列出你的工程中全部的step定义,和使用step定义的step。它显示了没有使用的step定义,使用平均执行时间对step定义进行排序。Usage的输出能够很快找出缓慢的部分,也是一个对你的step定义全面把握的好方法。

Stepdefs格式和usage很想,信息有点减小。

最后还有一个rerun格式。这是一个特殊的格式:

$ cucumber -f rerun

若是全部的场景都经过,rerun格式不会输出任何信息。然而,若是有失败场景,它会输出他们的位置,你能够拷贝和粘贴输出来从新运行场景。当确认失败场景时,这个选项能够节省不少时间。

为文件指定格式和使用多个格式

一般,全部格式都会打印到输出窗口。所以,若是咱们想要看一般的pretty输出,可是想要一个html或者rerun格式怎么办呢?使用—out选项便可。它告诉cucumber不要输出到控制台,输出到文件中。例如:

$ cucumber -f pretty -f html --out cukes.html -f rerun --out rerun.txt

这个命令告诉cucumber编写HTML报告到cukes.html中,编写rerun报告到rerun.txt文件中,最终显示pretty格式到控制台上。

显示完整回溯堆栈

使用—backtrace选项致使cucumber为每一个失败打印完整的回溯调用。当你在查找bug时很是有用。

4. 指定step定义的位置

你有没有奇怪:cucumber如何找到你的step定义?cucumber只会在featues/step_definitions中指望找到他们。事实上,若是你重命名这两个目录为jolly/jumper,运行cucumber jolly,也能正常运行。

Cucumber递归地扫面一个目录的rb文件来加载,若是在他们里面有step定义,他们就会被加载。理解什么目录会被cucumber扫描是很重要的。一旦你理解了它,你就从疑惑中解放出来。演示一个例子,加入你这样管理你的feature和step定义:

features

├── billing

│      └── credit_card.feature

├── scoring

│      ├── multi_player.feature

│      └── single_player.feature

└── step_definitions

├── billing_steps.rb

└── scoring_steps.rb

若是你使用cucumber features或者cucumber来运行,一切都很正常。若是有一天你添加一个新场景到credit_card.feature来支持一个新credit卡。而后运行:

$ cucumber features/billing/credit_card.feature:104 -f progress

UUUU

全部的step都是未定义的,即便你的新场景都是重用的已存在的step定义。为step定义和支持代码扫描的目录是由feature文件决定的。若是你给了一个feature文件,它就会在它的目录下查找rb文件。若是你给它一个目录,它就会在那个目录下查找。你能够经过—verbose选项查看cucumber在哪里寻找代码。

解决这个问题的方案是使用—require来显式告诉cucumber到哪儿去加载代码。前一个例子正确的方式是:

$ cucumber features/billing/credit_card.feature:104 -f progress -r features

...F

咱们的代码如今就能找到而且加载了,咱们能够继续开发咱们的新场景。你也能够经过一个自定义配置文件来实现它。

5. 在Progress中管理你的工做

你应该尽可能避免存在大量的pending或者半成品的feature和场景。这会让咱们很难保持注意力,它对每一个feature的交付周期也有影响。对于开始启动但未完成的工做,有一个术语叫work in progress,简称为WIP。若是你的项目保持WIP的低数量,每一个feature的周期时间也会更短。

Cucumber帮助你管理你的WIP,使用tag和—wip选项。尽可能养成为每一个场景的习惯,正在进行时标记@wip,经过时移除这个tag。若是你的项目组赞成一直保持WIP少于3个。可使用下列命令强制执行:

$ cucumber –wip @wip:3

这也有一些有趣的影响。首先若是cucumber发现超过3个@wip场景,它会当即失败,不会执行任何场景。这个标志说明这里有太多的场景正在工做中,一些场景须要经过(或者移除掉@wip),另外一些才能添加进来。

另外一个影响是:任何场景经过(记住,你仅仅运行@wip的场景),cucumber也会失败。这种失败提醒你移除@wip标志。

6. 使用profiles

当你对不一样的命令行选项感兴趣后,试着使用更多的选项,在命令行中一遍又一遍地输入他们是很枯燥的。Cucumber容许你存储命令行选项,保存到一个cucumber.yml文件中。 这个文件必须放在你工程目录下或者在config目录下。下面是一个例子:

default: --tags ~@wip --require features

wip:  --tags @wip:3 --wip --require features

如今,若是你运行cucumber –profile wip,关键字wip的命令行选项会自动附加到你的命令行中。若是你没有指定—profile,cucumber会使用default的选项。注意:咱们添加了—require features到全部profile中。由于,咱们想要让cucumber自动加载definitions和支持代码,无论咱们想要运行哪一个文件夹中的feature。

7. 从Rake中运行cucumber

不少项目使用构建工具来执行经常使用任务。对于ruby工程,Rake是实际上的构建工具。Cucumber有一个Rake任务,使得从rake中运行cucumber很容易。若是你使用cucumber-Rails,自动安装时,你就会有Rake任务。

若是你尚未使用Rake,本身安装很容易。在你的工程根目录下添加这个到Rakefile文件中。

require 'cucumber/rake/task'

Cucumber::Rake::Task.new # defines a task named cucumber

如今你能够从Rake中运行Cucumber:

$ rake cucumber

当你从Rake中运行cucumber时,cucumber.yml中的profile会自动执行,可是你能够在Rakefile中定义命令行选项:

Cucumber::Rake::Task.new('cucumber_progress') do |t|

t.cucumber_opts = %w{--format progress}

end

如今你知道从本地命令行和Rake中两种方式如何运行cucumber,你也可使用你的持续集成环境来运行它。若是任何场景失败,Cucumber会有1做为状态码,所以你的持续集成服务能够正确检测失败。

8. 在CI(持续集成)中运行cucumber

不少项目组安装他们的CI来每次运行cucumber,互相共享改变。所以这是一个命令行工具,没有任何须要作的,仅仅须要把cucumber命令放入到CI工程的配置下就好了。

你可能想要为CI使用一些不一样的命令行选项。你能够经过定义一个特殊的profile。

Being Strict

当咱们开发一个新场景时,存在未实现的或者pending的step是很正常的。一些项目组尽力保存主线上没有这样的step。

CI系统经过检查进行的推出状态来检测失败。若是有一个或多个失败的step,默认cucumber仅仅存在一个失败状态。若是你在CI环境中运行Cucumber,若是有人添加了未实现的step或者pending的step,你可能想要它失败。

若是咱们传人—strict选项,若是未实现的或者pending的step就会和failing同样,返回非0值。你能够在运行cucumber后检查返回值。

# OS X or Linux

$ echo $?

# Windows

$ echo %ERRORLEVEL%

共享报告

前面咱们看到如何使用—format选项改变cucumber的输出。若是传入--out JUNIT_DIR --format junit到cucumber中,你能够配置CI服务器来从JUNIT_DIR中获取报告,而后分析。一些CI服务器能够生成趋势图,向你展现你的构建是如何变得健康的。这是一个很好的方法:检测你的项目是进化了仍是倒退了。

你也可使用--out HTML_FILE --format html来生成HTML报告。若是你有浏览器自动化工具,这就很是好用,由于你能够嵌入浏览器的截图到HTML报告中。

相关文章
相关标签/搜索