用Visual Studio Team System Test Editon测试C++的问题,急,等。

joylnwang 2009-04-14 02:52:36
用Test Edition创建一个测试项目之后,用来测试同一个解决方案内的另一个项目内的类。我已经导入要测试的函数所在的头文件,该类的实现在同名的.cpp文件内。在测试项目中运行测试,报错“未解析的外部符号 error lnk2028”。也就是说,虽然导入了头文件,但相应的实现,测试类并不能访问。在同一项目(被测试项目)内建立main函数测试所编写的类,一切OK,说明代码编写没问题。微软的文档在演示测试人员版本的时候用的是C#,只要在想测试的方法大括号内右键点击,就会出现添加单元测试的选项,可是在Visual C++ 2008内并没有此选项,只得自行建立测试项目。不知道究竟是差在了哪里。
...全文
371 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ga6840 2011-08-17
  • 打赏
  • 举报
回复
学习了
jichuanchun 2009-10-19
  • 打赏
  • 举报
回复
十分好文章,tks
wshcdr 2009-09-02
  • 打赏
  • 举报
回复
MK
joylnwang 2009-04-14
  • 打赏
  • 举报
回复
终于在微软官方论坛搜到答案,该问题可以终了矣。现将原文摘录如下:

My project is built on Visual C++, however the version of Visual Studio im using is 2008.
I have created a Testing Project for my VC++ project.
That creates properly, however when i choose to add a Unit Test Via the Unit Test Wizard, it gives me the error "InvalidMetadataException - Unknown virtual address 0" when i try to expand the list of classes in my VC++ Project,
Anybody know why this is happening?
Also, anybody got any tutorials or sites that will tell me how to create test methods for VC++?


Hi,
As far as I know, the unit test net framework built in VS 2008 depends on assembly metadata. Thus, if your Visual C++ project has no common language support, when attempting to create a unit test project on it, the unit test framework triggers “InvalidMetadataException”, which indicates the unit test framework can’t get the metadata. To solve this issue, please bring up the project properties and change the General Setting for CLR type to be '/clr', this will allow native code to be mixed into the project.
For detail information of C++ Unit Test projects, you can refer to
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1104293&SiteID=1
For detail information of unit test, you can refer to
http://msdn2.microsoft.com/en-us/library/ms182515(vs.80).aspx
Hope this helps!


It all works now, however when i try to expand the class it shows me this message:
No classes or namespaces in this assembly.
And no methods/modules within that class shows up.


Hi,
It is because there is no managed class defined in your program.
For a native C++ class in C++/CLI, it is represented as a struct value type in the assembly’s metadata, and the members of it are not included in the metadata (You can use IL Dasm (ildasm.exe) tool to see the assembly metadata). Thus, the unit test framework ignores native C++ classes, but tries to load managed classes.
For a test, you can add a managed class to your program and try again. For example,
Code Snippet
public ref class Test
{
public:
System::Void FuncTest()
{
}
};
Hope this helps!


红色的是某人的提问,绿色的是一位强人的回答。某人已经做了我所做过的所有尝试,但是在新建单元测试的时候显示InvalidMetadataException - Unknown virtual address 0,创建单元测试失败。根据强人的解释,微软的测试者版本,支持的是托管代码,本地代码必须支持CLR,所以那个哥们将编译器的CLR选项打开,但是重新建立单元测试文件的时候,系统识别不出文件中的类和方法。根据强人的解释,本地C++代码在CLR Metadata里面是用类似结构的东西来表示的,开发环境无法识别这样的元素,如果要识别这些元素,需要以托管代码(Managed)C++的书写方式编写程序,这时才可以识别项目中的相应元素,建立单元测试。当然了,光是这样还不可以,第一个链接讲解了要使用测试者版本测试C++本地非托管代码的具体设置方法,比较繁琐,而且还只能测试静态库(static library)。反正说白了,就是C++本地非托管代码基本没法用测试者版本里内建的单元测试功能来进行驱动开发测试,即使想测,也要费相当大的周章。看来,VC下面的本地非托管C++开发,要想搞测试驱动,还是乖乖的用CPPUnit为上策。所以人家微软在测试者版本的文档里面基本没有提到C++,原来是这个原因。

gao125210 2009-04-14
  • 打赏
  • 举报
回复
up
bo_simao 2009-04-14
  • 打赏
  • 举报
回复
我自己再顶起来,难道这么个小问题就真的没有大侠能够搞定么。Visual Studio的测试人员版本难道就是为C#做的么。
  • 打赏
  • 举报
回复
不清楚,帮顶
mengde007 2009-04-14
  • 打赏
  • 举报
回复
VS呢?
jackyjkchen 2009-04-14
  • 打赏
  • 举报
回复
我一向用team system的代码分析,但不用测试功能
joylnwang 2009-04-14
  • 打赏
  • 举报
回复
补充一点,原则上源代码和测试代码应该分在两个不同的Project之内,以免测试代码污染源代码。可是我找到的例子不管是Cppunit(包括其官方的例子money,他的Cppunit源码和测试代码好像是分在个不同的Project内,但是我也不知道是怎么分的)还是其他C++的单元测试工具,都是把测试代码和源代码混在一个项目里,我这里只是指示例代码,真正的工程代码我没有见过。而且查阅资料以后感觉两个项目之间互相调用类也是一件蛮复杂的事情,涉及很多问题,所以我到现在也不知道怎么把源代码和测试代码分散在两个不同的Project之内。希望真正的C++ TDD高手明示。
「已注销」 2009-04-14
  • 打赏
  • 举报
回复
不用VC,只用CB。
帮顶了

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