12
返回列表 发新帖
楼主: Sky-Tiger

Is Software Becoming More Testable?

[复制链接]
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
11#
 楼主| 发表于 2010-4-9 23:51 | 只看该作者
Leverage the use of dependency frameworks

If the architecture permits the use of frameworks, consider using a dependency injection framework like Spring. Spring framework allows externalizing the dependencies by moving them to a configuration file (xml file). The advantages are:

    * A nice clean way of managing dependencies
    * Sparse use of singletons
    * Easy to plug in and out different implementations of the dependencies
    * In built support for unit testing

Define and document the method functionality

The method documentation should clearly state what the method is expected to do. If there are branches within the method, the documentation should explain the conditional logic. Consider giving as much documentation as possible to help the unit test developer to understand the method’s purpose.
Define and document the method contract

Design by contract (DBC) is a way of writing the method documentation so that it clearly defines what the code will do. It should clearly capture the preconditions, post conditions and any invariants should be clearly documented.

The following tags can be used for method documents:
@pre – Conditions that have to be true before method can be called
@post – Conditions that should be true after the method gets executed
@invariant – This is a constraint on the object state that is guaranteed to be true at all times, such as Collection.size() == Collection.toArray().length().

In the detailed design phase when identifying methods and behavior of classes, also identify the contract of the method. This contract specifies terms under which the code will be executed. The contract defines the following and this has to be documented in the method specification.

    * Input – Range of expected values for the input(s) of the method
    * Output - Range of expected values for output(s) of the method
    * Boundary - Clearly state the boundary conditions for the method. The conditions in which the method would fail (negative flows)
    * Exception – The cases when the method would fail by an exception. Document the input values that could cause this. Document the expected behavior of the system when the method fails through an exception.
    * Assumptions – Document the assumptions (if any) on how the method will behave. For example methods that deal with data in some formats should document the format. The assumption is that the data format would not change.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
12#
 楼主| 发表于 2010-4-9 23:51 | 只看该作者
Before writing the unit tests

Before getting started on writing the unit tests, it is very important to read the method documentation and understand the purpose of the method. This would give a complete idea about what is to be tested. It is also necessary to formulate the various conditions that are bad or invalid and the different scenarios in which the method would run into errors. Sometimes when the method document is not sufficient it would be very useful to update the method document with all the details that are needed to unit test. Some pointers to keep in mind before starting the unit test are:

    * Write the unit tests based on the documentation – It is important that the method documentation details the behavior of the method. If a public method does not give out all the necessary details, it might be a good idea to add that documentation as the developer. The idea is to adhere to the verbal contract of the method.
    * Write the unit tests based on the code – Revisit the code to be unit tested. Understand if it is working as the documentation suggests. If there is a difference between the contract and the code, then one of them must be fixed. Once the code looks fine there would be a clear idea of how to unit test. The places where assertions and failures have to be tested could be understood.
    * Write the unit tests based on the expected results – Test for all the scenarios – success, failure, boundary conditions and exceptions.
    * Unit Test the assumptions – there would be assumptions in the code when there are dependencies. For example an API call could be assumed to work always. It is good to unit test the piece if the API did not function as defined. By this we can see if the code is able to recover from an error and not crash.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
13#
 楼主| 发表于 2010-4-9 23:51 | 只看该作者
Advantages of thinking about unit testing upfront

Thinking about unit testing during design, leads to a good design. Unit tests are not just pieces to catch ‘bugs’, they also drive the design. Unit tests enforce the contract of the classes and methods and thus making sure the design adheres to the contract of the system. Some of the advantages of thinking of unit testing during design are following:

    * Unit tests drive the developers to think about interfaces. This practice leads to writing loosely coupled code. It also benefits in writing good apis.
    * Unit tests lead to better design by allowing a right mix of composition and interfaces
    * Unit tests drive the need for good method documentation. Remember the standard: If your program isn’t worth documenting, it probably isn’t worth running (Nagler, 1995)

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
14#
发表于 2010-4-11 23:56 | 只看该作者
nice job

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表