原文是写的一篇稿子,这里摘取了一部分(主要是代码)发表。


BDD(行为驱动开发)是很热门的话题。对于热门话题我都是有好奇心的 ^_^
仔细看了一下各种资料,发现 BDD 真是个好东西。

以前写测试,都是针对功能来写测试。而 BDD 是针对系统行为的来写测试,实际上就是用测试定义了系统的行为。这样一来,写测试的过程实际上就是“设计”。在设计系统各个子系统应该具有的功能,这些功能的行为等等。

所以 BDD 是一种完完全全的设计方法,而不是测试方法(其实 TDD 也可以算是设计方法,不过争议很大)。

好了,理论性的东西不多说了,先看看一个实际的“故事”和其测试代码。


故事“帐户持有人提取现金”及场景“帐户有足够的资金”:

Story: 帐户持有人提取现金

As an [帐户持有人]
I want [从 ATM 提取现金]
So that [可以在银行关门后取到钱]

Scenario 1: 帐户有足够的资金
Given [帐户余额为 $100]
And [有效的银行卡]
And [提款机有足够现金]
When [帐户持有人要求取款 $20]
Then [提款机应该分发 $20]
And [帐户余额应该为 $80]
And [应该退还银行卡]

看上去很容易理解吧 :)
下面是测试代码:

  1. <?php
  2.  
  3. require_once 'PHPUnit/Extensions/Story/TestCase.php';
  4.  
  5. /**
  6. * 测试从账户中取现
  7. */
  8. class AccountHolderWithdrawsCashSpec extends PHPUnit_Extensions_Story_TestCase
  9. {
  10.     /**
  11.      * @scenario
  12.      * 场景 1: 帐户有足够的资金
  13.      */
  14.     function AccountHasSufficientFunds()
  15.     {
  16.         $this->given('帐户余额为', 100)
  17.                  ->and('有效的银行卡')
  18.                  ->and('提款机有足够现金')
  19.              ->when('帐户持有人要求取款', 20)
  20.              ->then('提款机应该分发', 20)
  21.                  ->and('帐户余额应该为', 80)
  22.                  ->and('应该退还银行卡');
  23.     }
  24.  
  25.     // ...
  26.  
  27.     /**
  28.      * 处理 given
  29.      */
  30.     function runGiven(&$world, $action, $arguments)
  31.     {
  32.         switch($action)
  33.         {
  34.         case '帐户余额为':
  35.             // 由于 Account 对象必须属于一个 AccountHolder(帐户持有人),
  36.             // 因此需要构造一个 AccountHolder 对象
  37.             $account_holder = new AccountHolder();
  38.             $account_holder->name = 'tester';
  39.             // 创建一个 Account 对象,并设置余额为 $arguments[0]
  40.             $world['account'] = new Account($account_holder);
  41.             $world['account']->balance = $arguments[0];
  42.             break;
  43.  
  44.         case '有效的银行卡':
  45.             $card = new CreditCard($world['account']);
  46.             $card->valid = true;
  47.             $world['card'] = $card;
  48.             break;
  49.  
  50.         case '提款机有足够现金':
  51.             // 确保 ATM 的余额大于帐户余额
  52.             $world['atm'] = new ATM();
  53.             $world['atm']->balance = $world['account']->balance + 1;
  54.             break;
  55.  
  56.         default:
  57.             {
  58.                 return $this->notImplemented($action);
  59.             }
  60.         }
  61.     }
  62.  
  63.     /**
  64.      * 处理 when
  65.      */
  66.     function runWhen(&$world, $action, $arguments)
  67.     {
  68.         /**
  69.          * 在 when 中调用对象的业务方法
  70.          */
  71.         switch($action)
  72.         {
  73.         case '帐户持有人要求取款':
  74.             // 从指定提款机使用指定银行卡取出现金
  75.             $world['account']->drawingByATM($world['atm'], $world['card'], $arguments[0]);
  76.             break;
  77.  
  78.         default:
  79.             return $this->notImplemented($action);
  80.         }
  81.     }
  82.  
  83.     /**
  84.      * 处理 then
  85.      */
  86.     function runThen(&$world, $action, $arguments)
  87.     {
  88.         /**
  89.          * 在 then 中验证业务对象的状态是否符合标准
  90.          */
  91.         switch($action)
  92.         {
  93.         case '提款机应该分发':
  94.             // 验证提款机的余额
  95.             $this->assertEquals($arguments[0], $world['atm']->last_dispense, "提款机应该分发 {$arguments[0]}");
  96.             break;
  97.  
  98.         case '帐户余额应该为':
  99.             // 验证帐户余额
  100.             $this->assertEquals($arguments[0], $world['account']->balance, "帐户余额应该为 {$arguments[0]}");
  101.             break;
  102.  
  103.         case '应该退还银行卡':
  104.             // 验证银行卡没有被 ATM 锁定
  105.             $this->assertTrue($world['card']->isCheckedOut(), '应该退还银行卡');
  106.             break;
  107.  
  108.         default:
  109.             return $this->notImplemented($action);
  110.         }
  111.     }
  112. }

基本上测试代码就是从“故事”文本转换过来的。只不过 PHPUnit 对 BDD 的支持还不是很成熟,所以看上去有点别扭。

跑一下这个测试看看结果呢:

> phpunit –story AccountHolderWithdrawsCashSpec
PHPUnit 3.3.0beta1 by Sebastian Bergmann.

AccountHolderWithdrawsCashSpec
- Account has sufficient funds [failed]

Given 帐户余额为 100
and 有效的银行卡
and 提款机有足够现金
When 帐户持有人要求取款 20
Then 提款机应该分发 20
and 帐户余额应该为 80
and 应该退还银行卡

Scenarios: 1, Failed: 1, Skipped: 0, Incomplete: 0.

呵呵,测试失败哦。可惜 PHPUnit 3.3 beta 1 还没法显示具体是哪一个测试失败了,所以要去掉 –stroy 参数再运行一次测试:

> phpunit AccountHolderWithdrawsCashSpec

PHPUnit 3.3.0beta1 by Sebastian Bergmann.

F

Time: 0 seconds

There was 1 failure:

1) AccountHasSufficientFunds(AccountHolderWithdrawsCashSpec)
帐户余额应该为 120
Failed asserting that matches expected value .
D:\www\517sc\test\AccountHolderWithdrawsCashSpec.php:100

FAILURES!
Tests: 1, Assertions: 2, Failures: 1.

这样就很清楚了,原来是帐户余额测试没有通过。知道问题所在,解决起来就很简单了。