您的位置:首页 > 其它

单元测试

2016-03-25 10:16 253 查看
首先,先上概念,以下出自wiki:

In computer programming, unit testing is a software testing method by which individual units of source code, sets of one or more computer program
modules together with associated control data, usage procedures,and operating procedures, are tested to determine whether they are fit for use. Intuitively, one can view a unit as the smallest testable part of an application. In procedural programming, a unit
could be an entire module, but it is more commonly an individual function or procedure. In object-oriented programming, a unit is often an entire interface, such as a class, but could be an individual method.Unit tests are short code fragments created by programmers
or occasionally by white box testers during the development process.It forms the basis for component testing.

看了几篇文章我觉得这是最正统的一个对单元测试的定义,单元测试是白盒测试的一种,它是对代码的最小功能单元进行测试。

而什么是最小单元,我觉得这个其实可以对照单一职责。在过程化编程中,一个单元就是单个程序、函数、过程等;对于面向对象编程,最小单元就是方法,包括基类(超类)、抽象类、或者派生类(子类)中的方法。

这么做的好处在于写单元测试,可以使我们尽早地发现错误,同时提高了代码的质量。

A unit test is code that exercises a specific portion of your codebase in a particular context. Typically, each unit test sends a specific
input to a method and verifies that the method returns the expected value, or takes the expected action. Unit tests prove that the code you are testing does in fact do what you expect it to do.

我们在单元测试调用方法时,会输入一些参数,并对调用方法后产生的结果有一个预期值。

其实我们的单元测试就是在测试我们写出的代码和我们预期它达到的效果一不一致。

今天大概写点概念,明天会增加Junit用法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: