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


当前回答

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

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


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

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.


总结一下,

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

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

其他回答

单元测试: 单元测试特别用于逐个组件地测试产品,特别是在产品开发过程中。 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.

根据ISTQB的说法,这两者没有可比性。功能测试不是集成测试。

单元测试是测试级别的一种,功能测试是测试的一种。

基本上:

一个系统(或组件)的功能是“它做什么”。这是 通常在需求规范中描述的是功能性的 规范,或者用例。

组件测试,也称为单元、模块和程序测试, 查找软件的缺陷,并验证软件的功能 (例如模块、程序、对象、类等),它们是分开的 可测试的。

根据ISTQB,组件/单元测试可以是功能性或非功能性:

组件测试可能包括功能测试和特定的非功能特征,如资源行为(例如内存泄漏)、性能或健壮性测试,以及结构测试(例如决策覆盖率)。

引用自软件测试基础- ISTQB认证

AFAIK,单元测试不是功能测试。让我用一个小例子来解释。您希望测试电子邮件web应用程序的登录功能是否正常工作,就像用户一样。为此,您的功能测试应该如下所示。

1- existing email, wrong password -> login page should show error "wrong password"!
2- non-existing email, any password -> login page should show error "no such email".
3- existing email, right password -> user should be taken to his inbox page.
4- no @symbol in email, right password -> login page should say "errors in form, please fix them!" 

我们的功能测试是否应该检查我们是否可以使用无效输入登录?如。电子邮件没有@符号,用户名有多个点(只有一个点是允许的),.com出现在@之前等等?一般来说,没有!这种测试会进入单元测试。

您可以检查单元测试中是否拒绝了无效输入,如下面的测试所示。

class LoginInputsValidator
  method validate_inputs_values(email, password)
    1-If email is not like string.string@myapp.com, then throw error.
    2-If email contains abusive words, then throw error.
    3-If password is less than 10 chars, throw error.

注意,功能测试4实际上做的是单元测试1所做的。有时,由于不同的原因,功能测试可能会重复单元测试所完成的部分(而不是全部)测试。在我们的示例中,我们使用功能测试4来检查在输入无效输入时是否出现特定的错误消息。我们不想测试是否所有坏的输入都被拒绝了。这就是单元测试的工作。

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

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

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


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


单元测试应该是这样的:当我调用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.


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