单元测试和功能测试有什么区别?

发布于 2024-08-31 00:32:12 字数 35 浏览 6 评论 0原文

单元测试和功能测试有什么区别?单元测试也可以测试功能吗?

What is the difference between unit tests and functional tests? Can a unit test also test a function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

扶醉桌前 2024-09-07 00:32:12

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

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


单元测试和功能测试的一个很好解释的现实生活类比可以描述如下,

很多时候,系统的开发就像盖房子一样。虽然这个类比不太正确,但我们可以对其进行扩展,以便理解单元测试和功能测试之间的区别。

单元测试类似于建筑检查员参观房屋的建筑工地。他专注于房屋的各种内部系统、地基、框架、电气、管道等。他确保(测试)房屋的各个部分能够正确且安全地工作,即符合建筑规范。

此场景中的功能测试类似于房主访问同一建筑工地。他假设内部系统将正常运行,建筑检查员正在执行他的任务。房主关注的是住在这所房子里会是什么样子。他关心房子的外观,各个房间的大小是否舒适,房子是否适合家人的需求,窗户是否处于捕捉早晨阳光的好位置。

房主正在对房屋进行功能测试。他有用户的视角。

建筑检查员正在对房屋进行单元测试。他具有建设者的视角。


总而言之,

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

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

Unit tests tell a developer that the code is doing things right; functional tests tell a developer that the code is doing the right things.

You can read more at Unit Testing versus Functional Testing


A well explained real-life analogy of unit testing and functional testing can be described as follows,

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.


As a summary,

Unit Tests are written from a programmers perspective. They are made to ensure that a particular method (or a unit) of a class performs a set of specific tasks.

Functional Tests are written from the user's perspective. They ensure that the system is functioning as users are expecting it to.

如此安好 2024-09-07 00:32:12

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

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

Unit Test - testing an individual unit, such as a method (function) in a class, with all dependencies mocked up.

Functional Test - AKA Integration Test, testing a slice of functionality in a system. This will test many methods and may interact with dependencies like Databases or Web Services.

冰魂雪魄 2024-09-07 00:32:12
  • 单元测试测试独立的行为单元。什么是行为单位?它是系统中可以独立进行单元测试的最小部分。 (这个定义实际上是循环的,实际上它根本不是一个定义,但它在实践中似乎工作得很好,因为你可以直观地理解它。)

  • 功能测试测试独立的功能。


  • 行为单元非常小:虽然我绝对不喜欢这种愚蠢的“每个方法一个单元测试”的口头禅,但从大小的角度来看,它是正确的。行为单元是介于方法的一部分和可能的几个方法之间的东西。最多一个对象,但不超过一个。

  • 一项功能通常包含许多方法,并且跨越多个对象,并且通常穿过多个架构层。


  • 单元测试类似于:当我调用 validate_country_code() 函数并向其传递国家/地区代码 'ZZ' 时,它应该返回 false< /code>.

  • 功能测试是:当我使用国家/地区代码 ZZ 填写运输表单时,我应该被重定向到帮助页面,该页面允许我从菜单中选择我的国家/地区代码.


  • 单元测试是由开发人员、为开发人员、从开发人员的角度编写的。

  • 功能测试可能是面向用户的,在这种情况下,它们是由开发人员与用户一起编写的(或者可能使用正确的工具和正确的用户,甚至是用户自己),从用户的角度为用户编写。或者它们可能是面向开发人员的(例如,当它们描述用户不关心的某些内部功能时),在这种情况下,它们是由开发人员编写的,为开发人员编写,但仍然是从用户的角度出发。


  • 一种情况下,功能测试也可以充当验收测试以及功能需求或功能规范的可执行编码,在后一种情况下,它们也可以充当集成测试。

  • 单元测试经常更改,功能测试不应在主要版本中更改。


  • A unit test tests an independent unit of behavior. What is a unit of behavior? It's the smallest piece of the system that can be independently unit tested. (This definition is actually circular, IOW it's really not a definition at all, but it seems to work quite well in practice, because you can sort-of understand it intuitively.)

  • A functional test tests an independent piece of functionality.


  • A unit of behavior is very small: while I absolutely dislike this stupid "one unit test per method" mantra, from a size perspective it is about right. A unit of behavior is something between a part of a method and maybe a couple of methods. At most an object, but not more than one.

  • A piece of functionality usually comprises many methods and cuts across several objects and often through multiple architectural layers.


  • A unit test would be something like: when I call the validate_country_code() function and pass it the country code 'ZZ' it should return false.

  • A functional test would be: when I fill out the shipping form with a country code of ZZ, I should be redirected to a help page which allows me to pick my country code out of a menu.


  • 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.


  • In the former case, the functional tests may also serve as acceptance tests and as an executable encoding of functional requirements or a functional specification, in the latter case, they may also serve as integration tests.

  • Unit tests change frequently, functional tests should never change within a major release.


