2

有没有人使用 MSTest 来测试 MFC 代码?

我创建了一个托管 C++ 测试项目,并且可以在其中编写单元测试,但是一旦我#include <afxwin.h>遇到问题。代码将编译,当它开始运行测试时,UI 挂起并且不会加载任何符号。您也无法调试测试。如果我删除#include,那么测试将成功运行。有任何想法吗?

它使用带有多线程调试 (/mtd) 的 /clr 进行编译,并通过共享 dll 使用 MFC。如果我选择静态链接到 MFC,那么编译器 (VSVC9.0) 会告诉我 /clr 和 /mtd 不兼容。

另外,您认为在测试中添加 DependencyInput 会解决这个问题吗?我尝试添加一些 MFC dll 作为依赖项输入,但没有帮助。我可能做错了。

谢谢。

4

2 回答 2

1

在工作中,我们使用visual studio 2010,成功创建了一个托管的c++测试项目来测试mfc代码。

我们开始使用向导创建一个 c++ 测试项目。然后,在单元测试项目的配置属性中,更改以下内容:

  • 常规->使用 MFC = 更改为“在共享 DLL 中使用 MFC”
  • General->Common language runtime support = 改为“Common language support (/clr)
  • (仅在调试配置中)链接器->输出->忽略特定默认库 = 添加 MSVCRT

在 stdafx.h 中:我有以下内容

// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER              // Allow use of features specific to Windows XP or later.
#define WINVER 0x0501       // Change this to the appropriate value to target other versions of Windows.
#endif

#ifndef _WIN32_WINNT        // Allow use of features specific to Windows XP or later.                   
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
#endif                      

#ifndef _WIN32_IE           // Allow use of features specific to IE 6.0 or later.
#define _WIN32_IE 0x0600    // Change this to the appropriate value to target other versions of IE.
#endif

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions

#ifndef _AFX_NO_OLE_SUPPORT
#include <afxole.h>         // MFC OLE classes
#include <afxodlgs.h>       // MFC OLE dialog classes
#include <afxdisp.h>        // MFC Automation classes
#endif // _AFX_NO_OLE_SUPPORT

#ifndef _AFX_NO_DB_SUPPORT
#include <afxdb.h>          // MFC ODBC database classes
#endif // _AFX_NO_DB_SUPPORT

#include <afxdtctl.h>       // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>         // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

一切都像魅力一样!请让我知道这对你有没有用

于 2011-10-17T13:44:07.350 回答
0

在命令行中是否相同?

没有这样做。但是可以创建一个静态窗口吗?使用进程资源管理器检查 msest 进程。

于 2009-05-01T23:25:36.160 回答