双重测试有哪些不同类型及其用途? [英] What are different types of test doubles and their uses?

查看:69
本文介绍了双重测试有哪些不同类型及其用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关测试驱动开发的在线课程,并且遇到了测试倍增的概念.按照课程中对double的定义:

I was going through an online course on test driven development and came across the concept of test doubles. As per the definition of test double in the course :

测试双打:测试双打是在单元测试中用于替换实际生产系统协作者的对象.

Test Doubles : Test doubles are objects that are used in unit tests as replacement to the real production system collaborators.

我知道测试加倍意味着什么.但是后来有人提到,有多种类型的双打考试.在课程中提到的是:

I got an idea what test doubles mean. But then it was mentioned there are various types of test doubles. The ones mentioned in the course were :

虚拟:可以根据需要传递的对象,但是没有任何类型的测试实现,因此永远不要使用.

Dummy : Objects that can be passed around as necessary but do not have any type of test implementation and should never be used.

伪造:这些对象通常具有特定接口的简化功能实现,足以进行测试但不用于生产.

Fake : These objects generally have a simplified functional implementation of a particular interface that is adequate for testing but not for production.

存根:这些对象为实施提供适合测试的罐装答案.

Stub : These objects provide implementation with canned answers that are suitable for the tests.

间谍:这些对象提供了实现,该实现记录了传入的值,以便测试可以使用它们.

Spies : These objects provide implementation that record the values that were passed in so they can be used by the tests.

模拟:这些对象已预先编程为期望特定的调用和参数,并且在必要时可以引发异常.

Mocks : These objects are pre-programmed to expect specific calls and parameters and can throw exceptions when necessary.

我以前曾经使用过模拟,并且对它们是什么以及如何使用它们有一个简短的了解.尽管我对其他提到的双打类型感到困惑.

I have worked with mocks before and do have a brief idea of what they are and how to use them. Though I was confused regarding the other mentioned types of test doubles.

有人可以帮我解决这类双打考试和何时使用一次的区别吗?

Can someone help me with difference between these types of test doubles and when to use one ?

推荐答案

有用的文献,

  • 来自马丁·福勒(Martin Fowler):

    Dummy有点奇怪,因为您使用的是虚拟对象,因为被测试的代码实际上并未使用该虚拟对象.换句话说,系统的组成要求您提供一些多余的元素.通常,这暗示着您正在测试的逻辑应该可以通过更特定的界面访问.

    Dummy is sort of an odd case, in that you are using a dummy because the code under test doesn't actually use the dummy; in other words, the composition of the system is requiring you to supply some superfluous elements. Often, this is a hint that the logic you are testing should be accessible via a more specific interface.

    当测试方案要求被测代码从某种依赖关系中获取值时,通常会使用存根和伪造品.

    Stubs and Fakes are typically used when a test scenario requires that the code under test obtains values from some dependency.

    存根非常集中;它不会假装在内部做正确的事情,而只是返回一些固定答案.例如,您可以使用存根来模拟特定的故障模式.

    A stub is very focused; it doesn't pretend to do the right thing internally, but instead just returns some canned answer. For instance, you might use a stub to mimic a specific failure mode.

    Fake的名称很不正确-它是依赖项的真正实现,只是为测试(小型,确定性的,内存中的)而优化的,而不是针对生产(规模)的优化而实现的.在某些圈子中,您会听到替代,而不是伪造".

    Fake is badly named - it's a real implementation of the dependency, just one that is optimized for testing (small, deterministic, in memory) rather than optimized for production (scale). In some circles, you'll hear substitute, rather than "fake".

    当您试图确定代码将正确的信息发送给依赖项时,通常会使用间谍和模拟,而无需花费任何费用来耦合到该依赖项.

    Spies and mocks are typically used when you are trying to determine that your code sends the right information to a dependency, without the costs of being coupled to that dependency.

    间谍通常会保留他们在测试期间收到的邮件的历史记录,以后可以根据预期的邮件列表进行验证.嘲弄是这种想法的一种更积极的表达,即在场景运行时而不是在事后对被测代码的行为进行断言.

    Spies normally maintain a history of messages that they receive during testing, which can later be verified against an expected message list. Mocks are a more active expression of that idea, where assertions are made about the behavior of the code under test while the scenario is running, rather than afterwards.

    这篇关于双重测试有哪些不同类型及其用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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