最近在学习Cucumber,发现网上具体怎么使用Cucumber的实例太少,或者是不详细,或者是Demo有错误。
#B` a^j
^0 整理一下文档,仅供大家参考:
)?6VUi] f)D9rG0 前提条件:
-D5rW#SD$c
Ru_0 Cucumber环境搭建,请参考:http://www.cnblogs.com/puresoul/archive/2011/11/18/2254407.html
COw:v Com0
1~V~
v)B8~0 环境搭建OK后,请在DOC环境运行如下命令:51Testing软件测试网g%X I;ukaA
cucumber
–init51Testing软件测试网NI]Z9aI
51Testing软件测试网z%C ]4r;Wb
t}.{
创建cucumber的基础结构51Testing软件测试网+p,f/m*t!h/{:D.ZM
y
即如下文件夹目录:51Testing软件测试网1t\O,e7Q*l[}QK6G
Test
P3D2L3[T(D0J:]b0 |--Features
2Q9m,U+c a!o!DC0 |--Step_definitions
Bu i/? U7U5[!a6amF0 | |--add.rb51Testing软件测试网/C&b0K9wG{DL"[n
| |--adding.rb51Testing软件测试网&H%Wx&`u4?I
|--adding.feature
"BP:o[;pTySj051Testing软件测试网Wp
Yay7T
+b6_0mC9@7q)n:d0add.rb和adding.rb文件位于step_definitions文件夹中
[7qMZ2f}0请在add.rb文件中输入以下内容:51Testing软件测试网({4x+Xi n*p h
class Add
oxS$tt1dB0 def adding str1, str251Testing软件测试网$X
d)rp1Z6LVk
H:{
str1+str251Testing软件测试网[;_2gxM+w
end
%f(f8{|w3If0end51Testing软件测试网-AQ*T+j8o5q.Q8O.UN
请在adding.rb文件中输入以下内容:
._jzT5j*e0$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
5D.iv ZRe-C&O&QH0require 'cucumber'。
:N+s5aZt
R5v#p0require 'add.rb'
Of5_;H'Y.e)\]5Z3F0require 'rspec' 51Testing软件测试网$_(C1b*L;o
51Testing软件测试网!s_c/i,C$o
Given(/^input the first string "(.*?)"$/) do |str1|
vq*y${Gx B(~0 @str1=str1
O5IaR1VcM0X0
end
+gx*AF'^f(ib2p0And(/^input another string "(.*?)"$/) do |str2|
6R3c JJ8U+R9t%E0@str2=str251Testing软件测试网|bYcWg!bq.T*iU/f
end51Testing软件测试网+JVqIo/LAr(x
When(/^I press add button$/) do
^l3_I D9`?6x*`0@adder=Add.new
.?{n*UGw;O4J0@result=@adder.adding(@str1,@str2)
m\Ae2b
A
[|-y*qe0end
D9}DG,s"OM]a
{0Then(/^the screen will put "(.*?)"$/) do |str3|51Testing软件测试网2{Yjxx4?#K!\
@result==str3
+Q3_&Qk%r)],T(v|i|0 end51Testing软件测试网Lg(~
^'NM
k
Adding.feature位于features文件夹下
SE9B]2vEA)C
[/S'u
b0
-@!sHTQ|0
;p&h'KSE#H @P1c0请在adding.feature文件输入如下内容:51Testing软件测试网9fQicZ_
s6J
Feature: To test two string add result51Testing软件测试网9P&r%E8jb4B8m!n
1N9khA!f0 Scenario: Inorder to test two string adding function
-{H8}p_ `j0 Given input the first string "Hello "51Testing软件测试网,W5r:A;I(@6YlQ
And input another string "World"
n;wt9?f8cY/Z0 When I press add button
c!Q+w2X4Z0p9oN8L0 Then the screen will put "Hello World"51Testing软件测试网JUx:\D5]*DI
51Testing软件测试网R!?
c x3C
请在adding.feature的上上级目录运行cucumber命令51Testing软件测试网~SnPc
D:\Test>cucumber features/adding.feature51Testing软件测试网x,tS~#Z7m|FH.B
绿色部分表示场景已经运行成功。
-R7Ffc1})l6W0