[PHP]用PHPUnit进行测试驱动开发(Test-Driven Development)
单元测试是软件开发过程中极其重要的一部分,几个著名的软件开发实践都推崇以单元测试为主导:Test-First Programming, Extreme Programming(XP极限编程), Test-Driven Development(TDD,敏捷方法的核心实践).
它们也允许那些即使在语言结构上并不支持这种方法论的编程语言使用Design-by-Contract(契约式设计)。
你可以在你开发的过程中用PHPUnit写test。然而,在新错误发生时越快写好test越有价值,所以在代码完成后几个月才去写test,不如在一个defect发生后
马上写。既然如此,为什么不干脆在defect可能引入前写呢?(Test-First Programming)
Test-First Programming是XP和TDD的一部分,它就是基于这种思路并且将其发挥至极致。依靠今日计算机的强大性能,我们可以每天RUN成千上万个test。
我们可以从这些测试中获取的反馈来保证代码的每一小步的质量。这些test就像pitons(岩钉),确保不管发生了什么,你只能落到上一步结束的位置。
当你先写test,它可能无法运行,因为它要调用的对象和方法还没有coding。一开始可能觉得奇怪,但你马上就能适应它。如果你遵循面向对象原则使用
接口编程,Test-First Programming是最实用的实践。当你写test的时候,你在思考你所要测试对象的接口,这是从外部去观察这个对象。当你要让这个test
真正跑起来,你在进行纯抽象的思考。这样,接口的错误就可以被失败的test修复。
The point of Test-Driven Development is to drive out the functionality the software actually needs, rather than what the programmer thinks it probably ought to have. The way it does this seems at first counterintuitive, if not downright silly, but it not only makes sense, it also quickly becomes a natural and elegant way to develop software.
--Dan North
TDD的要点是drive out软件确切需要的功能,而不是程序员自己认为应该实现的功能。这初看起来是违反直觉的,其实它不光是合理的,而且快速成为软件开发一种自然而优雅的方式。
--Dan North
接下来用一个例子简要介绍如何进行TDD。要想更详细的了解,推荐几本书:
Test-Driven Development by Kent Beck;
A Practical Guide to Test-Driven Development by Dave Astels;
银行账户例子
BankAccount类需要deposit(存款)和withdraw(取现)方法。而且它要遵守下面2个契约条件:
1、银行账户初始值必须为0.
2、银行账户值不能为负。
在编码实现这个类之前我们先写test,我们用契约条件作为设计test的准则
代码
1<?php
2require_once 'PHPUnit/Framework.php';
3require_once 'BankAccount.php';
4
5class BankAccountTest extends PHPUnit_Framework_TestCase
6{
7 protected $ba;
8
9 protected function setUp()
10 {
11 $this->ba = new BankAccount;
12 }
13
14 public function testBalanceIsInitiallyZero()
15 {
16 $this->assertEquals(0, $this->ba->getBalance());
17 }
18
19 public function testBalanceCannotBecomeNegative()
20 {
21 try {
22 $this->ba->withdrawMoney(1);
23 }
24
25 catch (BankAccountException $e) {
26 $this->assertEquals(0, $this->ba->getBalance());
27
28 return;
29 }
30
31 $this->fail();
32 }
33
34 public function testBalanceCannotBecomeNegative2()
35 {
36 try {
37 $this->ba->depositMoney(-1);
38 }
39
40 catch (BankAccountException $e) {
41 $this->assertEquals(0, $this->ba->getBalance());
42
43 return;
44 }
45
46 $this->fail();
47 }
48}
49?>
接下来我们开始写BankAccount类,我们为了让第一个test,testBalanceIsInitiallyZero()能够成功跑起来,我们写最少的代码。
代码
1<?php
2class BankAccount
3{
4 protected $balance = 0;
5
6 public function getBalance()
7 {
8 return $this->balance;
9 }
10}
11?>
phpunit BankAccountTest
PHPUnit 3.4.2 by Sebastian Bergmann.
.
Fatal error: Call to undefined method BankAccount::withdrawMoney()
这样第一个test就能通过了,第二个现在还无法pass,接下来继续完善BankAccount类
代码
1<?php
2class BankAccount
3{
4 protected $balance = 0;
5
6 public function getBalance()
7 {
8 return $this->balance;
9 }
10
11 protected function setBalance($balance)
12 {
13 if ($balance >= 0) {
14 $this->balance = $balance;
15 } else {
16 throw new BankAccountException;
17 }
18 }
19
20 public function depositMoney($balance)
21 {
22 $this->setBalance($this->getBalance() + $balance);
23
24 return $this->getBalance();
25 }
26
27 public function withdrawMoney($balance)
28 {
29 $this->setBalance($this->getBalance() - $balance);
30
31 return $this->getBalance();
32 }
33}
34?>
phpunit BankAccountTest
PHPUnit 3.4.2 by Sebastian Bergmann.
...
Time: 0 seconds
OK (3 tests, 3 assertions)
除了上面这样写,你也可以使用PHPUnit_Framework_Assert类提供的静态assertion方法来把条件按契约式风格写进你的代码,
如果其中一个失败,一个PHPUnit_Framework_AssertionFailedError异常会抛出。这种方式你为条件检查所写的代码更少,而且测试代码
可读性也更好。但是,你使得PHPUnit的环境依赖加入了你的项目。
Design-by-Contract assertions
代码
1<?php
2require_once 'PHPUnit/Framework.php';
3
4class BankAccount
5{
6 private $balance = 0;
7
8 public function getBalance()
9 {
10 return $this->balance;
11 }
12
13 protected function setBalance($balance)
14 {
15 PHPUnit_Framework_Assert::assertTrue($balance >= 0);
16
17 $this->balance = $balance;
18 }
19
20 public function depositMoney($amount)
21 {
22 PHPUnit_Framework_Assert::assertTrue($amount >= 0);
23
24 $this->setBalance($this->getBalance() + $amount);
25
26 return $this->getBalance();
27 }
28
29 public function withdrawMoney($amount)
30 {
31 PHPUnit_Framework_Assert::assertTrue($amount >= 0);
32 PHPUnit_Framework_Assert::assertTrue($this->balance >= $amount);
33
34 $this->setBalance($this->getBalance() - $amount);
35
36 return $this->getBalance();
37 }
38}
39?>
这样我们使用了契约式设计来开发BankAccount类。写的时候注意遵循Test-First Programming,代码必须让测试通过。然而我们忘记
去写用来测试setBalance()、depositMoney()、withdrawMoney()符合契约条件时的test了。我们需要一种机制来测试我们的test或者至少
来衡量它们的质量。这就是接下来将讨论的代码覆盖率分析(analysis of code-coverage information).
页:
[1]