对DLL中未导出的类进行单元测试 [英] Unit testing non-exported classes in a DLL

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

问题描述

我们使用Visual Studio 2008开发C ++应用程序,并使用Boost.Test进行单元测试.目前,我们有一个单独的解决方案,其中包含我们的单元测试.

We develop a C++ application using Visual Studio 2008 and unit test using Boost.Test. At the moment, we have a separate solution which contains our unit tests.

我们在核心解决方案中的许多项目都产生了DLL.测试范围有限,因为我们无法测试未导出的类.

Many of our projects in the core solution produce DLL's. We're limited in test coverage because we cannot test non-exported classes.

关于如何进行测试,我有两个想法:

I have two ideas on how these could be tested:

  1. 导出所有内容
  2. 将测试放入DLL(相同的项目和解决方案)中,并使用Boost.Test的外部运行器

我不太确定缺点会是什么.上面的数字1破坏了模块级别的封装,而数字2可能导致更大的DLL,除非可以在某些配置中仅包含测试代码.

I'm not entirely sure what the drawbacks would be. Number 1 above breaks module level encapsulation, and number 2 could result in a much larger DLL, unless it's possible to only include the test code in certain configurations.

因此,上述方法是否存在严重缺陷,或者您能想到其他解决方案吗?

So, are there any severe drawbacks to the above methods, or can you think of other solutions?

推荐答案

Tom Quarendon对这个问题的回答使用了西蒙·斯蒂尔(Simon Steele)的回应的一个小变化:

Expanding on Tom Quarendon's answer to this question, I have used a slight variant of Simon Steele's response:

  • 创建一个测试项目(使用任何喜欢的测试框架,我都使用 CppUnit ).
  • 在您的test_case.cpp中,#include <header/in/source/project.h>.
  • 在测试项目中的属性:
    • 在链接器->常规"中,将源项目的$(IntDir)添加到其他库目录"中.
    • 在链接器"->输入"中,将.obj文件添加到其他依赖项"中.
    • Create a test project (using whatever test framework you like, I use CppUnit).
    • In your test_case.cpp, #include <header/in/source/project.h>.
    • In the test project properties:
      • In Linker->General, add the source project's $(IntDir) to the Additional Library Directories.
      • In Linker->Input, add the .obj files to the Additional Dependencies.

      同样,唯一的维护开销是用于单元测试的标准开销-创建对要测试的单元的依赖性.

      Again, the only maintenance overhead is the standard one for unit tests - to create the dependency on the unit(s) you want to test.

      这篇关于对DLL中未导出的类进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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