visualstudio 2013 或 visualstudio 2010
(1)首先打开VS2013,选择工具-扩展和更新,搜索并安装Unit Test Generator
(2)给现有工程(即:待测项目)添加一个编译配置: unit_test(从Debug复制),并添加预定义:TEST_
(3)将unit_test配置中的应用程序类型改为:DLL,注:如果程序本身已经是DLL,请忽略该步骤。
(4)在unit_test配置中,修改头文件包含目录及库目录,以增加对测试的支持Include Directories$(VCInstallDir)UnitTest\include;$(IncludePath)Library Directories$(VCInstallDir)UnitTest\lib;$(LibraryPath)
(5)给现有工程添加一个专门编写测试类的cpp:复制以下代码。 ######################################################################################include 'stdafx.h'#ifdef TEST_ // 必须加入此项,否则运行Debug或Release时会出现“缺少 .net 的动态库”的提示 #include 'CppUnitTest.h' #include 'MyProjectUnderTest.h' // 此处按需修改 using namespace Microsoft::VisualStudio::CppUnitTestFramework; namespace MyTest { TEST_CLASS(MyTests) { public: TEST_METHOD(MyTestMethod) { Assert::AreEqual(MyProject::Multiply(2,3), 6); // 此处按需要修改 } TEST_METHOD(MyTestMethod_2) { Assert::AreEqual(MyProject::Multiply(2,3), 6); // 此处按需要修改 } // … }; }#endif // 此处必须加 #####################################################################################
(6)选择unit_test配置,点击菜单中的“测试”-》“运行全部”,注意:Debug和Release下无效。
(7)可在源代码中加入断点,并右击鼠标进行“调试测试”