使用Catch C ++进行单元测试会干扰我的main()

时间:2017-08-31 20:54:28

标签: c++ unit-testing catch-unit-test

我是单元测试的新手,并决定使用Catch框架进行c ++,因为它似乎很容易与其一个头文件集成。但是,我有一个多文件二进制搜索树程序(文件是:main.cpp,Tree.h,Tree.hxx,TreeUnitTests.cpp,catch.hpp)。如果我在main.cpp中注释掉我的int main()函数,我只能运行单元测试。我知道它与我的TreeUnitTests.cpp中的'#define CATCH_CONFIG_MAIN'声明冲突,但如果我不包含该声明,我无法运行单元测试。每次我想运行单元测试时,如何在不必评论main()的情况下运行它们?

这是我正在使用的头文件: https://raw.githubusercontent.com/philsquared/Catch/master/single_include/catch.hpp

我发现它的Catch教程并用作指南: https://github.com/philsquared/Catch/blob/master/docs/tutorial.md

一些相关文件供参考: 的 main.cpp中:

//******************* ASSN 01 QUESTION 02 **********************

#include "Tree.h"
#include <iostream>

using namespace std;

/*
int main()
{

    //creating tree with "5" as root
    Tree<int> tree(5);
    tree.insert(2);
    tree.insert(88);
    tree.inorder();
    cout << "does tree contain 2?: ";
    cout << tree.find(2) << endl;
    cout << "does tree contain 3?: ";
    cout << tree.find(3) << endl;

    Tree<int> copytree(tree);
    cout << "copied original tree..." << endl;
    copytree.preorder();
    cout << "after deletion of 2:\n";
    copytree.Delete(2);
    copytree.postorder();


    return 0;
}
*/

TreeUnitTests.cpp:

#include <iostream>
#include "Tree.h"
#define CATCH_CONFIG_MAIN
#include "catch.hpp"

TEST_CASE("Pass Tests")
{
    REQUIRE(1 == 1);
}

TEST_CASE("Fail test")
{
    REQUIRE(1 == 0);
}

(我的测试不是真正的测试,只是为了验证Catch框架是否正常工作。我想你可以说这是一个元测试)

2 个答案:

答案 0 :(得分:4)

当您遗漏CATCH_CONFIG_RUNNER时,Catch会为您实施main。对于大多数测试来说它已经足够好了,但是如果你需要更多控制,那么你需要告诉它使用你的主要和它在引导程序Catch中。

使用这样简单的东西:

<强>的main.cpp

#define CATCH_CONFIG_RUNNER // Configure catch to use your main, and not its own.
#include <catch.hpp>
#include <iostream>
#include <exception>

int main(int argCount, char** ppArgs)
{
    try {
        // bootstrap Catch, running all TEST_CASE sequences.
        auto result = Catch::Session().run(argCount, ppArgs);
        std::cin.get(); // Immediate feedback.
        return (result < 0xFF ? result : 0xFF);
    } catch (const std::exception& ex) {
        auto pMessage = ex.what();
        if (pMessage) {
            std::cout << "An unhandled exception was thrown:\n" << pMessage;
        }
        std::cin.get(); // Immediate feedback.
        return -1;
    }
}

<强> tests.cpp

#include <catch.hpp>

TEST_CASE("Pass Tests")
{
    REQUIRE(1 == 1);
}

TEST_CASE("Fail test")
{
    REQUIRE(1 == 0);
}

这或多或少都是我在生产代码中使用的,而且效果很好。

答案 1 :(得分:4)

由于您使用的是Visual Studio,因此正确的方法是使用Configuration Manager(可通过右键单击Solution Explorer工具窗口中的解决方案访问)并创建单独的解决方案配置。

在“新建解决方案配置”表单中,为配置指定有意义的名称(例如UnitTesting)。还有一个名为“Copy from:”的下拉列表,您可以在其中选择将设置复制到新配置的配置。不要将其保留在<Empty>,而是选择一些您用于构建源的源配置(因为它将正确设置包含文件夹和其他设置)。通过选中“创建新项目配置”复选框,确保还为解决方案中的所有项目创建匹配的项目配置。

创建配置后,使用工具栏下拉列表以与在Debug和Release配置之间切换相同的方式选择它:

Click to select your new config 点击以选择新的UnitTesting配置

现在,当您打开某个项目的属性页或该项目中的文件时(右键单击文件或项目,选择Properties),您可以选择特定配置(UnitTesting in您的情况),并指定仅对此单一配置有效的某些选项。

您还可以在属性页中选择All Configurations,以便将设置应用于所有配置。添加其他包含目录和常见预处理器设置时,这很重要。如果您不小心将一些include目录添加到Debug配置中,则在切换到UnitTesting后,编译器将无法找到头文件。

因此,要使此配置的行为不同,您可以执行以下操作:

1。从main.cpp构建配置

中排除UnitTesting

例如,您可以右键单击main.cpp,打开Properties,并仅在此配置中从构建中排除文件:

Excluding a file from build 从构建中排除特定配置的文件

2。使用预处理器宏有条件地排除main()函数

或者,您可以打开项目属性并设置一个特定的预处理器宏,该宏仅在此配置中定义:

enter image description here UNIT_TESTING配置创建UnitTesting宏,MS Paint再次用于为图形添加天赋

因此,对于后一种方法,您的实际main.cpp将更改为:

// remove if UnitTesting configuration is active
#ifndef UNIT_TESTING
int main(void)
{
    ...
}
#endif

第一种方法很简洁,因为它根本不需要你改变生产代码,但后一种方法对于在Visual Studio中查看代码的其他毫无戒心的人来说可能更明显,当他们开始想知道为什么某些代码时根本没有编译。我有时会忘记构建配置中缺少某个文件,然后盯着奇怪的编译错误一段时间。

而且,使用第二种方法,您还可以在同一位置轻松提供自定义Catch入​​口点:

#ifdef UNIT_TESTING

    // this is the main() used for unit testing

#   define CATCH_CONFIG_RUNNER
#   include <catch.hpp>

    int main(void)
    {
        // custom unit testing code
    }    

#else

    int main(void)
    {
        // actual application entry point
    }

#endif
相关问题