Cucumber---行为驱动开发的利器

上一篇 / 下一篇  2016-04-11 11:29:55 / 个人分类:BDD

Cucumber 是一个BDD的测试框架,它从业务角度出发,使用业务相关的语言(非技术语言),描述系统具有的行为。它更关注的是业务层面而非代码层面的逻辑。51Testing软件测试网p0`:Z7Y6l6RJ
在这篇文章里,我将首先介绍BDD, 然后介绍Cucumber以及相关概念。
bx7t;s ^'a0
)z;uQy$e)`#C7t/}0一 BDD51Testing软件测试网5W/n zi7i.I
   Behavior-Driven Development is about implementing an application by describing its behavior. from the perspective of its stakeholders.
这里需要注意两点:
1)stakeholders:软件的使用者,受益者,通俗的说就是软件的客户。
2)behavior:这里的behavior,指的是从stakeholders的角度看到的软件系统的行为。
aC5Dig7Ve-S2v0
BDD的作用:
其实BDD和agile是紧密相关的。在agile中,我们使用BDD测试作为acceptance test。也就是说,当分析出一个user story后,我们应该有一个BDD的case与之对应,作为衡量软件是否满足该需求的一个标准。其实,从某种程度上讲,BDD就是TDD的发展,BDD出现的背景,就是开发人员在TDD时,不清楚到底该将一个什么样的story作为自己TDD开发需要实现的目标。
而BDD简单的回答了这个问题:让stakeholders用一个test case告诉你,你的具体目标是什么。同时强调了,每一个开发目标都应该是系统的behavior的体现。

BDD的三原则:
We sum this up using the following three principles of BDD:

i)@a*d@,e\,L n#X0Enough is enough:  
1\)D7pL#M5e(uDh0Up-front planning, analysis, and design all have a diminishing return. We shouldn’t do less than we need to get started, but any more than that is wasted effort. This also applies to process automation. Have an automated build and deployment, but avoid trying to automate everything.
这一点秉承的原则:不做多余的事。
Deliver stakeholder value:51Testing软件测试网8`6S#in!]:j@~
If you are doing something that isn’t either delivering value or increasing your ability to deliver value, stop doing it, and do something else instead.
这一点秉承的原则:做对客户有价值的事。
a P3q-sX4wlov051Testing软件测试网-_rFuP3y hV
It’s all behavior:
 Whether at the code level, the application level, or beyond, we can use the same thinking and the same linguistic constructs to describe behavior. at any level of granularity.
这一点秉承的原则:实现的都是有价值的行为。

二 Cucumber51Testing软件测试网&^ I,?$BT)Y xbo
   Cucumber是一个基于BDD的测试框架,它能够帮助团队使用业务上的语言对软件系统的行为进行测试。同单元测试相比,Cucumber的测试更多关注的是业务级别的行为,因此非开发人员、业务专家或者客户能够容易的参与,从而保证软件系统的行为切实符合业务需求。同时,通过Cucumber的测试,开发人员也能更正确、清晰的理解业务需求,保证开发出来的系统满足客户的期望。由于Cucumber采用几乎近于自然语言的方式描述了软件的行为过程,因此可以直接作为软件的需求文档,也可以直接作为测试或者系统验收的标准文档。  这种方式也淋漓尽致的体现了敏捷的思想:代码即是最有效的文档。
-fVO0]CJ0
三 Cucumber的概念 
w1ML b p1YQB0Cucumber中有几个重要的概念:51Testing软件测试网hn0DJJ$?.knk
1. Feature
,MitT/Zg/~1Mg0    Feature指的是软件系统所提供的某个功能,一般对应 User Story。在一个Cucumber 的feature文件中,主要定义了用户如何使用软件提供的该功能。通常我们这样定义feature: 51Testing软件测试网OJ:^"u Z
Feature: <description> 51Testing软件测试网a&@ _ \:j3a5a3s9q
   As a <role>51Testing软件测试网XC7D mnv \+A
   I want <feature>
Gv2PVg2? F1V0   So that <business value>
即作为某个角色,我想使用系统做些事情,从而得到期望的实现业务价值的结果。 
T|TG"Pr0在feature的定义中,通常会涉及如下几个问题:51Testing软件测试网$q7a;K~ z;MRuT
   a)  Who is using the system?51Testing软件测试网 D,O:Lih'G p6}Og k1^
   b)  What are they doing?51Testing软件测试网#?OL8p0M1X4uy
   c)  Why do they care? 
rm5i7F&T9Z051Testing软件测试网w]1Mk)Nq3c@
比如说:  51Testing软件测试网^'@5Q i,k\S_VY
Feature: Adding the numbers by calculator
urk"F-P]0   As a customer,
w&s3H XWl7x0   I want to calculate the two numbers,
rU+w)KaOt0  so that I can get the right results. 
51Testing软件测试网N a#d]5Nc+v{
通常一个feature文件中会包括一个或者多个Scenario。
(eH ^X a!V[051Testing软件测试网lV Q]$p5Nv ]i

2. Scenario51Testing软件测试网1A\,Zz'b*_z8h.JZA(e
   Scenario描述了用户使用软件的场景,即在某种前提条件下,当用户使用软件触发某个行为,那么应该会得到一个预期的结果。 比如说:  
U oG6YHyG\0
)uF;]ZU/M g0
 Scenario: Add two numbers    
w;Ot+w U)T4a)Nb b&X0  Given the input "2+2"    
(d D3K/q g0  When the calculator is run    51Testing软件测试网VV;YLpWD Xx
  Then the output should be "4"  

"t7|/^ LP QE$f0即当使用计算器求和时,假如给定了2+2的前提条件,那么当点击计算时,期望的结果应该是4.
$U"Pf ]"PZ.M/r9P051Testing软件测试网Z'd;YuW$jp1Z2K
通常Scenario由一组步骤(step)组成。
"ws,e1B1J v0
z-A9TjsT0
3. Step  
eNk0y U*kW mZ0   Step 描述了在完成一次同软件交互的过程中,所需要的每个动作。51Testing软件测试网6@"Hj?n V
  通常有三种step:
p F#HT`5C f2t%j&V0    1) 给定的条件(Given)51Testing软件测试网 fS;YdI9t7h,lS
    2)触发的行为(When)51Testing软件测试网E l/p Zz&vi&v1q
    3)期待的结果(Then)