裂开嘴轻声笑有多痛 2024-09-07 00:32:12

TLDR:

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


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

在此处输入图像描述

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

当功能组合在一起时,您创建一个模块=一个独立的部分,可能带有可以测试的用户界面(模块测试)。一旦你至少有两个独立的模块,然后将它们粘合在一起,然后出现:

(2) 集成测试:将两个或多个(子)模块或(子)系统放在一起,看看它们是否能够很好地协同工作。

然后,您集成第三个模块,然后按照您或您的团队认为合适的顺序集成第四个和第五个模块,一旦所有拼图块放置在一起,就可以了

(3)系统测试:对软件进行整体测试。这几乎是“所有部分的集成测试”。

如果可以的话,那就来了

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

在此处输入图像描述

TLDR:

To answer the question: Unit Testing is a subtype of Functional Testing.


There are two big groups: Functional and Non-Functional Testing. The best (non-exhaustive) illustration that I found is this one (source: www.inflectra.com):

enter image description here

(1) Unit Testing: testing of small snippets of code (functions/methods). It may be considered as (white-box) functional testing.

When functions are put together, you create a module = a standalone piece, possibly with a User Interface that can be tested (Module Testing). Once you have at least two separate modules, then you glue them together and then comes:

(2) Integration Testing: when you put two or more pieces of (sub)modules or (sub)systems together and see if they play nicely together.

Then you integrate the 3rd module, then the 4th and 5th in whatever order you or your team see fit, and once all the jigsaw pieces are placed together, comes

(3) System Testing: testing SW as a whole. This is pretty much "Integration testing of all pieces together".

If that's OK, then comes

(4) Acceptance Testing: did we build what the customer asked for actually? Of course, Acceptance Testing should be done throughout the lifecycle, not just at the last stage, where you realise that the customer wanted a sportscar and you built a van.

enter image description here

鸵鸟症 2024-09-07 00:32:12

“功能测试”并不意味着您正在测试代码中的功能(方法)。一般来说,这意味着您正在测试系统功能 - 当我在命令行运行 foo file.txt 时,file.txt 中的行可能会颠倒过来。相比之下,单个单元测试通常涵盖单个方法的单个情况 - length("hello") 应返回 5,而 length("hi") 应返回return 2.

另请参阅IBM 对单元测试和功能测试之间界限的看法< /a>.

"Functional test" does not mean you are testing a function (method) in your code. It means, generally, that you are testing system functionality -- when I run foo file.txt at the command line, the lines in file.txt become reversed, perhaps. In contrast, a single unit test generally covers a single case of a single method -- length("hello") should return 5, and length("hi") should return 2.

See also IBM's take on the line between unit testing and functional testing.

滿滿的愛 2024-09-07 00:32:12

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

单元测试是测试级别之一,功能测试是测试类型。

基本上:

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

尽管

组件测试,也称为单元、模块和程序测试,
搜索软件的缺陷并验证软件的功能
(例如模块、程序、对象、类等)
可测试。

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

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

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

According to ISTQB those two are not comparable. Functional testing is not integration testing.

Unit test is one of tests level and functional testing is type of testing.

Basically:

The function of a system (or component) is 'what it does'. This is
typically described in a requirements specification, a functional
specification, or in use cases.

while

Component testing, also known as unit, module and program testing,
searches for defects in, and verifies the functioning of software
(e.g. modules, programs, objects, classes, etc.) that are separately
testable.

