Friday, 27 February 2015

Cucumber testing stack




Software teams work best when the developers and business stakeholders are communicating clearly with one another. A great way to do that is to collaboratively specify the work that’s about to be done using automated acceptance tests.

When the acceptance tests are written as examples, they stimulate people’s imaginations and help them see other scenarios they hadn’t previously considered.

When the team write their acceptance tests collaboratively, they can develop their own ubiquitous language for talking about their problem domain. This helps them avoid misunderstandings.


Cucumber was designed specifically to help business stakeholders get involved in writing acceptance tests.
Each test case in Cucumber is called a scenario, and scenarios are grouped into features. Each scenario contains several steps.

The business-facing parts of a Cucumber test suite, stored in feature files, must be written according to syntax rules—known as Gherkin—so that Cucumber can read them.

Under the hood, step definitions translate from the business-facing language of steps into Ruby code.

Thursday, 26 February 2015

Cucumber ruby - how to set load path

config
features\support\env.rb
lib

write this in the env.rb
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '../../', 'lib'))

Monday, 23 February 2015

cucumber - how to use ruby class defined

1>defined new ruby class under lib say calculator.rb
2>in rubymine : add lib to the load path
3>in definition ruby: add require 'calculator'

First_case
|--Features
   |--Step_definitions
   |  |--addition.rb
   |  |--adding.rb
   |--adding.feature
|--lib
   |--calcualtor.rb


Feature file - adding.feature
Feature: Addition
  In order to avoid silly mistakes
  As a math idiot
  I want to be told the sum of two numbers0

  Scenario: Add two numbers
    Given I have a calculator
    And I have entered 50 into the calculator
    And I have entered 70 into the calculator
    When I press add
    Then the result should be 120 on the screen
    
  

definition rb file - addition.rb
require 'calculator'
Given(/^I have a calculator$/) do  @c = Calculator.new
end
And(/^I have entered (\d+) into the calculator$/) do |arg|  @c.push(arg.to_i)
end
When(/^I press add$/) do  @c.sum
end
Then(/^the result should be (\d+) on the screen$/) do |arg|  expect(@c.sum).to eq(arg.to_i)
end

calculator.rb
class Calculator  def push(n)    #记数    @args ||= [] #初始化空数组    @args << n  end  def sum()      #返回所有数字和    sum = 0    @args.each do |i|      sum += i    end    @result = sum  end  def result    @result  endend


run it :

C:\dev\code\ruby\rubyCucumberTest>cucumber
Feature: Addition
  In order to avoid silly mistakes
  As a math idiot
  I want to be told the sum of two numbers0

  Scenario: Add two numbers                     # features\hello_cucumber.featur
e:6
    Given I have a calculator                   # features/step_definitions/addi
tion.rb:3
    And I have entered 50 into the calculator   # features/step_definitions/addi
tion.rb:7
    And I have entered 70 into the calculator   # features/step_definitions/addi
tion.rb:7
    When I press add                            # features/step_definitions/addi
tion.rb:11
    Then the result should be 120 on the screen # features/step_definitions/addi
tion.rb:15

1 scenario (1 passed)
5 steps (5 passed)
0m0.049s

cucumber - hello world 3

根据网络上的各位大神给出来的各种例子,看起来各种简单,但是对于小白级的我来说,也废了很多力气,遇到了一些小白无法解决的问题,特此分享到这里,希望其他小白看到了会得到帮助。

一.Cucumber的第一个case:两数相加

1.没错,我们首先要建立文件夹,为了让自己的case跑起来的时候不会遇到其他的文件路径的问题,最好是按照最原始的建立比较有规定的文件夹树,特描述如下:
First_case
|--Features
   |--Step_definitions
   |  |--add.rb
   |  |--adding.rb
   |--adding.feature
按照上述来建立好文件夹。

2.对应的各类文件编写步骤以及代码
add.rb:我这里是描述一个类,描述调用的这个方法,代码如下:
class Add
  def adding str1, str2
    str1+str2
  end
end

adding.feature:是对于要测试的case的描述,这里要测试的是简单的字符串相加。步骤如下:
Feature: To test two string add result

 Scenario: Inorder to test two string adding function
 Given input the first string  "Hello,summer."
 And  input another string "I'm nick!"
 When I press add button
 Then the screen will put "Hello,summer.I'm nick!"

adding.rb:这个就是来实现你所要执行的case所写的代码了:
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))#一般进行调试的时候,总是会有加载不到文件或者找不到方法的错误,加上这个命令,就可以减少这个错误了,我当时就是找这个错误找了足足两天,后来高手给我指点了,才知道!同学们要谨慎
require 'cucumber'#下面的Given,and...这些,都是包含在cucumber里面的,所以需要包含进来。
require 'add.rb' #简单的调用,所以要包含进来
require 'rspec' #最后一步所用的.should,需要包含这个文件夹

