是否可以测试“内部"使用MSTest从C ++ dll中获取类? [英] Is it possible to test "internal" class from a c++ dll using MSTest?

查看:84
本文介绍了是否可以测试“内部"使用MSTest从C ++ dll中获取类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们当前正在尝试将单元测试添加到我们的c ++应用程序中.该应用程序由30个生成29 dll和1 exe的项目组成.我们使用MSTest来运行我们的单元测试,因为它已经包含在Visual Studio 2010中.

We are currently trying to add unit testing to our c++ application. The application is made of 30 projects that generate 29 dll and 1 exe. We use MSTest to run our unit test since it's already included in Visual Studio 2010.

它对于声明为"public"的类非常有用.这些课程的开头是这样的:

It works great for class that are declared "public". These class have this at the beginning:

#ifdef RESEAU_IMPL
    #define CLASS_DECL      _declspec(dllexport)
#else
    #define CLASS_DECL      _declspec(dllimport)
#endif 

但是对于所有其他类(代码的90%),它们并未声明为公共,因此我们无法在测试中使用它们.

But for all the other class (90% of the code), they are not declared public so we can't use them in our test.

我已经在Google上阅读了有关InternalVisibleTo属性的信息,但它似乎仅适用于c#.NET程序集.我对吗?我也读过声明我的课程为"as_friend",但不确定将其放置在何处.

I've read on google about the InternalVisibleTo attribute but it seems to be only working with c# .NET assembly. Am I right? I also read to declare my class "as_friend" but I'm not sure where to put this.

所以总之:我想测试DLL中未导出/未公开的类.我该怎么办?

So in brief: I want to test class that are not exported/public in the DLL. How do I do that?

谢谢

*编辑*

Gishu评论说,在非托管代码中不可能进行单元测试,但是可以做到.看,这是一个测试本机c ++代码的TestMethode. CVersion在C ++ MFC中.

Gishu commented that Unit Testing was not possible in unmanaged code but it is possible. See, this is a TestMethode that test native c++ code. CVersion is in C++ MFC.

[TestMethod]
void AssignationCVersion()
{
    CVersion version1234(1,2,3,4);
    CVersion version4321(4,3,2,1);
    Assert::IsTrue(version1234 != version4321);
    version1234 = version4321;
    Assert::IsTrue(version1234 == version4321);
};

但是似乎不可能使用特殊标记来测试内部功能.我第一个同意测试内部方法不是一种好习惯,但是这些DLL不是实用程序功能,而是真实"应用程序的一部分(也许这是错误的设计,但它是15年前完成的).有人对这个主题有想法吗?

But what seems to be impossible is to use special tag to test internal function.I'm the first to agree that testing internal method is not good practice but these DLL are not utility functions but are part of the "real" application (maybe it's bad design but it was done 15 years ago). Anyone has an idea on the subject?

推荐答案

无论您是单元测试框架还是其他工具,都无法测试看不到的代码. Windows上的DLL仅导出定义了__declspec(dllexport)的符号.编译DLL时,任何其他符号都将视为内部 ,使用DLL的代码将看不到任何其他符号.

There is no way, whether you're a unit testing framework or something else, to test code that you can't see. A DLL on Windows only exports symbols which have __declspec(dllexport) defined. Any other symbol is treated as internal when the DLL is compiled, and won't be visible to code using the DLL.

这很重要,因为这意味着链接器可以优化,修改或删除未导出的代码.您要测试的代码可能根本不存在.它可能在那里,但是形式与您预期的不同. DLL是根据合同进行编译的,该合同要求使用dllexport声明的所有内容都必须存在且可见,而其他所有内容都必须起作用.不必从外部访问它.

This is important because it means that the linker can optimize, modify or remove code that isn't exported. The code you want to test might not be there at all. It might be there, but in a different form than you expect. The DLL is compiled under a contract that anything declared with dllexport must be present and visible, and anything else just has to work. It doesn't have to be accessible from the outside world.

这不是MSTest的缺点(尽管它还有很多其他缺点,并且对于单元测试C ++代码而言是一个非常糟糕的选择)

That's not a shortcoming of MSTest (even though it has plenty of other shortcomings and is a pretty awful choice for unit testing C++ code)

如果要测试该代码,则有两个选择:

If you want to test that code, you have two options:

  • 使用dllexport导出,或
  • 将单元测试代码编写为dll本身的一部分.
  • export it with dllexport, or
  • write your unit test code as part of the dll itself.

这篇关于是否可以测试“内部"使用MSTest从C ++ dll中获取类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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