私有方法的单元测试 [英] Unit testing of private methods

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

问题描述

我正在编写一些单元测试。
特别是我想测试一些私有方法。

I am in the process of writing some unit tests. In particular I want to test some private methods.

到目前为止我已经想出了使用。

So far the I have come up with using.

#define private public

非常满意这一点,因为它将从单元测试的角度破坏所有的封装。

But I am not happy with this as it will destroy all encapsulation from the point of view of the unit test.

你使用什么方法对私有方法进行单元测试。

What methods do you use to unit-test private methods.

推荐答案

如果方法复杂到足以保证隔离测试,那么将它们重构到自己的类中,并通过它们的公共接口s)。然后在原始类中私下使用它们。

If the methods are complex enough to warrant testing in isolation, then refactor them into their own class(es) and test via their public interface(s). Then use them privately in the original class.

这篇关于私有方法的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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