单元测试和功能测试有什么区别?
单元测试和功能测试有什么区别?单元测试也可以测试功能吗?
What is the difference between unit tests and functional tests? Can a unit test also test a function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
您可以在 单元测试与功能测试中阅读更多内容测试
单元测试和功能测试的一个很好解释的现实生活类比可以描述如下,
总而言之,
单元测试是从程序员的角度编写的。它们的目的是确保类的特定方法(或单元)执行一组特定任务。
功能测试是从用户的角度编写的。他们确保系统按照用户的期望运行。
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,
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.
单元测试 - 测试单个单元,例如类中的方法(函数),并模拟所有依赖项。
功能测试 - 又称集成测试,测试系统中的一部分功能。这将测试许多方法,并可能与数据库或 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.
单元测试测试独立的行为单元。什么是行为单位?它是系统中可以独立进行单元测试的最小部分。 (这个定义实际上是循环的,实际上它根本不是一个定义,但它在实践中似乎工作得很好,因为你可以直观地理解它。)
功能测试测试独立的功能。
行为单元非常小:虽然我绝对不喜欢这种愚蠢的“每个方法一个单元测试”的口头禅,但从大小的角度来看,它是正确的。行为单元是介于方法的一部分和可能的几个方法之间的东西。最多一个对象,但不超过一个。
一项功能通常包含许多方法,并且跨越多个对象,并且通常穿过多个架构层。
单元测试类似于:当我调用
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 returnfalse
.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.
TLDR:
回答这个问题:单元测试是功能测试的子类型。
有两大类:功能测试和非功能测试。我发现的最好的(非详尽的)插图是这个(来源:www.inflectra.com):
当功能组合在一起时,您创建一个模块=一个独立的部分,可能带有可以测试的用户界面(模块测试)。一旦你至少有两个独立的模块,然后将它们粘合在一起,然后出现:
然后,您集成第三个模块,然后按照您或您的团队认为合适的顺序集成第四个和第五个模块,一旦所有拼图块放置在一起,就可以了
如果可以的话,那就来了
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):
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:
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
If that's OK, then comes
“功能测试”并不意味着您正在测试代码中的功能(方法)。一般来说,这意味着您正在测试系统功能 - 当我在命令行运行
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 infile.txt
become reversed, perhaps. In contrast, a single unit test generally covers a single case of a single method --length("hello")
should return 5, andlength("hi")
should return 2.See also IBM's take on the line between unit testing and functional testing.
根据 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:
while
According to ISTQB component/unit test can be functional or not-functional:
Quotes from Foundations of software testing - ISTQB certification
在 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.
我们可以非常简单地说:
阅读更多 此处。
very simply we can say:
read more here.
AFAIK,单元测试不是功能测试。让我用一个小例子来解释一下。您想要测试电子邮件 Web 应用程序的登录功能是否正常工作,就像用户一样。为此,您的功能测试应该是这样的。
我们的功能测试是否应该检查我们是否可以使用无效输入登录?例如。电子邮件没有@符号,用户名有多个点(只允许一个点),.com出现在@之前等等?一般来说,不会!这种测试会进入你的单元测试中。
您可以检查单元测试中是否拒绝无效输入,如下面的测试所示。
请注意,功能测试 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.
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.
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.
我的想法是这样的:单元测试确定代码执行了您希望代码执行的操作(例如,您想要添加参数 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.
单元测试
单元测试包括对最小代码单元的测试,通常是函数或方法。单元测试主要由单元/方法/函数的开发人员完成,因为他们了解函数的核心。开发人员的主要目标是通过单元测试覆盖代码。
它有一个限制,即某些功能无法通过单元测试进行测试。即使在成功完成所有单元测试之后;它不保证产品的正确操作。相同的功能可以在系统的少数部分中使用,而单元测试仅为一种用途而编写。
功能测试
这是一种黑盒测试,测试将在产品的功能方面进行,而无需查看代码。功能测试主要由专门的软件测试人员完成。它将包括使用非标准化数据测试产品指定功能的正向、负向和 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.
测试类型
单元测试
- 在过程编程
中单元是一个过程,在面向对象编程
中单元是一个类。单元是独立的,反映了开发人员的观点功能测试
- 不仅仅是单元
。 用户视角,描述功能、用例、故事......集成测试
- 检查所有单独开发的组件
是否可以协同工作。它可以是其他应用程序、服务、图书馆、数据库、网络等。窄集成测试
- double[关于] 是用过的。主要目的是检查组件是否以正确的方式配置广泛集成测试
(端到端测试、系统测试)- 实时版本。主要目的是检查所有组件是否以正确的方式配置UI 测试
- 检查用户输入是否触发正确的操作以及在发生某些操作时 UI 是否发生更改非功能测试
- 其他情况性能测试
- 计算速度和其他指标可用性测试
- 用户体验[iOS测试]
[Android 测试]
Test types
Unit testing
- InProcedural programming
unit is a procedure, inObject oriented programming
unit is a class. Unit is isolated and reflects a developer perspectiveFunctional testing
- more thanUnit
. User perspective, which describes a feature, use case, story...Integration testing
- check if all separately developedcomponents
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 wayBroad integration test
(End to End test, System test) - live version. The main purpose is to check if all components are configured in a right wayUI testing
- checks if user input triggers a correct action and the UI is changed when some actions are happenedNon functional testing
- other casesPerformance testing
- calculate a speed and other metricsUsability testing
- UX[iOS tests]
[Android tests]
单元测试:-
单元测试特别用于在产品开发过程中逐个组件地测试产品。
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.
单元测试通常由开发人员完成。这样做的目的是确保他们的代码正常工作。一般经验法则是使用单元测试覆盖代码中的所有路径。
功能测试:这是一个很好的参考。 功能测试说明
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
让我们有一些对象(类、模块、系统等)。该对象有一些我们可以测试其正确性的
函数
(进行功能测试)。而且这个对象还具有一些非功能
功能,例如 - 性能(进行不同类型的测试)。例如,我们以计算器为例。它有一个功能——加法,从功能测试的角度来看,我们想检查这个功能的工作是否正确,即当你添加 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 somenon-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.