Given(/^input the first string  "(.*?)"$/) do |str1|
 @str1=str1
                                   end
And(/^input another string "(.*?)"$/) do |str2|
@str2=str2
 end
When(/^I press add button$/) do
@adder=Add.new
@result=@adder.adding(@str1,@str2)
end
Then(/^the screen will put "(.*?)"$/) do |str3|
  @result.should==str3
 end

3.万事具备,只欠东风了!点击运行adding.feautrue,接着就会报错,没有给Given定义,但是其实我们已经调用了cucumber文件夹,这里的错误就是在于,我们的路径有错!我们找到调试框去选择‘Edit configrations', 把Working directory的路径改为你目前所要测试case的顶层,比如说我们这里的顶层路径就是
First_case。

4.最后你的case就会一片大绿, 就会过了,哈哈哈。如果还遇到别的问题,可以在下面留言,我会每天晚上回来看到,就会回复哟~

祝大家能够天天向上!


也许还是有点乱,这里把完整的代码附上:

add.rb 中的代码:
class Add
  def adding str1, str2
    str1+str2
  end
end

adding.feature 中的步骤:
Feature: To test two string add result

 Scenario: Inorder to test two string adding function
 Given input the first string  "Hello,summer."
 And  input another string "I'm nick!"
 When I press add button
 Then the screen will put "Hello,summer.I'm nick!"

adding.rb中的代码:
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
require 'cucumber'。
require 'add.rb'
require 'rspec' 

Given(/^input the first string  "(.*?)"$/) do |str1|
 @str1=str1
                                   end
And(/^input another string "(.*?)"$/) do |str2|
@str2=str2
 end
When(/^I press add button$/) do
@adder=Add.new
@result=@adder.adding(@str1,@str2)
end
Then(/^the screen will put "(.*?)"$/) do |str3|
  @result.should==str3
 end

如此一来,再小白应该也是可以搞定的了。

cucumber - hello word 2

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

2、Cucumber的特点 
(1)、易于安装,使用。无论是在Ruby平台,.Net平台,Java平台安装都很容易。 
(2)、集成度高,对开发环境没有破坏,并和多种Web开发框架集成。 
(3)、易于理解,Cucumber的特性文档都是采用近似于普通文本的语法写成,很容易读懂和编写,甚至是非技术人员都可以编写。 
(4)、易于维护,因Cucumber是从用户的业务逻辑出发去写测试,不涉及到具体的技术框架,和内部业务流程,因为业务逻辑的变动频率相教于技术选择,内部处理流程稳定得多,测试被破坏的概率就低很多。 

3、Cucumber First Demo 
(1)、安装Ruby,之后通过命令gem install cucumber安装Cucumber,因为Cucumber还使用了Rspec作Assertion,因此我们还需要安装Rspec,命令”gem install rspec“。(在过程中,若出现ERROR:  Error installing cucumber:cucumber requires gherkin (~> 2.2.9,runtime)的错误,是因为Cucumber依赖的一个gem,gherkin有老版本。可通过gem install gherkin --version,指定安装新版本解决。或者gem uninstall gherkin,gem install gherkin重新安装)。 

(2)、创建一个Ruby project,然后在其项目目录下创建一个features的目录,在features目录下创建basic.feature的文件,其中的内容如下: 
Feature: Hello World Feature 
  In order to ensure that my installation works 
  As a Developer 
  I want to run a quick Cucumber test 

  Scenario: Hello World Scenario 
    Given The Action is Hello 
    When The Subject is World 
    Then The Greeting is Hello, World 
(3)、在features目录下创建step_definitions目录,在step_definitions目录下创建step_steps.rb文件,内容如下: 
require 'rspec/expectations' 

Given /The Action is ([A-z]*)/ do |action| 
  @action = action 
end 

When /The Subject is ([A-z]*)/ do |subject| 
  @subject = subject 
end 

Then /The Greeting is (.*)/ do |greeting| 
  greeting.should == "#{@action}, #{@subject}" 
end 

(4)、在项目根目录下执行cucumber,可以得到如下结果: 

4、Cucumber的术语 
(1)、Feature File:该文件通过一些具有代表性的例子来描述一个用户需求, 
(2)、Key Examples:关键用例,特性之间都可以通过自己的关键用例加以区分,每个关键用例都有明确的输入和输出。 
(3)、Scenario:测试场景,一个用户特性的一个关键用例就称之为一个测试场景。 
(4)、Step:测试步骤,一个测试场景涉及到多个步骤操作, 
(5)、Step_Definitions:步骤定义,用来定义测试用例中步骤的执行顺序。 
(6)、Gherkin:用来定义Feature File的结构和关键字含义的语言,可以通过这个语言在未支持cucumber的平台上实现一个cucumber。

Total visitors since Jan 2012

World Visitor Map