Cucumber---行為驅(qū)動(dòng)開發(fā)的利器(一)

          Cucumber 是一個(gè)BDD的測試框架,它從業(yè)務(wù)角度出發(fā),使用業(yè)務(wù)相關(guān)的語言(非技術(shù)語言),描述系統(tǒng)具有的行為。它更關(guān)注的是業(yè)務(wù)層面而非代碼層面的邏輯。
          在這篇文章里,我將首先介紹BDD, 然后介紹Cucumber以及相關(guān)概念。

          一 BDD
             Behavior-Driven Development is about implementing an application by describing its behavior from the perspective of its stakeholders.
          這里需要注意兩點(diǎn):
          1)stakeholders:軟件的使用者,受益者,通俗的說就是軟件的客戶。
          2)behavior:這里的behavior,指的是從stakeholders的角度看到的軟件系統(tǒng)的行為。

          BDD的作用:
          其實(shí)BDD和agile是緊密相關(guān)的。在agile中,我們使用BDD測試作為acceptance test。也就是說,當(dāng)分析出一個(gè)user story后,我們應(yīng)該有一個(gè)BDD的case與之對應(yīng),作為衡量軟件是否滿足該需求的一個(gè)標(biāo)準(zhǔn)。其實(shí),從某種程度上講,BDD就是TDD的發(fā)展,BDD出現(xiàn)的背景,就是開發(fā)人員在TDD時(shí),不清楚到底該將一個(gè)什么樣的story作為自己TDD開發(fā)需要實(shí)現(xiàn)的目標(biāo)。
          而BDD簡單的回答了這個(gè)問題:讓stakeholders用一個(gè)test case告訴你,你的具體目標(biāo)是什么。同時(shí)強(qiáng)調(diào)了,每一個(gè)開發(fā)目標(biāo)都應(yīng)該是系統(tǒng)的behavior的體現(xiàn)。

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

          Enough is enough:  
          Up-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.
          這一點(diǎn)秉承的原則:不做多余的事。
          Deliver stakeholder value:
          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.
          這一點(diǎn)秉承的原則:做對客戶有價(jià)值的事。

          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.
          這一點(diǎn)秉承的原則:實(shí)現(xiàn)的都是有價(jià)值的行為。

          二 Cucumber
             Cucumber 是一個(gè)基于BDD的測試框架,它能夠幫助團(tuán)隊(duì)使用業(yè)務(wù)上的語言對軟件系統(tǒng)的行為進(jìn)行測試。同單元測試相比,Cucumber的測試更多關(guān)注的是業(yè)務(wù)級(jí)別的行為,因此非開發(fā)人員、業(yè)務(wù)專家或者客戶能夠容易的參與,從而保證軟件系統(tǒng)的行為切實(shí)符合業(yè)務(wù)需求。同時(shí),通過Cucumber的測試,開發(fā)人員也能更正確、清晰的理解業(yè)務(wù)需求,保證開發(fā)出來的系統(tǒng)滿足客戶的期望。由于Cucumber采用幾乎近于自然語言的方式描述了軟件的行為過程,因此可以直接作為軟件的需求文檔,也可以直接作為測試或者系統(tǒng)驗(yàn)收的標(biāo)準(zhǔn)文檔。  這種方式也淋漓盡致的體現(xiàn)了敏捷的思想:代碼即是最有效的文檔。

          三 Cucumber的概念 
          Cucumber中有幾個(gè)重要的概念:
          1. Feature
              Feature指的是軟件系統(tǒng)所提供的某個(gè)功能,一般對應(yīng) User Story。在一個(gè)Cucumber 的feature文件中,主要定義了用戶如何使用軟件提供的該功能。通常我們這樣定義feature: 
          Feature: <description> 
             As a <role>
             I want <feature>
             So that <business value>
          即作為某個(gè)角色,我想使用系統(tǒng)做些事情,從而得到期望的實(shí)現(xiàn)業(yè)務(wù)價(jià)值的結(jié)果。 
          在feature的定義中,通常會(huì)涉及如下幾個(gè)問題:
             a)  Who is using the system?
             b)  What are they doing?
             c)  Why do they care? 

          比如說:  
          Feature: Adding the numbers by calculator
             As a customer,
             I want to calculate the two numbers,
            so that I can get the right results. 

          通常一個(gè)feature文件中會(huì)包括一個(gè)或者多個(gè)Scenario。


          2. Scenario
             Scenario描述了用戶使用軟件的場景,即在某種前提條件下,當(dāng)用戶使用軟件觸發(fā)某個(gè)行為,那么應(yīng)該會(huì)得到一個(gè)預(yù)期的結(jié)果。 比如說:  

           Scenario: Add two numbers    
            Given the input "2+2"    
            When the calculator is run    
            Then the output should be "4"  

          即當(dāng)使用計(jì)算器求和時(shí),假如給定了2+2的前提條件,那么當(dāng)點(diǎn)擊計(jì)算時(shí),期望的結(jié)果應(yīng)該是4.

          通常Scenario由一組步驟(step)組成。


          3. Step  
             Step 描述了在完成一次同軟件交互的過程中,所需要的每個(gè)動(dòng)作。
            通常有三種step:
              1) 給定的條件(Given)
              2)觸發(fā)的行為(When)
              3)期待的結(jié)果(Then)
           比如上個(gè)例子中定義當(dāng)完成一次計(jì)算兩數(shù)相加的行為時(shí),需要3個(gè)步驟

          Given the input "2+2"             //步驟1,確定前提條件,如我希望計(jì)算的數(shù)字是2+2;    
          When the calculator is run       //步驟2,運(yùn)行計(jì)算器,并開始計(jì)算;    
          Then the output should be "4"  //步驟3, 驗(yàn)證期待的結(jié)果是4. 


          4. Step definition(implementations) 
             Step是使用業(yè)務(wù)層面的描述語言完成的,比如剛才提到的測試計(jì)算器的例子。 針對這種業(yè)務(wù)層面的描述語言,必然有對應(yīng)的技術(shù)層面的實(shí)現(xiàn),才能將這些業(yè)務(wù)描述語言轉(zhuǎn)變成對系統(tǒng)進(jìn)行測試的代碼。 我們稱這種對業(yè)務(wù)語言的技術(shù)實(shí)現(xiàn)為step definition.   比如對上個(gè)例子的step,我們使用的實(shí)現(xiàn)偽代碼如下所示 :
          Given /^the input([^"]*)"$/ do |input_number|  
            //get the input expression by the regexp match   
            input = input_number
          end 

          When /^the calculator is run$/ do  
            //run the calculator and trigger it to do calculation by input    
            result = calculator(input)
          end

          Then /^the output should be ([^"]*)"$/ do |expected_result|  
            //get the expected result by the regexp match  
            
          //verify whether the result is as we expected 
             result == expected_result
          end


          四 總結(jié)
            本文介紹了BDD、Cucumber的背景, 以及Cucumber中涉及的概念(Feature, Scenario, Step, Step definition),后續(xù)將介紹如何使用Cucumber來幫助我們測試軟件系統(tǒng)的行為。 

          posted on 2012-05-19 14:24 想飛就飛 閱讀(4540) 評(píng)論(1)  編輯  收藏 所屬分類: Cucumber

          評(píng)論

          # re: Cucumber---行為驅(qū)動(dòng)開發(fā)BDD的利器(一) 2012-05-21 23:09 Unmi

          這東西還是不錯(cuò)的,我們用它來測試 iOS 程序  回復(fù)  更多評(píng)論   


          只有注冊用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          公告


          導(dǎo)航

          <2012年5月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          統(tǒng)計(jì)

          常用鏈接

          留言簿(13)

          我參與的團(tuán)隊(duì)

          隨筆分類(69)

          隨筆檔案(68)

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 徐水县| 尚义县| 揭西县| 安西县| 德江县| 陈巴尔虎旗| 黄骅市| 江山市| 涡阳县| 湖北省| 平阳县| 孝义市| 尼玛县| 长垣县| 阿拉善盟| 弥渡县| 长兴县| 大足县| 三河市| 含山县| 马山县| 阿勒泰市| 大庆市| 元阳县| 崇信县| 吴江市| 固镇县| 庆云县| 丘北县| 广安市| 广南县| 云阳县| 洛阳市| 东海县| 和平区| 米易县| 虹口区| 基隆市| 南溪县| 建宁县| 陵川县|