现在的位置: 首页 > 综合 > 正文

尝试行为驱动开发

2013年12月10日 ⁄ 综合 ⁄ 共 3389字 ⁄ 字号 评论关闭

原文是为《PHPer》电子杂志写的一篇稿子,这里摘取了一部分(主要是代码)发表。


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

基本上测试代码就是从“故事”文本转换过来的。只不过 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 <120>matches expected value <80>.
D:/www/517sc/test/AccountHolderWithdrawsCashSpec.php:100

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

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

抱歉!评论已关闭.