D@-gT:Q9H0 比如上个例子中定义当完成一次计算两数相加的行为时,需要3个步骤
!mZp7{-i0
};i A7t2UR'm `0
Given the input "2+2"             //步骤1,确定前提条件,如我希望计算的数字是2+2;    
^2h [+Y A {%P^8iau0
When the calculator is run       //步骤2,运行计算器,并开始计算;    51Testing软件测试网 Qh6Y5fw^Y_
Then the output should be "4"  //步骤3, 验证期待的结果是4. 51Testing软件测试网4ITu4]3xY:l
51Testing软件测试网gk)?}8b ~'M3z-@^

4. Step definition(implementations) 51Testing软件测试网 nv&l"T.uR
   Step是使用业务层面的描述语言完成的,比如刚才提到的测试计算器的例子。 针对这种业务层面的描述语言,必然有对应的技术层面的实现,才能将这些业务描述语言转变成对系统进行测试的代码。 我们称这种对业务语言的技术实现为step definition.   比如对上个例子的step,我们使用的实现伪代码如下所示 :51Testing软件测试网6u[#L;d3KA
Given /^the input([^"]*)"$/ do |input_number|  
"p6_MZ5C!u4e"]R/i+}\0  //get the input expression by the regexp match   
Vz(ly_*JW4z{0
  input = input_number51Testing软件测试网5OS3O U/Td:R
end 
w#P6peV!g [j b a0
fV&Y] @6G X4k+j0When /^the calculator is run$/ do  51Testing软件测试网/p%Rfk#Qd;]
  //run the calculator and trigger it to do calculation by input    
t7HI9f)N0
  result = calculator(input)51Testing软件测试网-q8O,n$\MI RP8v
end
@n*n4^J6F0
U3QNOZfn/^b]0Then /^the output should be ([^"]*)"$/ do |expected_result|  
wWQ [Be0n~$?d0  //get the expected result by the regexp match  51Testing软件测试网y j Q S9|4kP+Z j M
  
//verify whether the result is as we expected 
m(sx+m1y/R-Z_6k*g(h0
   result == expected_result
+on+K(\io8[:E9f0end

9dZ9r$y+U;@O0
四 总结
P!L H dBx z%WDe X0  本文介绍了BDD、Cucumber的背景, 以及Cucumber中涉及的概念(Feature, Scenario, Step, Step definition),后续将介绍如何使用Cucumber来帮助我们测试软件系统的行为。 

TAG: 开发

 

评分:0

我来说两句

Open Toolbar