单元测试c ++.如何测试私人会员? [英] Unit testing c++. How to test private members?

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

问题描述

我想对我的C ++应用程序进行单元测试.

I would like to make unit tests for my C++ application.

测试班级私人成员的正确形式是什么?交一个朋友类来测试私人成员,使用派生类还是其他技巧?

What is the correct form to test private members of a class? Make a friend class which will test the private members, use a derived class, or some other trick?

测试API使用哪种技术?

Which technique does the testing APIs use?

推荐答案

通常,仅测试问题注释中讨论的公共接口.

Typically, one only tests the public interface as discussed in the question's comments.

但是,有时候测试私有或受保护的方法会有所帮助.例如,该实现可能具有一些对用户隐藏的非同寻常的复杂性,并且可以通过访问非公共成员来进行更精确的测试.通常,最好找出一种方法来消除这种复杂性,或者找出如何公开展示相关部分的方法,但并非总是如此.

There are times however when it is helpful to test private or protected methods. For example, the implementation may have some non-trivial complexities that are hidden from users and that can be tested more precisely with access to non-public members. Often it's better to figure out a way to remove that complexity or figure out how to expose the relevant portions publicly, but not always.

允许单元测试访问非公开成员的一种方法是通过 friend 构建.

One way to allow unit tests access to non-public members is via the friend construct.

这篇关于单元测试c ++.如何测试私人会员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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