目录

回顾

在上一节中,我介绍了cucumber一些基本的语法内容,如果你还没有进行相关的了解或者环境的配置,你可以点击这里来进行了解一下

在本节中,我会对cucumber的一些常用的命令来进行介绍

基本命令的执行

先回顾一下我们之前在执行cucumber用例的时候,运行的命令是 cucumber features/hello_cucumber.feature,因为之前只是涉及到一个文件而已,一个文件中也只有一个用例,所以还是十分的简单的。我们看看下面的一些常用的执行方式。在介绍另外的执行方式之前,我们先写几个文件,让我们的用例更充实一些,以至于看起来更真实一些。

目录如下:

 

login.feature:

Feature: Login
  Login system should work fine

  @LoginWithRightUsernameAndRightPassword
  Scenario: Login with right username and right password
    Given I open the login page
    When I set username with a right username
    And I set password with a right password
    And I click the login button
    Then I see login success


  @LoginWithRightUsernameAndErrorPassword
  Scenario: Login with right username and error password
    Given I open the login page
    When I set username with a right username
    And I set password with a error password
    And I click the login button
    Then I see login fail

register.feature:

Feature: Register
  Register system should work fine

  @Register
  Scenario: Register
    Given I open the Register page
    When I fill textboxs on the Register page
      | id       | value |
      | username | fan   |
      | password | 123   |
    And I click the register button
    Then I see register success

common_step.rb:

Given /^I launch the browser$/ do
  puts "launch browser"
end

login.rb:

Given /^I open the login page$/ do
    step "I launch the browser"
    step "I go to login page"
end

When /^I set username with a right username$/ do
  puts "set a right username"
end

When /^I set password with a (.*) password/ do |password|
  puts "set a #{password} password"
end

When /^I click the login button$/ do
  puts "click login button"
end

Then /^I see login success$/ do
  puts "login success"
end

Given /^I go to login page$/ do
  puts "go to login page"
end

Then /^I see login fail$/ do
  puts "login fail"
end

register.rb:

Given /^I open the Register page$/ do
  steps %Q{
    Given I launch the browser
    And I go to Register page
  }
end

Given /^I go to Register page$/ do
  puts "go to Register page"
end

When /^I fill textboxs on the Register page$/ do |table|
  table.hashes.each do |item|
    puts "set #{item["id"]} to #{item["value"]}"
  end
end

When /^I click the register button$/ do
  puts "click register button"
end

Then /^I see register success$/ do
  puts "register success"
end

在上面我基本上用到了上一节所讲述的所有的知识,知识在每个feature的Scenario中,添加了一个以@开头的tag,这个只是给了一个Scenario一个标记,在命令中会用到。

在整个工程含有了2个feature文件,每个文件都含有对应的step实现,而且他们共有的步骤也进行了一个提取,以至于更方便的去管理它。现在看看一些关于如何按照自己的需求去执行这些用例的命令行。

(1)执行一个文件下面所有的用例

cucumber features/login.feature

执行结果如下:

通过上图可以看出,上面的命令执行了login.feature下面所有的Scenario。

(2)执行所有的用例

cucumber features/.

执行结果如图:

通过上图可以看出,3个用例都被执行了。

(3)执行一个特定的用例

cucumber -t @LoginWithRightUsernameAndErrorPassword   
或者
cucumber --tag @LoginWithRightUsernameAndErrorPassword

结果如下:

通过-t参数可以发现我们执行了一个单独的用例。

(4)执行用例的某一行所在的用例

cucumber -l 8 features/login.feature
或者
cucumber --line 8 features/login.feature
或者
cucumber  features/login.feature:8

执行结果如下:

第8行所在的用例是LoginWithRightUsernameAndRightPassword这一个,所以结果和我们预期的一样,这个用例被执行了

cucumber中执行的命令大致会用到这一些,其他的一些执行命令你可以通过cucumber --help去进行了解

关于日志的生成

 在测试中,一般用例执行完了后需要测试报告,cucumber自身也提供了这样的功能。在cucumber中测试报告提供了好几种形式,比如json,html,junit等等,看看这几种测试报告生成的结果是什么样的

(1) Json

命令如下: 

cucumber features/login.feature -f json -o login.out.json

前面的一段命令和之前的运行命令是一样的,后面 -f 参数代表了格式(format)是json, -o参数代表了生成输出文件为 login.out.json

看看生成的json文件是如何的:

这里的json文件太长并没有完整的截取,查看json文件的时候感觉对于报告这种东西来说并不太清晰,但是json十分的好用来分析,如果想生成比较清晰的结果最好是生成html,看看html是如何的被生成的:

(2)html

命令如下:

cucumber features/login.feature -f html -o login.out.html

通过命令发现只用把json换成html就行了,看看生成的html是如何的:

绿色就代表这个用例是通过的,比起刚刚的json来说,这个就清晰明了多了。

(3)junit

junit提供了xml格式的报告,和刚刚一样只需要改下参数就行:

cucumber features/login.feature -f junit -o login.out

运行了命令后,在HelloWorld文件下面重新生成了一个login.out的文件夹,里面包含了一个xml文件,是这次运行的结果,它能直接的使用到junit+ant这种架构下面去。

其他的格式在这里就不讲解了,需要大家去探索下。

posted on 2016-03-16 18:51  饭小  阅读(2029)  评论(0编辑  收藏  举报