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

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

阅读更多
一旦你开始写自动测试,你就会想要发掘更多用途。以下是一些例子。
12-1. 敏捷文档
通常来说,在采用敏捷方法作为开发流程的项目中,如极限编程,文档很难和迅速变化的项目设计和代码同步。极限编程要求集体拥有代码,应此,每个开发人员都熟悉整个系统。如果你严格按照规定,编写的类具有自说明能力,你就可以用PHPUnit的TextDox功能根据测试来自动生成文档。这种文档给开发人员对每个类的用途描绘了一个概貌。
PHPUnit的TestDoc功能查找测试类的所有方法的名字,把它们转换成句子,如:testBalanceIsInitiallyZero( )变成“Balance is initially zero。”如果有几个测试方法名字的不同只在于前缀或数字,如testBalanceCannotBecomeNegative( ) 和 testBalanceCannotBecomeNegative2( ),转换过的句子“Balance cannot become negative”只会出现一次,其他这样的测试都会成功。
以下代码显示用PHPUnit 生成的关于银行帐号(Bank Account)类的敏捷文档,命令是:
phpunit --testdox-text BankAccountTest.txt BankAccountTest
结果显示:
BankAccount
- Balance is initially zero
- Balance cannot become negative
也可以用--testdox-html BankAccountTest.htm选项生成HTML格式的敏捷文档。
敏捷文档也用以用于为项目中的第三方包的假定条件来记录文档。当你使用一个外部包的时候,你也在冒一定的风险。外部包可能不想你预期的那样工作,或在未来版本中有微妙变化,导致你的代码不可运行。你也通过对外部包的假定条件编写测试来解决这个问题。如果你的测试成功了,你的假定条件还是有效。如果你对所有假定条件的测试都作了文档,那么,外部包的版本问题将不再是个问题:如果测试成功了,你的系统还会正常运行。
12-2.跨团队测试
当你将测试的假定条件文档化后,你就要为你的测试负责。你和包的提供者一起确定这些假设,你要记住,包的提供者对你的测试一无所知。如果你想要与包的提供者形成更密切的关系,你可以使用测试来沟通协调相关活动。
当你同意和包的供应商协调活动时, 你们可以一起写测试。 这样做,测试会显露出尽可能许多的假定。隐藏假定条件会导致合作失败。通过测试,你可以在提供的包中地将你的期望准确记入文档。当所有的测试运行完毕时,包的供应商就知道它正常运行了。
使用残根(参见本书前面的“残根”一章),你能进一步减弱和包供应商之间的耦合。供应商的工作是测试能够运行在包的真正实现上。你的工作是测试运行在自己的代码上。在你得到供应商的包的真正的实现之前,你使用残根工作。用这种方法,两个团队能够独立工作。
12-3 调试测试
当你得到一份缺陷报告时,你的第一冲动也许是赶快修复它。经验表明,这种冲动效果不好:修复此缺陷可能会导致其它缺陷。
你应该通过检查如下列表来控制冲动:
- 再次核实一下,确信你能重现缺陷。
- 发现最小范围的出现缺陷的代码。例如,输出时如果数字不正确,找到计算那个数字的对象。
- 写一个自动化的测试,如果该缺陷被修复,此测试会通过,反之会失败。
- 修复缺陷。
发现缺陷的最小可靠再现部分提供了一个发现缺陷真正原因的机会。你写的测试将会提高正确修复缺陷的机会,因为新的测试减少了当未来代码改变时取消本次修复的可能性。
前面所写的所有测试都会减少因疏忽而造成其它问题的可能性。
12-4 重构
重构是一种改进现有的代码的设计的受控技术,只有当你有一套测试套件时才能被安全的应用。否则,你也许没有注意到当你修改结构时系统崩溃了。 重构可以分解为一系列行为保留的小的改进,这种改进不会改变程序的行为。
以下情况将帮助你改进项目的代码和设计,使用单位测试验证重构的变革步奏是行为保留的,不会引入其它错误:
- 所有单位测试运行正确。
- 代码传达它的设计意图。
- 代码没有冗余。
- 代码只包含最小数量的类和方法。
--------------------------------------------------------------------------------------------------------------------
原文:
Chapter 12. Other Uses for Tests
Once you get used to writing automated tests, you will likely discover more uses for tests. Here are some examples.
12-1. Agile Documentation
Typically, in a project that is developed using an agile process, such as Extreme Programming, the documentation cannot keep up with the frequent changes to the project's design and code. Extreme Programming demands collective code ownership, so all developers need to know how the entire system works. If you are disciplined enough to use "speaking names" for your tests that describe what a class should do, you can use PHPUnit's TestDox functionality to generate automated documentation for your project based on its tests. This documentation gives developers an overview of what each class of the project is supposed to do.
PHPUnit's TestDox functionality looks at a test class and all the test method names and converts them from camel case PHP names to sentences: testBalanceIsInitiallyZero( ) becomes "Balance is initially zero." If there are several test methods whose names differ only by a suffix of one or more digits, such as testBalanceCannotBecomeNegative( ) and testBalanceCannotBecomeNegative2( ), the sentence "Balance cannot become negative" will appear only once, assuming that all of these tests succeed.
The following code shows the agile documentation for the Bank Account class (in Example 10) generated by running phpunit --testdox-text BankAccountTest.txt BankAccountTest:
BankAccount
- Balance is initially zero
- Balance cannot become negative
Alternatively, the agile documentation can be generated in HTML format by using --testdox-html BankAccountTest.htm.
Agile documentation can be used to document the assumptions you make about the external packages in your project. When you use an external package, you are exposed to the risks that the package will not behave as you expect, and that future versions of the package will change in subtle ways that will break your code, without you knowing it. You can address these risks by writing a test about how the external package works every time you make an assumption. If your test succeeds, your assumption is valid. If you document all your assumptions with tests, future releases of the external package will be no cause for concern: if the tests succeed, your system should continue working.
12-2. Cross-Team Tests
When you document assumptions with tests, you own the tests. The supplier of the packagewho you make assumptions aboutknows nothing about your tests. If you want a closer relationship with the supplier of a package, you can use the tests to communicate and coordinate your activities.
When you agree on coordinating your activities with the supplier of a package, you can write the tests together. Do this in such a way that the tests reveal as many assumptions as possible. Hidden assumptions are the death of cooperation. With the tests, you document exactly what you expect from the supplied package. The supplier will know the package is complete when all the tests run.
By using stubs (see the section "Stubs," earlier in this book), you can further decouple yourself from the supplier. The job of the supplier is to make the tests run with the real implementation of the package. Your job is to make the tests run for your own code. Until such time as you have the real implementation of the supplied package, you use stub objects. Following this approach, the two teams can develop independently.
12-3. Debugging Tests
When you get a defect report, your impulse might be to fix the defect as quickly as possible. Experience shows that this impulse will not serve you well; it is likely that the fix for the defect will cause another defect.
You can hold your impulse in check by doing the following:
Verifying that you can reproduce the defect.
Finding the smallest-scale demonstration of the defect in the code. For example, if a number appears incorrectly in an output, find the object that is computing that number.
Writing an automated test that fails but will succeed when the defect is fixed.
Fixing the defect.
Finding the smallest reliable reproduction of the defect gives you the opportunity to really examine the cause of the defect. The test you write will improve the chances that when you fix the defect, you really fix it, because the new test reduces the likelihood of undoing the fix with future code changes.
All the tests you wrote before reduce the likelihood of inadvertently causing a different problem.
12-4. Refactoring
Refactoring, the controlled technique for improving the design of an existing code base, can be applied safely only when you have a test suite. Otherwise, you might not notice the system breaking while you are carrying out the restructuring. Refactoring can be broken down into a series of small behavior-preserving transformations.
The following conditions will help you to improve the code and design of your project, while using unit tests to verify that the refactoring's transformation steps are, indeed, behavior- preserving and do not introduce errors:
All unit tests run correctly.
The code communicates its design principles.
The code contains no redundancies.
The code contains the minimal number of classes and methods.
分享到:
评论

相关推荐

    PHPUnit袖珍指南.doc

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

    PHPUnit袖珍指南之自动测试

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

    PHPUnit PDF手册【袖珍指南】

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

    PHPUnit袖珍指南之装置器

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

    PHPUnit袖珍指南之PHPUnit的目的

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

    PHPUnit的并行测试库.zip

    PHPUnit的并行测试库

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

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

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

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

    PHPUnit单元测试

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

    PHPUnit单元测试的配置及使用

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

    shopex之phpunit测试环境

    shopex之phpunit测试环境。

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

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

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

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

    phpunit 4.2 最新版

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

    phpunit-speedtrap:报告您PHPUnit测试套件中运行缓慢的测试

    SpeedTrap直接在控制台中报告运行缓慢PHPUnit测试。 许多因素会影响测试执行时间。 如果测试没有与可变的延迟(数据库,网络等)适当隔离,甚至测试计算机上的基本负载都将导致测试执行时间波动。 SpeedTrap有助...

    phpunit、php的单元测试

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

    基于laravel框架的PHPunit 测试爬坑

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

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

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

    phpunit+selenium测试环境搭建浅谈

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

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

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

Global site tag (gtag.js) - Google Analytics