According to ISTQB component/unit test can be functional or not-functional:

Component testing may include testing of functionality and specific non-functional characteristics such as resource-behavior (e.g. memory leaks), performance or robustness testing, as well as structural testing (e.g. decision coverage).

Quotes from Foundations of software testing - ISTQB certification

浅唱々樱花落 2024-09-07 00:32:12

在 Rails 中,单元文件夹用于保存模型的测试,功能文件夹用于保存控制器的测试,集成文件夹用于保存涉及任意数量控制器交互的测试。夹具是一种组织测试数据的方式;它们驻留在装置文件夹中。 test_helper.rb 文件保存测试的默认配置。
您可以访问

In Rails, the unit folder is meant to hold tests for your models, the functional folder is meant to hold tests for your controllers, and the integration folder is meant to hold tests that involve any number of controllers interacting. Fixtures are a way of organizing test data; they reside in the fixtures folder. The test_helper.rb file holds the default configuration for your tests.
u can visit this.

月下客 2024-09-07 00:32:12

我们可以非常简单地说:

  • 黑盒:像功能测试一样的用户界面测试
  • 白盒:像单元测试一样的代码测试

阅读更多 此处

very simply we can say:

  • black box: user interface test like functional test
  • white box: code test like unit test

read more here.

[浮城] 2024-09-07 00:32:12

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 [email protected], 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 来检查在输入无效输入时是否出现特定的错误消息。我们不想测试是否所有错误的输入都被拒绝。这就是单元测试的工作。

AFAIK, unit testing is NOT functional testing. Let me explain with a small example. You want to test if the login functionality of an email web app is working or not, just as a user would. For that, your functional tests should be like this.

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!" 

Should our functional tests check if we can login with invalid inputs ? Eg. Email has no @ symbol, username has more than one dot (only one dot is permitted), .com appears before @ etc. ? Generally, no ! That kind of testing goes into your unit tests.

You can check if invalid inputs are rejected inside unit tests as shown in the tests below.

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

Notice that the functional test 4 is actually doing what unit test 1 is doing. Sometimes, functional tests can repeat some (not all) of the testing done by unit tests, for different reasons. In our example, we use functional test 4 to check if a particular error message appears on entering invalid input. We don't want to test if all bad inputs are rejected or not. That is the job of unit tests.

烙印 2024-09-07 00:32:12

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

