带有Visual Studio 2010的CATCH单元测试套件

CATCH Unit Testing Suite with Visual Studio 2010

本文关键字:CATCH 单元测试 套件 2010 Visual Studio 带有      更新时间:2023-10-16

我了解,我可以简单地将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预处理器指令中。