TDD 第一章 使用入门(二)

本文介绍测试驱动开发(TDD)的概念及应用,强调先编写测试再编写实际代码的重要性。通过具体的例子说明如何为一个类编写相应的测试类,以及它们之间的依赖关系。
[size=large][b]1.1 Testing[/b] [/size]
[size=large]
TDD——测试驱动开发,意味着你需要为你实际书写的每一部分代码书写测试。也意味着你将率先书写测试。测试是衡量代码是否完成所需工作的一种手段,在写完相对应的代码后,测试可以保证代码实现了测试的功能。

你编的每个类都将有它所对应的测试类,在1.1节中,StudentTest类就是Student类的测试类。
[img]/upload/attachment/74189/58383d25-dd53-3315-be81-834f401ce6fc.bmp[/img]
StudentTest类将不得不创建Student类的对象,并且对这些对象发送消息,一旦所有的消息发送成功,就证明这个类可以正常工作。很明显,就像图中的连线所表示的一样,StudentTest是依赖于Student类的;相反,Student类并不依赖于StudentTest类,应该向被测试类隐藏测试类的实现。


[quote] Test-driven development means that you will write tests for virtually every bit of code. It also means that you will write the tests first. The tests are a means of specifying what the code needs to do. After writing the corresponding code, the tests are run to ensure that the code does what the tests specify.
Each class you code will have a corresponding test class. In Figure 1.1, StudentTest is the test class for the production class Student.
StudentTest will have to create objects of class Student, send messages to these objects, and prove that once all the messages have been sent, things are as expected. StudentTest is thus dependent on Student, as shown by the navigable association in the diagram. Conversely, Student is not dependent on StudentTest: The production classes you build should know nothing about the tests written for them.[/quote][/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值