如果我可以只使用集成测试,我为什么要费心进行单元测试? [英] Why should I bother with unit testing if I can just use integration tests?

查看:32
本文介绍了如果我可以只使用集成测试,我为什么要费心进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道我要发表这样的声明,所以我的问题是让每个人都说服我我错了.以这个场景为例:

Ok, I know I am going out on a limb making a statement like that, so my question is for everyone to convince me I am wrong. Take this scenario:

我有方法A,它调用方法B,它们在不同的层中.

I have method A, which calls method B, and they are in different layers.

所以我对 B 进行了单元测试,结果为 null.所以我测试返回null,并且单元测试通过.不错.

So I unit test B, which delivers null as a result. So I test that null is returned, and the unit test passes. Nice.

然后我对 A 进行单元测试,它期望从 B 返回一个空字符串.所以我模拟了 B 层,返回一个空字符串,测试通过.又好.(假设我没有意识到 A 和 B 的关系,或者可能是两个不同的人正在构建这些方法)

Then I unit test A, which expects an empty string to be returned from B. So I mock the layer B is in, an empty string is return, the test passes. Nice again. (Assume I don't realize the relationship of A and B, or that maybe two differente people are building these methods)

我担心的是,直到我们一起测试 A 和 B,即集成测试,我们才会发现真正的问题.由于集成测试提供了对单元测试区域的覆盖,因此构建所有这些实际上并没有告诉我们任何(或非常)有意义的单元测试似乎是一种浪费.

My concern is that we don't find the real problem until we test A and B togther, i.e. Integration Testing. Since an integration test provides coverage over the unit test area, it seems like a waste of effort to build all these unit tests that really don't tell us anything (or very much) meaningful.

为什么我错了?

推荐答案

这里是 一篇关于测试分类的文章 带有一些论据

Here's an article on test categorization with some arguments

我不会提及整体测试的好处,一旦我们只是比较单元测试与功能测试:

I won't mention the benefits of testing as a whole, once we're just comparing unit tests vs. functional tests:

  • 单元测试可帮助您缩小出现错误时查看的范围. - 在此场景中,让我们包括类 C、D、E、...、Z.如果您只有集成测试并且失败了,您从哪里开始寻找?如果您没有单元测试,您需要查看每个类中的每一个以及布线"" 在这些之间(这是一个更窄的范围).如果你有单元测试,那么你只需要检查接线.在这种情况下,如果没有一些较小的集成测试,例如仅测试 A、B、C 和 D(因此您已经知道它们之间的接线"是否确实有效),也将是一件坏事.
  • 通过单元测试,您可以更快地失败.如果您使用 TDD,则更是如此.您可能在创建所有 A 类和 B 类之后编写测试.当您运行测试时,A 和 B 的工作方式在您的脑海中并不新鲜(也许您是在前一周编写的 - 希望您不要开始测试仅当产品完成"时).然后你必须记住当你写这些的时候你在想什么.此外,单元测试速度更快,因此您更有可能更频繁地运行它们(也许每次保存时都会自动运行它们?)
  • 单元测试提供了更好的文档说明您的类应该如何表现.如果您是普通程序员",您可能讨厌编写文档.这会强制您在编程时编写文档,并且强制文档永远不会过时(如果是,您的测试将失败).而且当您需要更改其他人的代码时,它也很有帮助.
  • Unit testing helps you reduce the scope where to look when there's an error. - Let's include classes C, D, E, ..., Z in this scenario. If you have only integration test and it fails, where do you start looking? If you don't have unit tests, you would need to look everywhere inside each of those classes AND the "wiring" between those (which is a narrower scope).If you had unit tests, then you'd just need to check the wiring. In this case, it would also be a bad thing not having some smaller integration tests, like testing A, B, C and D only (so you already know if the "wiring" between those are in fact working).
  • With unit tests, you fail faster. This is even more true if you TDD. You probably write your tests after you created all of class A and B. When you run your test, the way A and B works is not as fresh in your mind (maybe you wrote them the week before - hopefully you don't start testing only when the product is "finished"). You must then remember what you were thinking when you wrote those. Also, unit test are faster, so you're more likely to run them more frequently (perhaps run them automatically every time you save?)
  • Unit tests provide a better documentation how your class should behave. If you're a "normal programmer", you probably hate writing documentation. This forces you to write documentation while programming, and forces the documentation to never be obsolete (if it is, your tests fail). And it also helps when you need to change somebody else's code.

理想世界中,当测试失败时,您不需要超过 2 分钟就知道要查看什么(无需调试).进行各种规模的测试的想法只是实现这一目标的指导方针,而不是花费数小时/数天/数周进行调试 =).

In the ideal world, when a test fails, you won't need more than 2 minutes to know what to look (with no need of debugging). The idea of having tests of all sizes is just a guideline to achieve this goal, rather than spending hours/days/weeks debugging =).

这篇关于如果我可以只使用集成测试,我为什么要费心进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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