`
shangjava
  • 浏览: 1189004 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

PHPUnit袖珍指南 第九章 测试优先编程

阅读更多

第九章 测试优先编程

单元测试是几种软件开发实践和过程至关重要的部份,譬如测试优先编程,极限编程[3],测试驱动开发[4] 单元测试也允许在结构上不支持的编程语言中支持契约式设计[5]

[3] http://en.wikipedia.org/wiki/Extreme_Programming

[4] http://en.wikipedia.org/wiki/Test-driven_development

[5] http://en.wikipedia.org/wiki/Design_by_Contract

您可以完成代码后使用PHPUnit。但是,代码有错误,越快找到它们,测试就越有价值。因此,与其在代码错误“完成”后几个月才写测试,我们可以在缺陷引入后几天,几小时甚至几分钟内就写好它们。对了,为什么这样就够了?为什么不在一个缺陷引入前就写好测试呢?

测试优先编程,是极限编程和测试驱动开发的一部分,基于这种想法并发挥到了极限。以今天的计算机能力,我们可以每天进行数千次的测试,每个测试运行数千遍。我们能使用所有这些测试的反馈来编程,就向小步跑,除了以前有的测试之外,每步都有自动化的测试作保证。测试就是岩钉一样,保证不管发生了,一旦取得了进展,就会固定已有的进展。

当你第一次写测试时,它可能无法运行,因为你在调用还未实现的对象和方法。这也许起初让人感到奇怪,但你会很快习惯它。测试优先编程被认为是一种有效的的实践,符合面向对象的编程技术原则:面向接口而不是面向实现。当你写测试时,你就在考虑正在测试的对象的接口。从外面看,对象应该什么样?当你真正地让测试可以运行,你就是在考虑纯实现。接口会被失败的测试固定下来。

接下来简单介绍一些必要的测试优先编程内容。你可以在其他其它的书中找到更多的议题,譬如《举例说明测试驱动开发-Kent Beck》(Addison Wesley)或《测试驱动开发:Dave Astels的实践指南》(Prentice Hall)。

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

原文:

Chapter 9. Test-First Programming

Unit tests are a vital part of several software development practices and processes, such as test-first programming, Extreme Programming,[3] and test-driven development.[4] They also allow for design-by-contract[5] in programming languages that do not support this methodology with language constructs.

[3] http://en.wikipedia.org/wiki/Extreme_Programming

[4] http://en.wikipedia.org/wiki/Test-driven_development

[5] http://en.wikipedia.org/wiki/Design_by_Contract

You can use PHPUnit to write tests once you are done programming. However, the sooner a test is written after an error has been introduced, the more valuable the test is. So, instead of writing tests months after the code is "complete," we can write tests days, hours, or minutes after the possible introduction of a defect. Why stop there? Why not write the tests a little before the possible introduction of a defect?

Test-first programming, which is part of Extreme Programming and test-driven development, builds upon this idea and takes it to the extreme. With today's computational power, we have the opportunity to run thousands of tests, thousands of times per day. We can use the feedback from all of these tests to program in small steps, each of which carries with it the assurance of a new automated test, in addition to all the tests that have come before. The tests are like pitons, assuring you that no matter what happens, once you have made progress, you can only fall so far.

When you first write the test, it cannot possibly run because you are calling on objects and methods that have not been programmed yet. This might feel strange at first, but, after a while, you will get used to it. Think of test-first programming as a pragmatic approach to following the object-oriented programming principle of programming to an interface instead of programming to an implementation: while you are writing the test, you are thinking about the interface of the object you are testingwhat does this object look like from the outside? When you go to make the test really work, you are thinking about pure implementation. The interface is fixed by the failing test.

What follows is a necessarily abbreviated introduction to test-first programming. You can explore the topic further in other books, such as Test-Driven Development: By Example by Kent Beck (Addison Wesley) or Test-Driven Development: A Practical Guide by Dave Astels (Prentice Hall).

分享到:
评论

相关推荐

    PHPUnit袖珍指南.doc

    PHPUnit袖珍指南.doc PHPUnit 单元测试 从环境配置到 段元详细介绍

    PHPUnit袖珍指南之自动测试

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

    PHPUnit袖珍指南之装置器

    编写测试最耗时的部分是边编写设置整个程序到达一个已知状态,而后在测试结束后返回到原始状态。这个已知状态叫做测试的装置器。本文将为大家介绍PHPUnit袖珍指南之装置器。

    PHPUnit PDF手册【袖珍指南】

    PHPunit pdf教程,非常不错,值得推荐

    PHPUnit袖珍指南之PHPUnit的目的

    当我们开始测试大量的array_*()函数时,每个都需要一个测试。我们可以每 个都从头写起。但是,更好的方法是一次性写好一个测试基础构架,以后就只用写每个测试不同的部分。PHPUnit就是这样一个基础构架。

    phpunit-speedtrap, 在PHPUnit测试套件中,报告缓慢运行的测试.zip

    phpunit-speedtrap, 在PHPUnit测试套件中,报告缓慢运行的测试 phpunit-speedtrap SpeedTrap报告你的控制台中PHPUnit测试套件中运行缓慢的测试。影响测试执行时间的许多因素。 测试没有正确地从变量延迟( 数据库,...

    PHPUnit的并行测试库.zip

    PHPUnit的并行测试库

    PHPUnit(PHP 单元测试框架) v6.5.3.zip

    PHPUnit(PHP 单元测试框架)简介 PHPUnit是一个轻量级的PHP测试框架。它是在PHP5下面对JUnit3系列版本的完整移植,是xUnit测试框架家族的一员(它们都基于模式先锋Kent Beck的设计) 单元测试是几个现代敏捷开发方法...

    PHPUnit单元测试的配置及使用

    PHPUnit单元测试的配置及使用,PHPUnit单元测试的配置及使用,PHPUnit单元测试的配置及使用,PHPUnit单元测试的配置及使用,PHPUnit单元测试的配置及使用

    针对phpunit 接口自动化测试系列

    针对接口自动化----phpunit 接口自动化测试系列,接口自动化用例的编写以及PHP及PHPUNIT的安装

    PHPUnit单元测试

    php的单元测试,单元测试是每个开发必备的素质 PHPUnit作为php单元测试工具,提供各种支持,如mock

    shopex之phpunit测试环境

    shopex之phpunit测试环境。

    phpunit-parallel:PHPUnit并发测试执行器

    另一个PHPUnit并发测试执行器 我不再维护该项目,它仅在这里作为存档。 另一种方法是,检查出 它是如何工作的? phpunit-parallel使用基于react-php的even循环与许多辅助进程进行通信。 它通过unix管道(fd = 3)...

    一个PHPUnit插件可以检测经过测试的代码或测试中的内存泄漏

    一个PHPUnit插件,可以检测经过测试的代码或测试中的内存泄漏

    phpunit、php的单元测试

    使用PHPUnit进行测试与你曾经的做法并非完全不同。它只是在操作方式上有所不同。区别在于测试和执行成套测试,前者检查程序行为是否符合预期,后者是可运行的代码片断,它们自动测试软件各部分(单元)的正确性。...

    phpunit 4.2 最新版

    phpunit 3 67 最新版 PHPUnit是一个轻量级的PHP测试框架 它是在PHP5下面对JUnit3系列版本的完整移植 是xUnit测试框架家族的一员 它们都基于模式先锋Kent Beck的设计 单元测试是几个现代敏捷开发方法的基础 使得...

    基于laravel框架的PHPunit 测试爬坑

    基于 Laravel 框架的 phpunit 单元测试爬坑记录。适用于laravel初学者,和单元测试初学者。后期代码有过修改,欢迎大家加好友给建议~(WX:YAN-ZI-ZY)

    phalcon框架的基本使用方法和phpunit测试

    phalcon框架的基本使用方法 phpunit测试框架的安装和测试

    think-phpunit, ThinkPHP应用单元测试辅助类.zip

    think-phpunit, ThinkPHP应用单元测试辅助类

    phpunit+selenium测试环境搭建浅谈

    这是我个人对phpunit+selenium测试环境搭建的一些总结绝对原创,本人只是菜鸟水平有很多没有写到的地方还望多多包涵。

Global site tag (gtag.js) - Google Analytics