简体   繁体   English

集成与单元测试

[英]Integration vs Unit Testing

Im developing with Grails. 我正在用Grails开发。 Since the framework will bootstrap data and a fully flushed out spring context, I find that I write a lot of integration tests for services. 由于框架将引导数据和完全刷新的spring上下文,我发现我为服务编写了大量的集成测试。 Let me rephrase that: I find I write no unit tests for services, only integration tests. 让我重新说一下:我发现我没有为服务编写单元测试,只编写集成测试。 Is this a bad idea? 这是一个坏主意吗? The only downside I see to it is that my tests take a bit longer to run. 我看到的唯一缺点是我的测试需要更长的时间才能运行。

I do use unit testing on controllers, as in a controller I am testing the various application flows, result types, redirect logic, etc. But the majority of tests I write are integration tests. 我在控制器上使用单元测试,因为在控制器中我正在测试各种应用程序流,结果类型,重定向逻辑等。但我编写的大多数测试都是集成测试。 This seems a break from tradition J2EE tests, where mostly unit tests are written. 这似乎是传统J2EE测试的一个突破,其中大多数是单元测试。

edit- to be clear, Im not writing integration tests because the code is so complicated only integration tests will do. 编辑 - 要清楚,我没有编写集成测试,因为代码是如此复杂,只有集成测试才能做到。 Im writing integration tests because it is easier to test everything together, since the framework gives you so much. 我正在编写集成测试,因为它更容易一起测试所有内容,因为框架为您提供了很多。 I do mock certain things, like if a service collaborates with the acegi authenticationService, I mock that. 我会模拟某些事情,比如服务与acegi authenticationService合作,我嘲笑它。 I also mock anytime we interact with a webservice, because you have to in order to get a test that runs without special setup. 我也可以随时与网络服务进行交互,因为你必须为了在没有特殊设置的情况下运行测试。

I clearly see a trend towards more functional tests and fewer unit tests, in particular for heavily-connected components low on logic. 我清楚地看到了更多功能测试和更少单元测试的趋势,特别是对于逻辑低的高连接组件。 If I implement a complicated algorithm in a particular class, I will usually write unit tests for it, but if the complexity is due to integration with other components (which is very frequently the case), unit tests are not worth the hassle. 如果我在特定的类中实现一个复杂的算法,我通常会为它编写单元测试,但如果复杂性是由于与其他组件的集成(这种情况经常发生),单元测试就不值得麻烦了。

In general, the important measure is the code coverage. 一般来说,重要的衡量标准是代码覆盖率。

In can be advantageous to do integration tests if the global interface is more stable (less subject to change). 如果全局接口更稳定(更不易改变),那么进行集成测试可能是有利的。

You should test as soon as possible, because you want errors to show up as soon as possible, ideally while our code is still under your own control. 您应该尽快测试,因为您希望尽快显示错误,理想情况是我们的代码仍在您自己的控制之下。 The later an error is discovered the higher the costs of the correction will be. 发现的错误越晚,修正的成本就越高。

If you are exposing non trivial logic, you should test the logic of the main decision paths with unit tests, and the exposure of that logic and the interaction with external dependencies in the integration tests. 如果要暴露非平凡的逻辑,则应该使用单元测试来测试主决策路径的逻辑,以及在集成测试中暴露该逻辑以及与外部依赖关系的交互。

If you are a lone developer or working in a small team, sometimes it's difficult to see the distinction between unit and integration tests. 如果您是一个单独的开发人员或在一个小团队中工作,有时很难看到单元测试和集成测试之间的区别。 If you are in an environment where multiple teams are working on a single product, the assumption is that the logic inside your code has already been verified (unit test) and now you want to see how the modules play with each other (integration test). 如果您处于多个团队正在处理单个产品的环境中,则假设您的代码中的逻辑已经过验证(单元测试),现在您想要了解模块之间的相互作用(集成测试) 。

It is not a good pattern to load too much into the integration test, because you are deferring testing to a later phase in your project or development environment, and sooner or later you are going to find that you are discovering errors once the code has left your desk. 在集成测试中加载太多不是一个好的模式,因为您将测试推迟到项目或开发环境的后期阶段,并且迟早您会发现在代码离开后您发现错误你的桌子。 That will mean holding up the entire and possibly the product release while you fix something you could and should have discovered earlier. 这意味着在您修复可能并且应该在之前发现的内容的同时,保留整个产品和可能的产品版本。

You may be able to use a mock framework to isolate different parts of your services for individual testing. 您可以使用模拟框架来隔离服务的不同部分以进行单独测试。 Instead of relying on the massive Spring context, directly instantiate your service class and fill in the dependencies not directly relevant to the test with mocks. 而不是依赖于大量的Spring上下文,直接实例化您的服务类并使用模拟填充与测试不直接相关的依赖项。 You may need a little refactoring to decouple your dependencies, but it will usually be for the best. 您可能需要一些重构来解耦您的依赖关系,但它通常是最好的。 This can lead to writing more and smaller tests instead of relying on a few large integration tests. 这可以导致编写更多和更小的测试,而不是依赖于一些大型集成测试。

I am in a similar situation where many established tests are really integration tests, and it can be difficult, but not impossible, to change this going forward. 我处于类似的情况,其中许多已建立的测试实际上是集成测试,并且可能很难但不是不可能改变这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM