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

标签: 测试编程interface单元测试objectmethods
5234人阅读 评论(0) 收藏 举报

第九章 测试优先编程

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









接下来简单介绍一些必要的测试优先编程内容。你可以在其他其它的书中找到更多的议题,譬如《举例说明测试驱动开发-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.








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).



* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    • 访问:184634次
    • 积分:2101
    • 等级:
    • 排名:第18170名
    • 原创:13篇
    • 转载:5篇
    • 译文:13篇
    • 评论:41条