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

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

问题描述

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

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).编写它们是为了测试规范的一部分,例如以 2 的参数调用函数 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天全站免登陆