PHPUnit袖珍指南-第一章 简介

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

第一章 简介

很长时间里,我对“你什么时候会为PHPUnit写一个文档”这个问题的回答是:“你不需要PHPUnit文档,去读读JUnit文档或买一本Junit的书,试一试用PHPPHPUnit来重写JavaJunit的例子”。当我和O’Reilly德国办公室的Barbara WeissAlexandra Follenius这样说的时候,他们鼓励我想想是不是可以写一本书来作为PHPUnit的文档。

1.1 基本要求

本书的主题是PHPUnit,这是一个支持用PHP进行测试驱动开发的开放源代码框架。本书覆盖了PHPUnit 2.3,需要PHP5.1支持。当然,大部分例子在PHPUnit 2.02.2PHP5.0也都可以运行。本书的后半部分,“在PHP4中使用PHPUnit”一章中,覆盖了老式的,不再进一步积极发展的在PHP4中运行的PHPUnit

本书的读者需要对使用PHP5进行面向对象编程要很好的理解。建议德文读者读读我写的Professionelle Softwareentwicklung mit PHP 5一书,这是对使用PHP5进行面向对象编程的介绍。英文读者可以参考Andi Gutmans, Stig BakkenDerick Rethans合著的PHP 5 Power Programming

1.2 本书是自由发布的

本书可以在Creative Commons license版权公约下自由获得。访问你可以总是发现本书的最新版本。你根据自己的需要修改和重新发布。当然,相比你发布你自己的版本,我更推荐你发送反馈和更新给

1.3 本书的约定














1.4 如何联系我们




O'Reilly Media, Inc.

1005 Gravenstein Highway North

Sebastopol, CA95472

(800) 998-9938 (in the U.S. or Canada)

(707) 829-0515 (international/local)

(707) 829-0104 (fax)






1.5 鸣谢

我要感谢Kent BeckErich Gamma发明了Junit给了我发明PHPUnit的灵感,我更要谢谢Kent Beck写了Junit口袋书一书,这使我有了写本书的冲动。同时我要感Allison Randal, Alexandra FolleniusBarbara Weiss,是他们在O’Reilly负责本书。

我要感谢Andi Gutmans, Zeev SuraskiMarcus Börger,是他们开发了PHP5的核心Zend Engine 2。我还有谢谢Derick Rethans,他开发了PHP扩展功能Xdebug,使得PHPUnit的代码覆盖率功能成为可能。最后,我还有谢谢Michiel Rook,他写了PhingPHPUnit任务。




Chapter 1. Introduction

For a very long time, my answer to the question, "When will you write documentation for PHPUnit?" has been, "You do not need documentation for PHPUnit. Just read the documentation for JUnit or buy a book on JUnit and adapt the code examples from Java™ and JUnit to PHP and PHPUnit." When I mentioned this to Barbara Weiss and Alexandra Follenius from the O'Reilly Germany office, they encouraged me to think it over and write a book that would serve as the documentation for PHPUnit.


1.1. Requirements

The topic of this book is PHPUnit, an open source framework for test-driven development with the PHP programming language. This book covers Version 2.3 of PHPUnit, which requires PHP 5.1. However, most of the examples should work with PHPUnit Versions 2.02.2, as well as PHP 5.0. The "PHPUnit for PHP 4" section, later in this book, covers the older, no longer actively developed version of PHPUnit for PHP 4.


The reader should have a good understanding of object-oriented programming with PHP 5. To German readers, I recommend my book Professionelle Softwareentwicklung mit PHP 5 as an introduction to object-oriented programming with PHP 5. A good English book on the subject is PHP 5 Power Programming by Andi Gutmans, Stig Bakken, and Derick Rethans (Prentice Hall PTR).


1.2. This Book Is Free

This book is available under the Creative Commons license. You will always find the latest version of this book at its web site: You may distribute and make changes to this book however you wish. Of course, rather than distribute your own private version of the book, I would prefer you send feedback and patches to


1.3. Conventions Used in This Book

