PHPUnit袖珍指南-第二章 自动测试

翻译 2006年05月20日 01:28:00

第二章 自动测试

最好的程序员也会犯错误。好程序员和差程序员的区别在于:好程序员能通过测试尽可能的发现错误。你越快测试错误,你就越快发现它们,发现和修正的成本就越低。这解释了为什么只在软件发布前才测试的做法为什么问题那么多。大多数错误根本就没有发现过,修正发现的错误是那么的高,以至于你不得不根据优先级来决定只修正那些错误,因为你根本就承受不起全部修正的费用。

相比你正在使用的方法,采用PHPUnit进行测试并不是一个全然不同的东西。它们只是方法不同。两者之间的不同在于,检查程序行为是否符合正确是通过一批可以自动测试的代码片断来进行的。这些代码片断叫做单元测试。

在这一部分,我们先基于打印的测试代码进行自动测试。假设我们要测试PHP的内建数组Array。需要测试之一是函数sizeof(),对任何新创建的数组,sizeof()函数应该返回 0。当我们加入一个新数组成员,sizeof()应该返回1。例1显示了我们想测试什么。

 

1. 测试数组和sizeof()

<?php

$fixture = Array( );

// $fixture

 

$fixture[] = "element";

// $fixture

?>

最简单的测试方法是在加入数组成员前后打印sizeof()的运算结果,如果返回01,说明Arraysizeof()运行正常。

 

2. 采用打印语句测试Arraysizeof()

<?php

$fixture = Array( );

print sizeof($fixture) . "/n";

 

$fixture[] = "element";

print sizeof($fixture) . "/n";

?>

0

1

 

现在,我们让测试程序从需要手工解释变为自动运行。在例3中,我们比较了期望值和实际值,如果相等就打印ok。如果我们发现有的结果不是ok,我们就知道有问题了。

3. 比较Arraysizeof()的期望值和实际值

<?php

$fixture = Array( );

print sizeof($fixture) == 0 ? "ok/n" : "not ok/n";

 

$fixture[] = "element";

print sizeof($fixture) == 1 ? "ok/n" : "not ok/n";

?>

ok

ok

 

我们现在引入一个新的要素,如果期望值和实际值不同,我们就抛出一个异常。这样我们的输出就更简单了。如果测试成功,什么也不做,如果有一个未处理异常,我们知道有问题了。

 

4.使用断言函数来测试Arraysizeof()

<?php

$fixture = Array( );

assertTrue(sizeof($fixture) = = 0);

 

$fixture[] = "element";

assertTrue(sizeof($fixture) = = 1);

 

function assertTrue($condition) {

 if (!$condition) {

  throw new Exception("Assertion failed.");

 }

}

?>

 

现在测试完全自动化了。和我们第一个版本不同,这个版本使得测试完全自动化了。

 

使用自动测试的目的是尽可能少的犯错误。尽管你的代码还不是完美的,用优良的自动测试,你会发现错误会明显减少。自动测试给了你对代码公正的信心。有这个信心,你可以在设计上有大胆的飞越(参见本书后“重构”一章),和你的团队伙伴关系更好(参见本书后“跨团队测试”一章),改善你和客户之间的关系,每天安心入睡,因为你可以证明由于你的努力,系统变得更好了。

 

--------------------------------------------------------------------------------------------------------------------

原文:

Chapter 2. Automating Tests

Even good programmers make mistakes. The difference between a good programmer and a bad programmer is that the good programmer uses tests to detect his mistakes as soon as possible. The sooner you test for a mistake, the greater your chance of finding it, and the less it will cost to find and fix. This explains why it is so problematic to leave testing until just before releasing software. Most errors do not get caught at all, and the cost of fixing the ones you do catch is so high that you have to perform triage with the errors because you just cannot afford to fix them all.

 

Testing with PHPUnit is not a totally different activity from what you should already be doing. It is just a different way of doing it. The difference is between testingthat is, checking that your program behaves as expectedand performing a battery of testsrunnable code-fragments that automatically test the correctness of parts (units) of the software. These runnable code-fragments are called unit tests.

 

In this section, we will go from simple print-based testing code to a fully automated test. Imagine that we have been asked to test PHP's built-in Array. One bit of functionality to test is the function sizeof( ). For a newly created array, we expect the sizeof( ) function to return 0. After we add an element, sizeof( ) should return 1. Example 1 shows what we want to test.

 

Example 1. Testing Array and sizeof( )

<?php

$fixture = Array( );

// $fixture is expected to be empty.

 

$fixture[] = "element";

// $fixture is expected to contain one element.

?>

 

 

 

A really simple way to check whether we are getting the results we expect is to print the result of sizeof( ) before and after adding the element (see Example 2). If we get 0 and then 1, Array and sizeof( ) are behaving as expected.

 

