单元测试和功能测试之间的区别是什么?单元测试也可以测试函数吗?
当前回答
单元测试测试一个独立的行为单元。行为的单位是什么?它是系统中可以独立进行单元测试的最小部分。(这个定义实际上是循环的,它根本不是一个定义,但在实践中似乎很管用,因为你可以直观地理解它。) 功能测试测试一个独立的功能部分。
行为单元非常小:虽然我绝对不喜欢这种愚蠢的“每个方法测试一个单元”的咒语,但从大小的角度来看,它是正确的。行为单元介于方法的一部分和几个方法之间。最多是一个对象,但不能超过一个。 一个功能块通常包含许多方法,并跨越多个对象,通常跨越多个体系结构层。
单元测试应该是这样的:当我调用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.
在前一种情况下,功能测试还可以作为验收测试,并作为功能需求或功能规范的可执行编码,在后一种情况下,它们还可以作为集成测试。 单元测试经常更改,而功能测试在主要版本中不应该更改。
其他回答
单元测试测试一个独立的行为单元。行为的单位是什么?它是系统中可以独立进行单元测试的最小部分。(这个定义实际上是循环的,它根本不是一个定义,但在实践中似乎很管用,因为你可以直观地理解它。) 功能测试测试一个独立的功能部分。
行为单元非常小:虽然我绝对不喜欢这种愚蠢的“每个方法测试一个单元”的咒语,但从大小的角度来看,它是正确的。行为单元介于方法的一部分和几个方法之间。最多是一个对象,但不能超过一个。 一个功能块通常包含许多方法,并跨越多个对象,通常跨越多个体系结构层。
单元测试应该是这样的:当我调用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.
在前一种情况下,功能测试还可以作为验收测试,并作为功能需求或功能规范的可执行编码,在后一种情况下,它们还可以作为集成测试。 单元测试经常更改,而功能测试在主要版本中不应该更改。
我认为它是这样的:单元测试确定代码执行了您希望代码执行的操作(例如,您想要添加参数A和b,实际上是将它们相加,而不是减去它们),功能测试测试所有代码一起工作以获得正确的结果,因此您希望代码执行的操作实际上在系统中获得了正确的结果。
“功能测试”并不意味着测试代码中的函数(方法)。一般来说,这意味着您正在测试系统功能——当我在命令行上运行foo file.txt时,file.txt中的行可能会颠倒过来。相反,单个单元测试通常包含单个方法的单个情况——length(“hello”)应该返回5,length(“hi”)应该返回2。
另请参阅IBM关于单元测试和功能测试之间界限的论述。
单元测试通常由开发人员完成。这样做的目的是确保他们的代码正常工作。一般的经验法则是使用单元测试覆盖代码中的所有路径。
功能测试:这是一个很好的参考。功能测试说明
单元测试告诉开发人员代码的工作是正确的;功能测试告诉开发人员代码正在做正确的事情。
你可以在单元测试与功能测试中阅读更多内容
单元测试和功能测试在现实生活中的类比可以描述如下:
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.
总结一下,
单元测试是从程序员的角度编写的。它们用于确保类的特定方法(或单元)执行一组特定的任务。
功能测试是从用户的角度编写的。它们确保系统按照用户的期望运行。
推荐文章
- 为什么Visual Studio 2015/2017/2019测试运行器没有发现我的xUnit v2测试
- 单元测试反模式目录
- ReferenceError: description没有定义NodeJs
- 类未找到:IntelliJ中的空测试套件
- Mockito的argumentCaptor的例子
- 单元测试:日期时间。现在
- 为什么单元测试中的代码不能找到包资源?
- 从导入的模块中模拟函数
- 在单元测试中设置HttpContext.Current.Session
- 何时使用Mockito.verify()?
- 在PHP单元测试执行期间,如何在CLI中输出?
- 单元测试的一些常用命名约定是什么?
- 如何检查动态附加的事件监听器是否存在?
- 强制重新测试或禁用测试缓存
- 如何直接从测试驱动程序调用自定义的Django manage.py命令?