单元测试和功能测试之间的区别是什么?单元测试也可以测试函数吗?


当前回答

单元测试

单元测试包括测试最小的代码单元,通常是函数或方法。单元测试主要由单元/方法/函数的开发人员完成,因为他们了解函数的核心。开发人员的主要目标是通过单元测试来覆盖代码。

它有一个限制,即某些函数不能通过单元测试进行测试。即使在成功完成所有单元测试之后;它不保证产品的正确操作。相同的函数可以在系统的少数部分中使用,而单元测试仅为一种用途而编写。

功能测试

It is a type of Black Box testing where testing will be done on the functional aspects of a product without looking into the code. Functional testing is mostly done by a dedicated Software tester. It will include positive, negative and BVA techniques using un standardized data for testing the specified functionality of product. Test coverage is conducted in an improved manner by functional tests than by unit tests. It uses application GUI for testing, so it’s easier to determine what exactly a specific part of the interface is responsible for rather to determine what a code is function responsible for.

其他回答

单元测试通常由开发人员完成。这样做的目的是确保他们的代码正常工作。一般的经验法则是使用单元测试覆盖代码中的所有路径。

功能测试:这是一个很好的参考。功能测试说明

单元测试——测试单个单元,例如类中的方法(函数),模拟所有依赖项。

功能测试——又名集成测试,测试系统中的一部分功能。这将测试许多方法,并可能与数据库或Web服务等依赖项交互。

单元测试告诉开发人员代码的工作是正确的;功能测试告诉开发人员代码正在做正确的事情。

你可以在单元测试与功能测试中阅读更多内容


单元测试和功能测试在现实生活中的类比可以描述如下:

Many times the development of a system is likened to the building of a house. While this analogy isn't quite correct, we can extend it for the purposes of understanding the difference between unit and functional tests. Unit testing is analogous to a building inspector visiting a house's construction site. He is focused on the various internal systems of the house, the foundation, framing, electrical, plumbing, and so on. He ensures (tests) that the parts of the house will work correctly and safely, that is, meet the building code. Functional tests in this scenario are analogous to the homeowner visiting this same construction site. He assumes that the internal systems will behave appropriately, that the building inspector is performing his task. The homeowner is focused on what it will be like to live in this house. He is concerned with how the house looks, are the various rooms a comfortable size, does the house fit the family's needs, are the windows in a good spot to catch the morning sun. The homeowner is performing functional tests on the house. He has the user's perspective. The building inspector is performing unit tests on the house. He has the builder's perspective.


总结一下,

单元测试是从程序员的角度编写的。它们用于确保类的特定方法(或单元)执行一组特定的任务。

功能测试是从用户的角度编写的。它们确保系统按照用户的期望运行。

单元测试测试一个独立的行为单元。行为的单位是什么?它是系统中可以独立进行单元测试的最小部分。(这个定义实际上是循环的,它根本不是一个定义,但在实践中似乎很管用,因为你可以直观地理解它。) 功能测试测试一个独立的功能部分。


行为单元非常小:虽然我绝对不喜欢这种愚蠢的“每个方法测试一个单元”的咒语,但从大小的角度来看,它是正确的。行为单元介于方法的一部分和几个方法之间。最多是一个对象,但不能超过一个。 一个功能块通常包含许多方法,并跨越多个对象,通常跨越多个体系结构层。


单元测试应该是这样的:当我调用validate_country_code()函数并将国家代码'ZZ'传递给它时,它应该返回false。 一个功能测试是:当我用国家代码ZZ填写发货表单时,我应该被重定向到一个帮助页面,该页面允许我从菜单中选择国家代码。


Unit tests are written by developers, for developers, from the developer's perspective. Functional tests may be user facing, in which case they are written by developers together with users (or maybe with the right tools and right users even by the users themselves), for users, from the user's perspective. Or they may be developer facing (e.g. when they describe some internal piece of functionality that the user doesn't care about), in which case they are written by developers, for developers, but still from the user's perspective.


在前一种情况下,功能测试还可以作为验收测试,并作为功能需求或功能规范的可执行编码,在后一种情况下,它们还可以作为集成测试。 单元测试经常更改,而功能测试在主要版本中不应该更改。


“功能测试”并不意味着测试代码中的函数(方法)。一般来说,这意味着您正在测试系统功能——当我在命令行上运行foo file.txt时,file.txt中的行可能会颠倒过来。相反,单个单元测试通常包含单个方法的单个情况——length(“hello”)应该返回5,length(“hi”)应该返回2。

另请参阅IBM关于单元测试和功能测试之间界限的论述。