Cucumber---行为驱动开发的利器
上一篇 / 下一篇 2016-04-11 11:29:55 / 个人分类:BDD
Cucumber 是一个BDD的测试框架,它从业务角度出发,使用业务相关的语言(非技术语言),描述系统具有的行为。它更关注的是业务层面而非代码层面的逻辑。
yE^T;?7D"iGl0在这篇文章里,我将首先介绍BDD, 然后介绍Cucumber以及相关概念。
cg0MW?*S%Sl$e051Testing软件测试网Z^s x I'W.V2wy
一 BDD
RbR\Em;}0
BDD的三原则:
二 Cucumber
ZH:y r$Mre0 Cucumber是一个基于BDD的测试框架,它能够帮助团队使用业务上的语言对软件系统的行为进行测试。同单元测试相比,Cucumber的测试更多关注的是业务级别的行为,因此非开发人员、业务专家或者客户能够容易的参与,从而保证软件系统的行为切实符合业务需求。同时,通过Cucumber的测试,开发人员也能更正确、清晰的理解业务需求,保证开发出来的系统满足客户的期望。由于Cucumber采用几乎近于自然语言的方式描述了软件的行为过程,因此可以直接作为软件的需求文档,也可以直接作为测试或者系统验收的标准文档。 这种方式也淋漓尽致的体现了敏捷的思想:代码即是最有效的文档。
]t+faT{0
三 Cucumber的概念
t/Em8l5H%q;[.V)x0Cucumber中有几个重要的概念:51Testing软件测试网%g)|b&q@
1. Feature
(k,tLcbZ` H;g}0 Feature指的是软件系统所提供的某个功能,一般对应 User Story。在一个Cucumber 的feature文件中,主要定义了用户如何使用软件提供的该功能。通常我们这样定义feature: 51Testing软件测试网{Fju7Krh
~v oSzGX0在feature的定义中,通常会涉及如下几个问题:51Testing软件测试网-M9m)@Z7SQl w
a) Who is using the system?
nlb8M;MU e0 b) What are they doing?
G2Pn$V C3w\?0 c) Why do they care? 51Testing软件测试网T9P4{K{ P(K,s/cWc
j:m;F,bR1cX0比如说: 51Testing软件测试网`o,rJly
#x#n wO&d~p9d0通常一个feature文件中会包括一个或者多个Scenario。
I5Z:~8J#t051Testing软件测试网D {r9gmj?V
2. Scenario51Testing软件测试网3iDW2H7Y:L!qb
Scenario描述了用户使用软件的场景,即在某种前提条件下,当用户使用软件触发某个行为,那么应该会得到一个预期的结果。 比如说: 51Testing软件测试网7N D9W }9k(J)E$dmI
NcBt4~+N0
即当使用计算器求和时,假如给定了2+2的前提条件,那么当点击计算时,期望的结果应该是4.
HI8x,Sg \%y0
}? }:ZTG%A%q0通常Scenario由一组步骤(step)组成。
1p;C%FGFLFN051Testing软件测试网ngo;P-K6\+hY
3. Step 51Testing软件测试网7C%P+x+E@
Step 描述了在完成一次同软件交互的过程中,所需要的每个动作。51Testing软件测试网9w-y{BQ%X'N NW
通常有三种step:51Testing软件测试网^,O8Dm Sy vx
1) 给定的条件(Given)
'i#RHiEw.Kk%~0 2)触发的行为(When)
yE^T;?7D"iGl0在这篇文章里,我将首先介绍BDD, 然后介绍Cucumber以及相关概念。
cg0MW?*S%Sl$e051Testing软件测试网Z^s x I'W.V2wy
一 BDD
RbR\Em;}0
Behavior-Driven Development is about implementing an application by describing its behavior. from the perspective of its stakeholders.
这里需要注意两点:
1)stakeholders:软件的使用者,受益者,通俗的说就是软件的客户。
2)behavior:这里的behavior,指的是从stakeholders的角度看到的软件系统的行为。51Testing软件测试网
|Kq$z
]ZK
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:
51Testing软件测试网,fwL de,^,r
Enough is enough:
/FZD)Xjd&{2b+J,Pu0Up-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.
Enough is enough:
/FZD)Xjd&{2b+J,Pu0Up-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:
9K&ge6M'\+r0If 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.
9K&ge6M'\+r0If 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.
这一点秉承的原则:做对客户有价值的事。
k.Iy-f Y(M0
h)j \*iN0It’s all behavior:
k.Iy-f Y(M0
h)j \*iN0It’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.
这一点秉承的原则:实现的都是有价值的行为。
二 Cucumber
ZH:y r$Mre0 Cucumber是一个基于BDD的测试框架,它能够帮助团队使用业务上的语言对软件系统的行为进行测试。同单元测试相比,Cucumber的测试更多关注的是业务级别的行为,因此非开发人员、业务专家或者客户能够容易的参与,从而保证软件系统的行为切实符合业务需求。同时,通过Cucumber的测试,开发人员也能更正确、清晰的理解业务需求,保证开发出来的系统满足客户的期望。由于Cucumber采用几乎近于自然语言的方式描述了软件的行为过程,因此可以直接作为软件的需求文档,也可以直接作为测试或者系统验收的标准文档。 这种方式也淋漓尽致的体现了敏捷的思想:代码即是最有效的文档。
]t+faT{0
三 Cucumber的概念
t/Em8l5H%q;[.V)x0Cucumber中有几个重要的概念:51Testing软件测试网%g)|b&q@
1. Feature
(k,tLcbZ` H;g}0 Feature指的是软件系统所提供的某个功能,一般对应 User Story。在一个Cucumber 的feature文件中,主要定义了用户如何使用软件提供的该功能。通常我们这样定义feature: 51Testing软件测试网{Fju7Krh
Feature: <description> 51Testing软件测试网 CT'IRx?3R
As a <role>51Testing软件测试网3[d1d3K(^|q
I want <feature>51Testing软件测试网Ovg }4};}
So that <business value>
即作为某个角色,我想使用系统做些事情,从而得到期望的实现业务价值的结果。 As a <role>51Testing软件测试网3[d1d3K(^|q
I want <feature>51Testing软件测试网Ovg }4};}
So that <business value>
~v oSzGX0在feature的定义中,通常会涉及如下几个问题:51Testing软件测试网-M9m)@Z7SQl w
a) Who is using the system?
nlb8M;MU e0 b) What are they doing?
G2Pn$V C3w\?0 c) Why do they care? 51Testing软件测试网T9P4{K{ P(K,s/cWc
j:m;F,bR1cX0比如说: 51Testing软件测试网`o,rJly
Feature: Adding the numbers by calculator51Testing软件测试网;e0t#Qy"f
t-r%x&j
As a customer,
a-y?7PPA@0 I want to calculate the two numbers,51Testing软件测试网8}m!E&GH d
so that I can get the right results.
As a customer,
a-y?7PPA@0 I want to calculate the two numbers,51Testing软件测试网8}m!E&GH d
so that I can get the right results.
#x#n wO&d~p9d0通常一个feature文件中会包括一个或者多个Scenario。
I5Z:~8J#t051Testing软件测试网D {r9gmj?V
2. Scenario51Testing软件测试网3iDW2H7Y:L!qb
Scenario描述了用户使用软件的场景,即在某种前提条件下,当用户使用软件触发某个行为,那么应该会得到一个预期的结果。 比如说: 51Testing软件测试网7N D9W }9k(J)E$dmI
NcBt4~+N0
Scenario: Add two numbers
uA:v*qs!y0 Given the input "2+2"
9vfV*CJm0 When the calculator is run 51Testing软件测试网_A8~-b y
Then the output should be "4"
51Testing软件测试网)h0|-k.eVzuA:v*qs!y0 Given the input "2+2"
9vfV*CJm0 When the calculator is run 51Testing软件测试网_A8~-b y
Then the output should be "4"
即当使用计算器求和时,假如给定了2+2的前提条件,那么当点击计算时,期望的结果应该是4.
HI8x,Sg \%y0
}? }:ZTG%A%q0通常Scenario由一组步骤(step)组成。
1p;C%FGFLFN051Testing软件测试网ngo;P-K6\+hY
3. Step 51Testing软件测试网7C%P+x+E@
Step 描述了在完成一次同软件交互的过程中,所需要的每个动作。51Testing软件测试网9w-y{BQ%X'N NW
通常有三种step:51Testing软件测试网^,O8Dm Sy vx
1) 给定的条件(Given)
'i#RHiEw.Kk%~0 2)触发的行为(When)