单元,功能,验收和集成测试之间有什么区别? [英] What's the difference between unit, functional, acceptance, and integration tests?

查看:173
本文介绍了单元,功能,验收和集成测试之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单元测试,功能测试,验收测试和集成测试(以及我没有提到的任何其他类型的测试)之间有什么区别?

What is the difference between unit, functional, acceptance, and integration testing (and any other types of tests that I failed to mention)?

推荐答案

根据您所处的位置,您会得到略有不同的答案.我已经阅读了很多有关该主题的文章,这是我的精读.再次,这些有点毛茸茸,其他人可能会不同意.

Depending on where you look, you'll get slightly different answers. I've read about the subject a lot, and here's my distillation; again, these are slightly wooly and others may disagree.

单元测试

测试最小的功能单元,通常是方法/功能(例如,给定具有特定状态的类,在该类上调用x方法应导致y发生).单元测试应集中于一项特定功能(例如,当堆栈为空时调用pop方法应抛出InvalidOperationException).它涉及的所有内容都应该在内存中完成;这意味着测试代码不应:

Tests the smallest unit of functionality, typically a method/function (e.g. given a class with a particular state, calling x method on the class should cause y to happen). Unit tests should be focussed on one particular feature (e.g., calling the pop method when the stack is empty should throw an InvalidOperationException). Everything it touches should be done in memory; this means that the test code and the code under test shouldn't:

  • 召集(平凡的)合作者
  • 访问网络
  • 打数据库
  • 使用文件系统
  • 启动线程

应该使用适当的技术对任何缓慢/难以理解/初始化/操纵的依赖项进行打桩/模拟/处理,以使您可以专注于代码单元在做什么,而不是代码在做什么.

Any kind of dependency that is slow / hard to understand / initialise / manipulate should be stubbed/mocked/whatevered using the appropriate techniques so you can focus on what the unit of code is doing, not what its dependencies do.

简而言之,单元测试尽可能简单,易于调试,可靠(由于减少了外部因素),执行速度快,并有助于证明程序的最小构造块在投入使用之前便达到了预期的功能一起.需要注意的是,尽管您可以证明它们在隔离状态下可以完美地工作,但是合并后的代码单元可能会破裂,这将使我们陷入困境.

In short, unit tests are as simple as possible, easy to debug, reliable (due to reduced external factors), fast to execute and help to prove that the smallest building blocks of your program function as intended before they're put together. The caveat is that, although you can prove they work perfectly in isolation, the units of code may blow up when combined which brings us to ...

集成测试

集成测试通过组合代码单元并测试所产生的组合能否正常运行,在单元测试的基础上进行构建.这可以是一个系统的内部功能,也可以是将多个系统组合在一起以执行有用的操作.同样,将集成测试与单元测试区分开的另一件事是环境.集成测试可以并且将使用线程,访问数据库或执行所有必要的操作,以确保所有代码都可以正常工作.

Integration tests build on unit tests by combining the units of code and testing that the resulting combination functions correctly. This can be either the innards of one system, or combining multiple systems together to do something useful. Also, another thing that differentiates integration tests from unit tests is the environment. Integration tests can and will use threads, access the database or do whatever is required to ensure that all of the code and the different environment changes will work correctly.

如果您构建了一些序列化代码并在不接触磁盘的情况下对其内部进行了单元测试,那么您如何知道它在加载并保存到磁盘后会起作用?也许您忘记了刷新和处理文件流.也许您的文件权限不正确,并且您已经测试了在内存流中使用的内部函数.唯一可以确定的方法是使用最接近生产环境的真实"测试.

If you've built some serialization code and unit tested its innards without touching the disk, how do you know that it'll work when you are loading and saving to disk? Maybe you forgot to flush and dispose filestreams. Maybe your file permissions are incorrect and you've tested the innards using in memory streams. The only way to find out for sure is to test it 'for real' using an environment that is closest to production.

主要优点是,他们将发现单元测试无法解决的错误,例如接线错误(例如,类A的实例意外接收到B的空实例)和环境错误(在单CPU机器上运行良好) ,但我同事的4核计算机无法通过测试).主要缺点是集成测试涉及更多代码,可靠性更低,失败更难以诊断,测试也难以维护.

The main advantage is that they will find bugs that unit tests can't such as wiring bugs (e.g. an instance of class A unexpectedly receives a null instance of B) and environment bugs (it runs fine on my single-CPU machine, but my colleague's 4 core machine can't pass the tests). The main disadvantage is that integration tests touch more code, are less reliable, failures are harder to diagnose and the tests are harder to maintain.

此外,集成测试并不一定证明完整的功能有效.用户可能不在乎我的程序的内部细节,但是我知道!

Also, integration tests don't necessarily prove that a complete feature works. The user may not care about the internal details of my programs, but I do!

功能测试

功能测试通过将给定输入的结果与规范进行比较来检查特定功能的正确性.功能测试与中间结果或副作用无关,仅与结果有关(它们并不关心在执行x之后,对象y处于状态z).它们被编写来测试规范的一部分,例如调用参数Square2的函数Square(x)返回4".

Functional tests check a particular feature for correctness by comparing the results for a given input against the specification. Functional tests don't concern themselves with intermediate results or side-effects, just the result (they don't care that after doing x, object y has state z). They are written to test part of the specification such as, "calling function Square(x) with the argument of 2 returns 4".

验收测试

验收测试似乎分为两种:

Acceptance testing seems to be split into two types:

标准验收测试包括在整个系统上执行测试(例如,通过网络浏览器使用您的网页),以查看应用程序的功能是否满足规范.例如. 单击缩放图标应将文档视图放大25%."没有真正的结果连续性,只有通过或失败的结果.

Standard acceptance testing involves performing tests on the full system (e.g. using your web page via a web browser) to see whether the application's functionality satisfies the specification. E.g. "clicking a zoom icon should enlarge the document view by 25%." There is no real continuum of results, just a pass or fail outcome.

优势在于,测试以纯正的英语进行描述,并确保软件作为一个整体具有完整的功能.缺点是您已将测试金字塔上移了另一个级别.验收测试涉及大量的代码,因此跟踪失败可能很棘手.

The advantage is that the tests are described in plain English and ensures the software, as a whole, is feature complete. The disadvantage is that you've moved another level up the testing pyramid. Acceptance tests touch mountains of code, so tracking down a failure can be tricky.

此外,在敏捷软件开发中,用户接受测试涉及创建测试以反映开发过程中由软件客户创建的/为软件客户创建的用户故事.如果测试通过,则意味着软件应满足客户的要求,并且故事可以被认为是完整的.验收测试套件基本上是一种用领域特定语言编写的可执行规范,该规范以系统用户使用的语言描述测试.

Also, in agile software development, user acceptance testing involves creating tests to mirror the user stories created by/for the software's customer during development. If the tests pass, it means the software should meet the customer's requirements and the stories can be considered complete. An acceptance test suite is basically an executable specification written in a domain specific language that describes the tests in the language used by the users of the system.

结论

它们都是互补的.有时专注于一种类型或完全避开它们是有利的.对我而言,主要区别在于,某些测试从程序员的角度看待事物,而另一些则以客户/最终用户为关注重点.

They're all complementary. Sometimes it's advantageous to focus on one type or to eschew them entirely. The main difference for me is that some of the tests look at things from a programmer's perspective, whereas others use a customer/end user focus.

这篇关于单元,功能,验收和集成测试之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