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


当前回答

TLDR:

回答这个问题:单元测试是功能测试的一个子类型。


有两大类:功能测试和非功能测试。我发现的最好的(非详尽的)例子是这个(来源:www.inflectra.com):

(1)单元测试:测试小段代码(函数/方法)。它可以被认为是(白盒)功能测试。

当功能放在一起时,你创建了一个模块=一个独立的部分,可能有一个可以测试的用户界面(模块测试)。一旦你有了至少两个独立的模块,然后你把它们粘在一起,然后就来了:

(2)集成测试:当你把两个或多个(子)模块或(子)系统放在一起,看看它们是否能很好地发挥作用。

然后你整合了第3个模块,然后是第4和第5个模块,按照你或你的团队认为合适的顺序,一旦所有的拼图碎片放在一起,就完成了

(3)系统测试:对软件进行整体测试。这差不多就是“所有部件的集成测试”。

如果没问题,那就来吧

(4)验收测试:我们实际构建了客户要求的东西吗?当然,验收测试应该在整个生命周期中进行,而不仅仅是在最后一个阶段,在这个阶段,你意识到客户想要一辆跑车,而你建造了一辆货车。

其他回答

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

功能测试——又名集成测试,测试系统中的一部分功能。这将测试许多方法,并可能与数据库或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.


总结一下,

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

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

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

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

单元测试: 单元测试特别用于逐个组件地测试产品,特别是在产品开发过程中。 Junit和Nunit类型的工具还将帮助您按照单元测试产品。 **与其在整合之后解决问题,不如在开发早期就解决问题。

功能测试: 就测试而言,主要有两种类型的测试 1.功能测试 2.非功能性测试。

Non-Functional Test is a test where a Tester will test that The product will perform all those quality attributes that customer doesn't mention but those quality attributes should be there. Like:-Performance,Usability,Security,Load,Stress etc. but in the Functional Test:- The customer is already present with his requirements and those are properly documented,The testers task is to Cross check that whether the Application Functionality is performing according to the Proposed System or not. For that purpose Tester should test for the Implemented functionality with the proposed System.

我认为它是这样的:单元测试确定代码执行了您希望代码执行的操作(例如,您想要添加参数A和b,实际上是将它们相加,而不是减去它们),功能测试测试所有代码一起工作以获得正确的结果,因此您希望代码执行的操作实际上在系统中获得了正确的结果。