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/nzi7i.I
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.
二 Cucumber51Testing软件测试网&^ I,?$BT)Yxbo
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:^"uZ
T|TG"Pr0在feature的定义中,通常会涉及如下几个问题:51Testing软件测试网$q7a;K~ z;MRuT
a) Who is using the system?51Testing软件测试网 D,O:Lih'Gp6}Ogk1^
b) What are they doing?51Testing软件测试网#?OL8p0M1X4uy
c) Why do they care?
rm5i7F&T9Z051Testing软件测试网w]1Mk)Nq3c@
比如说: 51Testing软件测试网^'@5Q i,k\S_V Y
通常一个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
"t7|/^LPQE$f0即当使用计算器求和时,假如给定了2+2的前提条件,那么当点击计算时,期望的结果应该是4.
$U"Pf ]"PZ.M/r9P051Testing软件测试网Z'd;Y uW$jp1Z2K
通常Scenario由一组步骤(step)组成。
"ws,e1B1J v0
z-A9Tj sT0
3. Step
eN k0y U*k W 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&v
在这篇文章里,我将首先介绍BDD, 然后介绍Cucumber以及相关概念。
bx7t;s ^'a0
)z;uQy$e)`#C7t/}0一 BDD51Testing软件测试网5W/nzi7i.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
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.
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.
这一点秉承的原则:做对客户有价值的事。
aP3q-sX4wlov051Testing软件测试网-_rFuP3yh V
It’s all behavior:
aP3q-sX4wlov051Testing软件测试网-_rFuP3yh V
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)Yxbo
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:^"uZ
Feature: <description> 51Testing软件测试网a&@_ \:j3a5a3s9q
As a <role>51Testing软件测试网XC7D mnv \+A
I want <feature>
Gv2PVg2?F1V0 So that <business value>
即作为某个角色,我想使用系统做些事情,从而得到期望的实现业务价值的结果。 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'Gp6}Ogk1^
b) What are they doing?51Testing软件测试网#?OL8p0M1X4uy
c) Why do they care?
rm5i7F&T9Z051Testing软件测试网w]1Mk)Nq3c@
比如说: 51Testing软件测试网^'@5Q i,k\S_V Y
Feature: Adding the numbers by calculator
urk"F-P]0 As a customer,
w&s3HXWl7x0 I want to calculate the two numbers,
rU+w)Ka Ot0 so that I can get the right results.
51Testing软件测试网N a#d]5Nc+v{urk"F-P]0 As a customer,
w&s3HXWl7x0 I want to calculate the two numbers,
rU+w)Ka Ot0 so that I can get the right results.
通常一个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+wU)T4a)Nb b&X0 Given the input "2+2"
(d D3K/qg0 When the calculator is run 51Testing软件测试网 VV;YLpWD Xx
Then the output should be "4"
w;Ot+wU)T4a)Nb b&X0 Given the input "2+2"
(d D3K/qg0 When the calculator is run 51Testing软件测试网 VV;YLpWD Xx
Then the output should be "4"
"t7|/^LPQE$f0即当使用计算器求和时,假如给定了2+2的前提条件,那么当点击计算时,期望的结果应该是4.
$U"Pf ]"PZ.M/r9P051Testing软件测试网Z'd;Y uW$jp1Z2K
通常Scenario由一组步骤(step)组成。
"ws,e1B1J v0
z-A9Tj sT0
3. Step
eN k0y U*k W 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&v