带有Visual Studio 2010的CATCH单元测试套件
CATCH Unit Testing Suite with Visual Studio 2010
我了解,我可以简单地将catch.hpp
放入我的项目目录中,将其添加到项目中,然后开始编写测试。一旦我尝试使用本教程中概述的一些宏,我就遇到了一些(确切地说是 11 个(链接器错误。
#include "catch.hpp"
unsigned int Factorial( unsigned int number ) {
return number <= 1 ? number : Factorial(number-1)*number;
}
TEST_CASE( "Factorials are computed", "[factorial]" ) {
REQUIRE( Factorial(1) == 1 );
REQUIRE( Factorial(2) == 2 );
REQUIRE( Factorial(3) == 6 );
REQUIRE( Factorial(10) == 3628800 );
}
我能够从第一个示例中创建一个文件并使用 g++ 编译它,但是当我只是将相同的代码粘贴到我的 VS2010 项目中然后尝试运行它时,我遇到了链接器错误。不幸的是,我现在对C++不够熟悉,无法自己解码错误,在查看了这里的单个文件后,我无法辨别问题。
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Catch::AutoReg::AutoReg(void (__cdecl*)(void),struct Catch::SourceLineInfo const &,struct Catch::NameAndDesc const &)" (??0AutoReg@Catch@@QAE@P6AXXZABUSourceLineInfo@1@ABUNameAndDesc@1@@Z) referenced in function "void __cdecl `anonymous namespace'::`dynamic initializer for 'autoRegistrar27''(void)" (??__EautoRegistrar27@?A0x3911e331@@YAXXZ) C:UsersMarkdocumentsvisual studio 2010ProjectsFirstCPPApplicationFirstCPPApplicationFirstCPPApplication.obj 1 1 FirstCPPApplication
Error 2 error LNK2019: unresolved external symbol "public: __thiscall Catch::ExpressionResultBuilder::ExpressionResultBuilder(enum Catch::ResultWas::OfType)" (??0ExpressionResultBuilder@Catch@@QAE@W4OfType@ResultWas@1@@Z) referenced in function __catch$?____C_A_T_C_H____T_E_S_T____27@@YAXXZ$0 C:UsersMarkdocumentsvisual studio 2010ProjectsFirstCPPApplicationFirstCPPApplicationFirstCPPApplication.obj 1 1 FirstCPPApplication
Error 3 error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl Catch::translateActiveException(void)" (?translateActiveException@Catch@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function __catch$?____C_A_T_C_H____T_E_S_T____27@@YAXXZ$0 C:UsersMarkdocumentsvisual studio 2010ProjectsFirstCPPApplicationFirstCPPApplicationFirstCPPApplication.obj 1 1 FirstCPPApplication
Error 4 error LNK2019: unresolved external symbol "public: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionResultBuilder::endExpression(enum Catch::ResultDisposition::Flags)" (?endExpression@ExpressionResultBuilder@Catch@@QAEAAV12@W4Flags@ResultDisposition@2@@Z) referenced in function "void __cdecl ____C_A_T_C_H____T_E_S_T____27(void)" (?____C_A_T_C_H____T_E_S_T____27@@YAXXZ) C:UsersMarkdocumentsvisual studio 2010ProjectsFirstCPPApplicationFirstCPPApplicationFirstCPPApplication.obj 1 1 FirstCPPApplication
Error 5 error LNK2019: unresolved external symbol "public: __thiscall Catch::AssertionInfo::AssertionInfo(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,struct Catch::SourceLineInfo const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,enum Catch::ResultDisposition::Flags)" (??0AssertionInfo@Catch@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABUSourceLineInfo@1@0W4Flags@ResultDisposition@1@@Z) referenced in function "void __cdecl ____C_A_T_C_H____T_E_S_T____27(void)" (?____C_A_T_C_H____T_E_S_T____27@@YAXXZ) C:UsersMarkdocumentsvisual studio 2010ProjectsFirstCPPApplicationFirstCPPApplicationFirstCPPApplication.obj 1 1 FirstCPPApplication
Error 6 error LNK2019: unresolved external symbol "struct Catch::IContext & __cdecl Catch::getCurrentContext(void)" (?getCurrentContext@Catch@@YAAAUIContext@1@XZ) referenced in function "struct Catch::IResultCapture & __cdecl Catch::getResultCapture(void)" (?getResultCapture@Catch@@YAAAUIResultCapture@1@XZ) C:UsersMarkdocumentsvisual studio 2010ProjectsFirstCPPApplicationFirstCPPApplicationFirstCPPApplication.obj 1 1 FirstCPPApplication
Error 7 error LNK2019: unresolved external symbol "public: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionResultBuilder::setOp(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setOp@ExpressionResultBuilder@Catch@@QAEAAV12@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "private: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionLhs<int const &>::captureExpression<0,int>(int const &)" (??$captureExpression@$0A@H@?$ExpressionLhs@ABH@Catch@@AAEAAVExpressionResultBuilder@1@ABH@Z) C:UsersMarkdocumentsvisual studio 2010ProjectsFirstCPPApplicationFirstCPPApplicationFirstCPPApplication.obj 1 1 FirstCPPApplication
Error 8 error LNK2019: unresolved external symbol "public: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionResultBuilder::setRhs(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setRhs@ExpressionResultBuilder@Catch@@QAEAAV12@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "private: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionLhs<int const &>::captureExpression<0,int>(int const &)" (??$captureExpression@$0A@H@?$ExpressionLhs@ABH@Catch@@AAEAAVExpressionResultBuilder@1@ABH@Z) C:UsersMarkdocumentsvisual studio 2010ProjectsFirstCPPApplicationFirstCPPApplicationFirstCPPApplication.obj 1 1 FirstCPPApplication
Error 9 error LNK2019: unresolved external symbol "public: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionResultBuilder::setLhs(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?setLhs@ExpressionResultBuilder@Catch@@QAEAAV12@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "private: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionLhs<int const &>::captureExpression<0,int>(int const &)" (??$captureExpression@$0A@H@?$ExpressionLhs@ABH@Catch@@AAEAAVExpressionResultBuilder@1@ABH@Z) C:UsersMarkdocumentsvisual studio 2010ProjectsFirstCPPApplicationFirstCPPApplicationFirstCPPApplication.obj 1 1 FirstCPPApplication
Error 10 error LNK2019: unresolved external symbol "public: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionResultBuilder::setResultType(bool)" (?setResultType@ExpressionResultBuilder@Catch@@QAEAAV12@_N@Z) referenced in function "private: class Catch::ExpressionResultBuilder & __thiscall Catch::ExpressionLhs<int const &>::captureExpression<0,int>(int const &)" (??$captureExpression@$0A@H@?$ExpressionLhs@ABH@Catch@@AAEAAVExpressionResultBuilder@1@ABH@Z) C:UsersMarkdocumentsvisual studio 2010ProjectsFirstCPPApplicationFirstCPPApplicationFirstCPPApplication.obj 1 1 FirstCPPApplication
Error 11 error LNK2019: unresolved external symbol "public: __thiscall Catch::AutoReg::~AutoReg(void)" (??1AutoReg@Catch@@QAE@XZ) referenced in function "void __cdecl `anonymous namespace'::`dynamic atexit destructor for 'autoRegistrar27''(void)" (??__FautoRegistrar27@?A0x3911e331@@YAXXZ) C:UsersMarkdocumentsvisual studio 2010ProjectsFirstCPPApplicationFirstCPPApplicationFirstCPPApplication.obj 1 1 FirstCPPApplication
Error 12 error LNK1120: 11 unresolved externals C:UsersMarkdocumentsvisual studio 2010ProjectsFirstCPPApplicationDebugFirstCPPApplication.exe FirstCPPApplication
长话短说:将单个项目解决方案拆分为 3 个,使用旧exe
.lib
,将项目添加到适当的路径,更新链接器,就可以开始了。
AE Drew的回答帮助解决了我的问题,但我想放一个更完整的问题标题的解决方案,以防有人来自谷歌,特别是如果他们正在寻找初学者的简单细分透视。
如果解决方案中只有一个项目为你构建了.exe
,则 CATCH行不通:您的解决方案需要 3 个项目(据我所知(才能测试您的代码。
-
Source_project
,主代码库项目(其中的源代码为您的应用程序驻留( -
Test_project
,测试套件项目(其中出现REQUIRES
测试如上述问题所述( -
Main_project
,你实际调用并运行代码的项目源代码库。
为了将原始的单项目解决方案转换为您可以在两个测试中使用,并且要正确运行,您需要构建一个.lib
或一个.dll
,以便其他项目(包括其他人的项目(可以使用相同的代码。改变这个答案,只需要
在"解决方案资源管理器"中右键单击你的项目,然后选择"> 性能。。。。在"配置属性"-"常规">下,有一个>选项 称为配置类型。如果将其更改为 [静态库> (.lib(],则 项目将在构建时生成一个 [
.lib
]。
然后在第三个项目中,main_project
,您有一个准系统设置来自仅包含看起来有内容的文件的 Win32 控制台应用程序喜欢:
#include "stdafx.h"
#include "HEADER_FROM_FIRST_PROJECT.h"
int _tmain(int argc, _TCHAR* argv[])
{
METHOD_FROM_FIRST_PROJECT();
return 0;
}
从Source_project
导入头文件的地方,我们的第一个项目创建(曾经是exe
的那个,但现在正在构建为lib
(。但这还没有运行,您需要将Source_project
的目录添加到这将投影其他目录,方法是右键单击"解决方案资源管理器"下的"属性>配置属性"> C/C++> 常规>其他包含目录,将source_project
的文件夹添加到列表中。这将允许#include
行自动在该目录中搜索相应的文件,使您不必编写../source_project/HEADER_FROM_FIRST_PROJECT.h
.
,最后一步是确保链接器能够实际找到您尝试包含的对象。由于我们正在构建一个 .lib
,我们可以相当简单地做到这一点。在与 相同的设置对话框中在"项目属性"对话框中,我们将导航到"配置"链接器>属性>输入>其他依赖项,并将相对路径添加到 source_project.lib
文件,看起来像../Debug/source_project.lib
.
请注意,如果您有旧exe
项目中的任何.dll
,它将需要移入新main_project
.
另请注意,这对我来说都是非常新的,某些术语和信息必然是不正确的。
只需浏览您提供的链接,我认为您可能需要在文件顶部添加此行。
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
看起来您尝试链接的函数#if defined CATCH_CONFIG_MAIN .... #endif
预处理器指令中。
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- VC++本机单元测试,找不到调试符号
- 用于交叉编译和CMake的预处理器宏的单元测试
- C++ 用于单元测试的模板模板
- 提升 1.64 单元测试编译失败
- 单元测试欧拉到四元数实现失败
- 运行 C++ 单元测试时LNK2005链接错误
- 禁用自动捕获 Googletest 单元测试中的C++异常
- 有没有办法在不使用 #ifdef 的情况下不编译发布版本中的单元测试函数体?
- 使用 Google Test 对自定义断言函数进行单元测试
- 如何将我的 CMake 项目配置为运行所有单元测试?
- 在Qt C++单元测试中动态加载QQuickWindow而不是QQuickWidget
- MS 本机单元测试 - 断言::线程失败不起作用
- CATCH 单元测试C++比较 std::array
- "Catch"单元测试框架 - REQUIRE_THROWS_AS
- 带有Visual Studio 2010的CATCH单元测试套件
- 使用CATCH C++单元测试框架测试两个std::向量是否相等
- 将头文件导入catch c++单元测试框架
- catch单元测试框架是否支持junit风格的测试报告