The way I think of it is like this: A unit test establishes that the code does what you intended the code to do (e.g. you wanted to add parameter a and b, you in fact add them, and don't subtract them), functional tests test that all of the code works together to get a correct result, so that what you intended the code to do in fact gets the right result in the system.

£噩梦荏苒 2024-09-07 00:32:12

单元测试

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

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

功能测试

这是一种黑盒测试,测试将在产品的功能方面进行,而无需查看代码。功能测试主要由专门的软件测试人员完成。它将包括使用非标准化数据测试产品指定功能的正向、负向和 BVA 技术。与单元测试相比,功能测试以改进的方式进行测试覆盖率。它使用应用程序 GUI 进行测试,因此更容易确定界面的特定部分到底负责什么,而不是确定代码负责什么。

UNIT TESTING

Unit testing includes testing of smallest unit of code which usually are functions or methods. Unit testing is mostly done by developer of unit/method/function, because they understand the core of a function. The main goal of the developer is to cover code by unit tests.

It has a limitation that some functions cannot be tested through unit tests. Even after the successful completion of all the unit tests; it does not guarantee correct operation of the product. The same function can be used in few parts of the system while the unit test was written only for one usage.

FUNCTIONAL TESTING

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.

超可爱的懒熊 2024-09-07 00:32:12

测试类型

  • 单元测试 - 在过程编程中单元是一个过程,在面向对象编程中单元是一个类。单元是独立的,反映了开发人员的观点
  • 功能测试 - 不仅仅是单元用户视角,描述功能、用例、故事......
    • 集成测试 - 检查所有单独开发的组件是否可以协同工作。它可以是其他应用程序、服务、图书馆、数据库、网络等。
      • 窄集成测试 - double[关于] 是用过的。主要目的是检查组件是否以正确的方式配置
      • 广泛集成测试(端到端测试、系统测试)- 实时版本。主要目的是检查所有组件是否以正确的方式配置
    • UI 测试 - 检查用户输入是否触发正确的操作以及在发生某些操作时 UI 是否发生更改
    • ...
  • 非功能测试 - 其他情况
    • 性能测试 - 计算速度和其他指标
    • 可用性测试 - 用户体验
    • ...

[iOS测试]
[Android 测试]

Test types

  • Unit testing - In Procedural programming unit is a procedure, in Object oriented programming unit is a class. Unit is isolated and reflects a developer perspective
  • Functional testing - more than Unit. User perspective, which describes a feature, use case, story...
    • Integration testing - check if all separately developed components work together. It can be other application, service, library, database, network etc.
      • Narrow integration test - double[About] is used. The main purpose is to check if component is configured in a right way
      • Broad integration test (End to End test, System test) - live version. The main purpose is to check if all components are configured in a right way
    • UI testing - checks if user input triggers a correct action and the UI is changed when some actions are happened
    • ...
  • Non functional testing - other cases
    • Performance testing - calculate a speed and other metrics
    • Usability testing - UX
    • ...

[iOS tests]
[Android tests]

一生独一 2024-09-07 00:32:12

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

功能测试:-
至于测试,有两种主要类型的测试:
1.功能测试
2.非功能测试。

非功能测试是一种测试,测试人员将测试产品将执行客户未提及但应该存在的所有质量属性。
例如:-性能、可用​​性、安全性、负载、压力等。
但在功能测试中:- 客户已经提出了他的要求,并且这些要求已正确记录,测试人员的任务是交叉检查应用程序功能是否根据建议的系统执行。
为此,测试人员应测试所提议系统的已实现功能。

Unit Test:-
Unit testing is particularly used to test the product component by component specially while the product is under development.
Junit and Nunit type of tools will also help you to test the product as per the Unit.
**Rather than solving the issues after the Integration it is always comfortable to get it resolved early in the development.

Functional Testing:-
As for as the Testing is concerned there are two main types of Testing as
1.Functional Test
2.Non-Functional Test.

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.

能怎样 2024-09-07 00:32:12

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

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

Unit testing is usually done by developers. The objective of doing the same is to make sure their code works properly. General rule of thumb is to cover all the paths in code using unit testing.

Functional Testing: This is a good reference. Functional Testing Explanation

薄荷→糖丶微凉 2024-09-07 00:32:12

让我们有一些对象(类、模块、系统等)。该对象有一些我们可以测试其正确性的函数(进行功能测试)。而且这个对象还具有一些非功能功能,例如 - 性能(进行不同类型的测试)。

例如,我们以计算器为例。它有一个功能——加法,从功能测试的角度来看,我们想检查这个功能的工作是否正确,即当你添加 2 + 2 时,结果是否有可能不是 4;这个函数如何在边界值下工作,等等。就“非功能”测试而言,我们可以测试它添加两个数字的速度有多快;使用起来有多容易,等等。

单元测试是使用模拟外部依赖项对某些单元进行测试。单元测试也是功能测试。

但是我们可以在没有模拟外部依赖的情况下进行单元的功能测试。在这种情况下,我们进行集成测试(而不是单元测试)。一般来说,我们可以有不同的测试来测试功能。

因此,任何单元测试都是功能测试,但并非所有功能测试都是单元测试。

Let us have some object (class, module, system, etc.). This object has some functions that we can test for correctness (do functional testing). And this object also has some non-functional features, for example - performance (do different types of tests).

For example, let's take a calculator. It has a function - addition, from the point of view of functional testing we want to check how correctly this function works, i.e. Isn’t it possible that when you add 2 + 2 the result is not 4; how does this function work at boundary values, etc. In terms of "non-functional" testing, we could test how quickly it adds two numbers; how easy it is to use, etc.

Unit test is test of some unit with mock external dependencies. Also unit test is functional test.

But we can do functional testing of unit without mock external dependencies. In the case we have integration test (not unit test). In general we can have different tests for testing of functions.

Thus, any unit test is a functional test, but not any functional test is a unit test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文