Example 2. Using print to test Array and sizeof( )

<?php

$fixture = Array( );

print sizeof($fixture) . "/n";

 

$fixture[] = "element";

print sizeof($fixture) . "/n";

?>

0

1

 

 

 

Now, we would like to move from tests that require manual interpretation to tests that can run automatically. In Example 3, we write the comparison of the expected and actual values into the test code and print ok if the values are equal. If we see a not ok message, we know something is wrong.

 

Example 3. Comparing expected and actual values to test Array and sizeof( )

<?php

$fixture = Array( );

print sizeof($fixture) == 0 ? "ok/n" : "not ok/n";

 

$fixture[] = "element";

print sizeof($fixture) == 1 ? "ok/n" : "not ok/n";

?>

ok

ok

 

 

 

We now factor out the comparison of expected and actual values into a function that raises an exception when there is a discrepancy (Example 4). Now our test output gets simpler. Nothing gets printed if the test succeeds. If we see an unhandled exception, we know something has gone wrong.

 

Example 4. Using an assertion function to test Array and sizeof( )

<?php

$fixture = Array( );

assertTrue(sizeof($fixture) = = 0);

 

$fixture[] = "element";

assertTrue(sizeof($fixture) = = 1);

 

function assertTrue($condition) {

 if (!$condition) {

  throw new Exception("Assertion failed.");

 }

}

?>

 

 

 

The test is now completely automated. Instead of just testing as we did with our first version, with this version, we have an automated test.

 

The goal of using automated tests is to make fewer mistakes. While your code will still not be perfect, even with excellent tests, you will likely see a dramatic reduction in defects once you start automating tests. Automated tests give you justified confidence in your code. You can use this confidence to take more daring leaps in design (see "Refactoring," later in this book), get along better with your teammates (see "CrossTeam Tests," later in this book), improve relations with your customers, and go home every night with proof that the system is better now than it was that morning because of your efforts.

 

PHPUnit袖珍指南-第二章 自动测试

第二章 自动测试 最好的程序员也会犯错误。好程序员和差程序员的区别在于:好程序员能通过测试尽可能的发现错误。你越快测试错误,你就越快发现它们,发现和修正的成本就越低。这解释了为什么只在软件发布前才测...

PHPUnit袖珍指南 第十二章 测试的其他用途

一旦你开始写自动测试,你就会想要发掘更多用途。以下是一些例子。   12-1. 敏捷文档 通常来说,在采用敏捷方法作为开发流程的项目中,如极限编程,文档很难和迅速变化的项目设计和代码...

PHPUnit袖珍指南-第五章 命令行测试工具

第五章 命令行测试工具 PHPUnit命令行测试工具是通过phpunit命令调用的。如下代码显示如何通过PHPUnit命令行测试工具运行测试。 phpunit ArrayTest PHPUnit...

PHPUnit袖珍指南 第八章 未完成测试

第八章 未完成测试 当开始书写新的测试用例类时,你也许想要从空的测试方法开始,譬如:        public function testSomething( ) {        } 我们...

PHPUnit袖珍指南 第六章 装置器

第六章 装置器 编写测试最耗时的部分是边编写设置整个程序到达一个已知状态,而后在测试结束后返回到原始状态。这个已知状态叫做测试的装置器。 在例5中,装置器很简单,只是存储在变量$fixture中的...

PHPUnit袖珍指南-第一章 简介

很长时间里,我对“你什么时候会为PHPUnit写一个文档”这个问题的回答是:“你不需要PHPUnit文档,去读读JUnit文档或买一本Junit的书,试一试用PHP和PHPUnit来重写Java和Ju...

PHPUnit袖珍指南 第十章 代码覆盖率分析

第十章 代码覆盖率分析 你已经学会了怎么使用单元测试代码,但你怎么测试你的测试呢?你怎么发现没被测试的代码,换句话说,没被测试覆盖的代码?怎么衡量测试的完整性?所有这些问题的答案就是代码覆盖率分析。...

PHPUnit袖珍指南-概述

概述 聪明的Web开发人员会告诉你,越快找到代码的错误,你就能越快修正它,长期而言,项目成本越低。好了,能最有效的检测PHP代码中的错误的工具是PHPUnit,一个开发源代码的框架,它在你不在的时候...

PHPUnit袖珍指南.doc

  • 2012年04月03日 11:19
  • 869KB
  • 下载

PHPUnit袖珍指南-第四章 安装PHPUnit

第4章 安装PHPUnit PHPUnit可以通过PHP扩展和程序库(PEAE)获得。PEAR是可重用PHP组件的框架和分发系统。安装PHPUnit可以通过PEAR安装程序命令获得:   pea...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:PHPUnit袖珍指南-第二章 自动测试
举报原因:
原因补充:

(最多只允许输入30个字)