blog.programfan.info
新网站即将启用

[译]EUnit——erlang的轻量级单元测试框架(1)

译文:

单元测试

单元测试中的单元没有固定的定义,它可以是模块,函数,进程,甚至整个应用。 不过一般来说常用的是用模块或者函数做一个单元。为了测试这个单元,需要做的是首先确定一系列的测试,然后根据需要设置好它的最小的配置环境(一般来说,都是不需要专门进行设定的,据我本人理解,操纵数据库的测试需要设置好环境,其他情况我暂还没有想到)。这时你就可以进行测试了,然后得到测试结果。这儿要注意的是最后一定不要忘记的是:要进行垃圾回收,确保下次能够再次进行测试。一个测试框架是为了在进程的每一个环结都能帮助你很容易的写测试代码,运行它,并得到错误信息以方便你能修改这些bug.

一、单元测试的好处

1.1 减少程序修改时的风险

  大多数程序在它的生命周期里会因为如下原因被修改:修复bug,增加功能,优化性能,有时还可能为了让它更好的工作而对它进行重构或清理。每次代码的修改都有引入bug或重新引入之前已经修复过的bug。如果用有一系列的单元测试,你就可以用很小的代价很容易的得到修改后的代码是否还是按预想的一样工作(这儿叫做回归测试)。这些工作可以减少修改或重构代码时遇到的问题。

1.2 帮助指导和加速开发过程

  因为要集中精力来让测试代码通过,因而会提高开发人员的工作效率,而不至于在早期的优化中超出预定或者迷失方向,并且它能让你在最开始的时候写出正确的代码(被称为测试驱动开发so-called test-driver development)

1.3 在实现时帮助区分接口

  因为要写测试代码,你可能会发现一些不应该有的信赖关系, 其实它可以抽取出来实现一个干净的代码设计。这会帮助你在扩展这段代码之前消除(eliminate)这些差的信赖。

1.4 让组件更容易集成

  用自下而上的方式来进行测试,开始时在最小的程序单元上进行测试,确保它按预想的工作。这样在测试含有N个类似单元的上一层次的组件时就会变的很容易。这在后面“集成测试”进会专门介绍。

1.5 可以作为它本身的文档

  这些测试代码可以当作文档来阅读,它一般会即有正确用法也有错误的用法,当然还有它对应的结果。

原文:

Unit testing

Unit Testing is testing of individual program "units" in relative isolation. There is no particular size requirement: a unit can be a function, a module, a process, or even a whole application, but the most typical testing units are individual functions or modules. In order to test a unit, you specify a set of individual tests, set up the smallest necessary environment for being able to run those tests (often, you don't need to do any setup at all), you run the tests and collect the results, and finally you do any necessary cleanup so that the test can be run again later. A Unit Testing Framework tries to help you in each stage of this process, so that it is easy to write tests, easy to run them, and easy to see which tests failed (so you can fix the bugs).

Advantages of unit testing

Reduces the risks of changing the program
Most programs will be modified during their lifetime: bugs will be fixed, features will be added, optimizations may become necessary, or the code will need to be refactored or cleaned up in other ways to make it easier to work with. But every change to a working program is a risk of introducing new bugs - or reintroducing bugs that had previously been fixed. Having a set of unit tests that you can run with very little effort makes it easy to know that the code still works as it should (this use is called regression testing; see Terminology). This goes a long way to reduce the resistance to changing and refactoring code.
Helps guide and speed up the development process
By focusing on getting the code to pass the tests, the programmer can become more productive, not overspecify or get lost in premature optimizations, and create code that is correct from the very beginning (so-called test-driven development; see Terminology).
Helps separate interface from implementation
When writing tests, the programmer may discover dependencies (in order to get the tests to run) that ought not to be there, and which need to be abstracted away to get a cleaner design. This helps eliminate bad dependencies before they spread throughout the code.
Makes component integration easier
By testing in a bottom-up fashion, beginning with the smallest program units and creating a confidence in that they work as they should, it becomes easier to test that a higher-level component, consisting of several such units, also behaves according to specification (known asintegration testing; see Terminology).
Is self-documenting
The tests can be read as documentation, typically showing both examples of correct and incorrect usage, along with the expected consequences.
原文地址: http://svn.process-one.net/contribs/trunk/eunit/doc/overview-summary.html#Unit_testing
posted @ 2011-04-01 16:15  Gordon Chao  Views(515)  Comments(0Edit  收藏  举报
www.programfan.info
新网站即将启用