在集成测试中模拟是否被认为是一种好习惯? [英] Is it in considered a good practice to mock in integration test?

查看:64
本文介绍了在集成测试中模拟是否被认为是一种好习惯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我,@ Mock通常仅用于单元测试,但我认为对于代替测试类之外的外部零件很有用.在集成测试中模拟是否正确?

Somebody told me @Mock is usually employed only for unit tests but I think is useful for substituting the external parts outside the tested class. Is it correct to mock in integration tests?

推荐答案

最后,这全都与措辞有关.

In the end, this is all about wording.

当您从基本意义上说正确"时,例如正确性,那么答案很简单:不.

When you think of "correct" in its very fundamental sense, as in correctness, then the answer is simply: no.

您看到,集成测试的目标是确保您的集成系统(由多个不同组件组成)能够按预期运行.集成测试的目的是验证组件的连接"是否按预期进行.因此:当模拟出系统的 parts 时,您无法验证系统是否正常工作.

You see, the goal of an integration test is to ensure that your integrated system (consisting of multiple, different components) functions as expected. The purpose of an integration test is to verify that your "plumbing" of components works as expected. Therefore: you can't verify that your system works when parts of that system are mocked out.

但是,您可以不太严格地认为正确性".

But then, you can think "correctness" less strictly.

示例:销售汽车的公司必须测试 ECU .基本上是一块硬件,运行着可能庞大的软件堆栈.这些ECU通常在汽车中运行.因此,当您要对ECU进行集成测试时,必须将ECU放入汽车中进行测试,对吗?可能还不存在的汽车.这里的解决方案:有硬件仿真器.您将ECU插入该仿真器中,然后ECU就会思考"位于真实汽车内的汽车.

Example: companies selling cars have to test ECUs. Basically a piece of hardware, running a potentially huge software stack. These ECUs normally operate within cars. So when you want to integration test an ECU, you would have to put the ECU into a car for testing, right? A car that probably doesn't exist yet. The solution here: there are hardware emulators. You plug the ECU into that emulator, and the ECU "thinks" that sits inside a real car.

因此:有很好的论据声称真正的集成测试不能使用模拟",但是同时,在现实世界中,这种模拟"一直在发生.

So: there are good arguments to claim "a true integration test can't use mocking", but at the same time, in the real world, such "mocking" happens all the time.

因此,真正的答案是:它取决于上下文.因此,没有一个普遍的答案.相反,这是关于通信的.您仅"必须确保您的组/组织中的所有人对这些术语具有相同的理解.

The real answer is therefore: it depends on context. Therefore there isn't a universal answer. Instead, this is about communication. You "simply" have to ensure that all people in your group/org have the same understanding of such terms.

该术语本身可以用不同的方式解释.您(共同!)选择最适合您需求的定义,然后确保所有对您的项目做出贡献的人都共享该视图(或至少了解该视图).

The term itself can be interpreted in different ways. You (jointly!) pick that definition that best fits your needs, to then make sure all people contributing to your project share that view (or at least know about it).

这篇关于在集成测试中模拟是否被认为是一种好习惯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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