单元测试和集成测试之间处理的重复 [英] Dealing with duplication between unit and integration tests

查看:201
本文介绍了单元测试和集成测试之间处理的重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由许多类实现的算法中,所有涉及的单元测试。

I have an algorithm implemented by a number of classes, all covered by unit test.

我想重构它,这将改变两类行为。 当我改变一个类和它的测试,所有的单元测试通过,但该算法变得不正确,直到重构完成。

I would like to refactor it, which will change behavior of two classes. When I change one class and its tests, all unit tests pass, though the algorithm becomes incorrect until refactoring is done.

这个例子说明了完全覆盖单元测试有时是不够的,我需要在投入产出方面的一体化测试整个算法。理想情况下,这样的测试应该完全覆盖我的算法的行为。

This example illustrates that complete coverage by unit tests is sometimes not enough and I need "integration" tests for the whole algorithm in terms of input-output. Ideally, such tests should cover the behavior of my algorithm completely.

我的提问:貌似加入这样的集成测试我做单元测试不必要的,多余的。我不想重复支持测试逻辑。 我应该删除我的单元测试或保持这些设置,例如:更容易错误的位置?

My question: looks like by adding such integration tests I make unit tests unnecessary and superfluous. I don't want to support duplicated test logic. Should I remove my unit tests or leave them as is, e.g. for easier bug location?

推荐答案

这是问题与测试,这是过于细粒度,并紧密结合实现部分。

This is part of the problem with tests which are too fine grained and are tightly coupled with the implementation.

我个人编写测试是专注于算法的行为,并会考虑这个单位。事实上,它被分成几类是一个实现细节,以相同的方式,打破一个公共方法的功能集成到几个较小的私有方法也是一个实施细节。我不会写测试的私有方法分开,他们会通过公共方法的功能测试进行测试。

Personally I would write tests which focus on the behaviour of the algorithm and would consider this 'a unit'. The fact that it is broken into several classes is an implementation detail, in the same way that breaking down a public method's functionality into several smaller private methods is also an implementation detail. I wouldn't write tests for the private methods separately, they would be tested by the tests of the functionality of the public method.

如果其中的一些类是一般的,并且被其他地方重复使用,那么我会考虑写单元测试他们在这一点上的话,他们将有自己的一些规定的行为。

If some of those classes are generically useful and will be reused elsewhere then I would consider writing unit tests for them at that point as then they will have some defined behaviour on their own.

这将导致一些重复,但是这是确定的这些类现在有一个公共合同维护(和这是由两个组件它使用它),而这些测试可以定义。

This would result in some duplication but this is ok as those classes now have a public contract to uphold (and which is used by both components which use it), which those tests can define.

有趣的是,看到单位的定义,这篇文章

Interestingly, see the definition of Unit in this article

这篇关于单元测试和集成测试之间处理的重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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