The following is a list of the typographical conventions used in this book:






Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, directories, and Unix utilities.


Constant width


Indicates commands, options, switches, variables, functions, classes, namespaces, methods, modules, parameters, values, objects, the contents of files, or the output from commands.


Constant width bold


Shows commands or other text that should be typed literally by the user.


Constant width italic


Shows text that should be replaced with user-supplied values.


You should pay special attention to notes set apart from the text with the following styles:



This is a tip, suggestion, or general note. It contains useful supplementary information about the topic at hand.



This is a warning or note of caution.


1.4. How to Contact Us

We have tested and verified the information in this book to the best of our ability, but you may find that features have changed (or even that we have made mistakes!).


As a reader of this book, you can help us to improve future editions by sending us your feedback. Please let us know about any errors, inaccuracies, bugs, misleading or confusing statements, and typos that you find anywhere in this book.


Please also let us know what we can do to make this book more useful to you. We take your comments seriously and will try to incorporate reasonable suggestions into future editions. You can write to us at:


O'Reilly Media, Inc.

1005 Gravenstein Highway North

Sebastopol, CA95472

(800) 998-9938 (in the U.S. or Canada)

(707) 829-0515 (international/local)

(707) 829-0104 (fax)

To ask technical questions or to comment on the book, send email to:

The web site for PHPUnit Pocket Guide lists examples, errata, and plans for future editions. You can find this page at:

For more information about this book and others, see the O'Reilly web site:


1.5. Acknowledgments

I would like to thank Kent Beck and Erich Gamma for JUnit and for the inspiration to write PHPUnit. I would also like to thank Kent Beck for his JUnit Pocket Guide, which sparked the idea for this book. I would like to thank Allison Randal, Alexandra Follenius, and Barbara Weiss for sponsoring this book at O'Reilly.


I would like to thank Andi Gutmans, Zeev Suraski, and Marcus Börger for their work on the Zend Engine 2, the core of PHP 5. I would like to thank Derick Rethans for Xdebug, the PHP extension that makes PHPUnit's code-coverage functionality possible. Finally, I would like to thank Michiel Rook, who wrote the PHPUnit tasks for Phing.


netty权威指南 ---------第三章 入门应用

  • bingjing12345
  • bingjing12345
  • 2014年09月21日 09:50
  • 4568

Netty权威指南 第2版学习笔记7——MessagePack编解码及LengthFieldBasedFrameDecoder

MessagePack是一个高效的二进制序列化框架,像JSON一样支持不同语言间的数据交换,速度更快,序列化之后的码流更小MessagePack特点 编解码高效,性能高 序列化后的码流小 支持跨语言 ...
  • xundh
  • xundh
  • 2017年01月02日 21:55
  • 2042


手机内置浏览器:  Android:Andriod BrowserIOS:Mobile SafariBlackBerry:WebkitSymbian S60: Web Browser f...
  • herion_liu
  • herion_liu
  • 2016年06月07日 16:01
  • 1327

PHPUnit袖珍指南-第一章 简介

  • hzcyclone
  • hzcyclone
  • 2012年07月04日 15:06
  • 297

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

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

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

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

PHPUnit袖珍指南 第六章 装置器

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

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

第五章 命令行测试工具 PHPUnit命令行测试工具是通过phpunit命令调用的。如下代码显示如何通过PHPUnit命令行测试工具运行测试。 phpunit ArrayTest PHPUnit...
  • hzcyclone
  • hzcyclone
  • 2012年07月04日 15:37
  • 314


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

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

第十章 代码覆盖率分析 你已经学会了怎么使用单元测试代码,但你怎么测试你的测试呢?你怎么发现没被测试的代码,换句话说,没被测试覆盖的代码?怎么衡量测试的完整性?所有这些问题的答案就是代码覆盖率分析。...
  • hzcyclone
  • hzcyclone
  • 2012年07月04日 15:39
  • 388
您举报文章:PHPUnit袖珍指南-第一章 简介