如何提高TDD(Test Driven Devlopment)的开发效率
在Java的开发中非常提倡采用TDD的方式开发程序,很多人会觉得这样会写很多的代码,甚至测试代码要超过功能代码好几倍,刹眼一看似乎这样并没提高效率,当然从短期看似乎似乎这样的,但是从长远看这样是划算的。以后我们重构了系统的代码,在进行测试就能节省很多时间,可以有测试代码来验证我们的重构,并没有改变程序原有的行为准则。这样我们重构代码时也没有后顾之忧了。
但是如何提高TDD开发的效率呢(希望大家献计献策)?
在TDD的开发中我发现了一些问题也是我在一些惨痛的教训中总结出来的,希望和你们一起分享
1.不要写出过于复杂的测试代码,一旦这样的测试代码中都存在bug就无法达到测试我们功能代码的目的,我们一直讲编程中提高的KISS原则(Keep It Simple, Stupid)要在这里充分发扬, 另外一个适度的保持DRY原则(Don’t Repeat Yourself),为什么是适度的呢,这里过于DRY的代码可能导致测试代码过于复杂因而导致测试代码中存在bug,从而无法正确的测试功能代码,连验证代码的代码都出问题了,还能谈对功能代码的测试么?
2. 准备测试数据可以通过一定规则生成,避免么一个都手工对对象进行复制,这样可以提高一部分效率,特别是构建一些复杂的组合对象时,及需要构造一定数量的此类对象。
3. 断言非基本类型,如自定义对象,对象集合等。这样的代码往往需要我们使用assertEquals(…,…);逐个的对其成员进行比较,而这些代码几乎是机械式的对于这种代码我们在测试中应该充分的运用DRY原则(参考上文)注意知识这种情况!首先将对象的比较其提取出一个方法,以后在其他地方需要断言两个对象或者集合进行断言时可直接调用前面已经提取好的方法,避免大面积的对某一对象的成员逐个assertEquals(…,…);
一个业务提供中的domain, 领域对象 或者说 entity, 都准备好对于的 assertXXXEquals,assertXXXListEquals, assertXXXArrayEquals, …
4. 另外要养成对一个习惯始终保持对功能代码和测试代码的pass,不要前期一段时间采用TDD后面由于时间关系放弃了,只顾着写功能代码而放弃测试代码,一定要每时每刻保持两者的pass, 对待测试代码要和功能代码一样尊重。你的代码行为需要它来监督(民主的代码更可靠)
关于如何如何编写单元测试有效的对涉及到操作数据库的场景,如何做到完成单元测试的同事也不会破坏数据库原有数据?以及其他其他结合Mock框架实现单元测试等心得及经验总结望大家在这方面大家在这方面多多献计献策!让我们彼此共同成长!
Tuesday, October 25, 2011
By: cppmule or sniper
E-Mail: 47406310@qq.com
分享到:
相关推荐
Test Driven: Practical TDD and Acceptance TDD for Java Developers (PDF英文版)
TDD(Test-Driven Development) 测试驱动开发是敏捷开发中的一项核心实践和技术,也是一种设计方法论。TDD得原理是在开发功能代码之前,先编写单元测试用例代码,测试代码确定需要编写什么产品代码。TDD虽是敏捷方法...
C# and .NET Core Test Driven Development: Dive into TDD to create flexible, maintainable, and production-ready .NET Core applications Learn how to apply a test-driven development process by building ...
Test.Driven.TDD.and.Acceptance.TDD.for.Java.Developers. 测试驱动开发方面相当好的一本书,值得下载
在软件开发领域,测试驱动开发(Test Driven Development,TDD)技术在逐渐为广大开发者所接受,它是在添加新代码之前让你的测试代码出错的一种开发技术。在开始编写代码之前首先要写你的测试代码,然后再编写代码让...
By taking you through the development of a real web application from beginning to end, the second edition of this hands-on guide demonstrates the practical advantages of test-driven development (TDD) ...
Test-Driven Development(TDD)简介,使用Nunit开发自动测试案例.
测试驱动开发(Test Driven Development.TDD)技术在逐渐为广大开发者所接受.它是在添加新代码之前让你的测试代码出错的一种开发技术。在开始编写代码之前首先要写测 试代码.然后再编写代码让你的测试通过.可以...
测试驱动的软件开发 TDD (Test-Driven Development)+敏捷开发过程 多个经典资料打包集合
Test Driven Development in Ruby: A Practical Introduction to TDD Using Problem and Solution Domain Analysis by Bala Paranj English | 5 Apr. 2017 | ISBN: 1484226372 | 288 Pages | PDF | 5.32 MB Learn ...
Test-driven development (TDD) is a development approach that relies on a test-first procedure that emphasises writing a test before writing the necessary code, and then refactoring the code to ...
模型驱动的开发,新的开发方式
This book is intended for Python developers who want to use the principles of test-driven development (TDD) to create efficient and robust applications. In order to get the best out of this book, you ...
iOS Test Driven Development by Tutorials FIRST EDITION, Learn real-world test-driven development PDF & EPUB & SourceCode
Test Driven Development Process.
测试驱动开发的艺术Test.Driven.TDD.and.Acceptance.TDD.for.Java.Developers
Test-driven development (TDD) is a development approach that relies on a test-first procedure that emphasises writing a test before writing the necessary code, and then refactoring the code to ...
Test-driven development (TDD) is a proven way to find software bugs early. Writing tests before your code improves the structure and maintainability of your app. Test-driven iOS Development with ...
This book has great reviews by other peering programmers. TDD is always our good friends, right? Honestly, I haven't read it but I might start within the next following days. Let's enjoy together.