使单元测试成为测试类的朋友有什么问题? [英] What is wrong with making a unit test a friend of the class it is testing?

查看:168
本文介绍了使单元测试成为测试类的朋友有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++中;我经常让单元测试类成为我测试类的朋友。我这样做是因为我有时觉得需要为一个私有方法编写一个单元测试,或者也许我想访问一些私有成员,所以我可以更容易地设置对象的状态,所以我可以测试它。对我来说,这有助于坚持封装和抽象,因为我不修改类的公共或保护的接口。

In c++; I have often made a unit test class a friend of the class I am testing. I do this because I sometimes feel the need to write a unit test for a private method, or maybe I want access to some private member so I can more easly setup the state of the object so I can test it. To me this helps perserve encapsulation and abstraction because I am not modifying the public or protected interface of the class.

如果我购买第三方库,我不想它的公共接口被一堆公共方法污染,我不需要知道简单地因为供应商想要单元测试!

If I buy a third party library, I wouldn't want it's public interface to be polluted with a bunch of public methods I don't need to know about simply because the vendor wanted to unit test!

也不需要担心一群受保护的成员,我不需要知道我是否从一个类继承。

Nor do I want have to worry about a bunch of protected members that I don't need to know about if I am inheriting from a class.

这就是为什么我说它保留抽象和封装。

That is why I say it preserves abstraction and encapsulation.

在我的新工作,他们皱眉反对使用朋友类甚至用于单元测试。他们说,因为类不应该知道任何关于测试,并且你不希望紧密耦合的类和它的测试。

At my new job they frown against using friend classes even for unit tests. They say because the class should not "know" anything about the tests and that you do not want tight coupling of the class and its test.

有人可以向我解释这些理由,以便我能更好地理解吗?我只是不明白为什么使用单元测试的朋友是坏的。

Can someone please explain these reasons to me more so that I may understand better? I just do not see why using a friend for unit tests is bad.

推荐答案

理想情况下,你不需要单元测试私有方法。你的类的所有消费者都应该关心的是公共接口,所以这是你应该测试。如果一个私有方法有一个bug,它应该被一个单元测试捕获,调用一些公共方法的类,最终最终调用buggy私有方法。如果一个bug设法逃脱,这表明你的测试用例不能完全反映你希望你的类实现的合同。这个问题的解决方案几乎肯定是用更仔细的测试公共方法,而不是让你的测试用例挖掘类的实现细节。

Ideally, you shouldn't need to unit test private methods at all. All a consumer of your class should care about is the public interface, so that's what you should test. If a private method has a bug, it should be caught by a unit test that invokes some public method on the class which eventually ends up calling the buggy private method. If a bug manages to slip by, this indicates that your test cases don't fully reflect the contract you wish your class to implement. The solution to this problem is almost certainly to test public methods with more scrutiny, not to have your test cases dig into the class's implementation details.

再次,这是理想的案件。在现实世界中,事情可能不总是那么清楚,并且有单元测试类作为类的朋友,它测试可能是可接受的,甚至是可取的。但是,它可能不是你想做的所有的时间。如果它看起来经常足够,这可能是一个迹象,你的类太大和/或执行太多的任务。如果是这样,通过将复杂的私有方法集合重构为单独的类来进一步细分它们应该有助于消除对单元测试知道实现细节的需要。

Again, this is the ideal case. In the real world, things may not always be so clear, and having a unit testing class be a friend of the class it tests might be acceptable, or even desirable. Still, it's probably not something you want to do all the time. If it seems to come up often enough, that might a sign that your classes are too large and/or performing too many tasks. If so, further subdividing them by refactoring complex sets of private methods into separate classes should help remove the need for unit tests to know about implementation details.

这篇关于使单元测试成为测试类的朋友有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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