eclipse - C++ Boost 测试、包结构和 Eclipse 项目设置

标签 eclipse packages main c++98 boost-test

我使用的是 C++98,除了标准库之外,我只能访问旧版本的 Boost(谢天谢地有 Boost Test)。文档虽然令人生畏,冗长,我只是不知道从哪里开始。

我有一些在 Java 中进行单元测试的经验(我正在寻找 C++ 中的单元测试),并且我见过 test包含独立于 src 的单元测试代码的包包,我也看到了Where do you put your unit test?以及 Unit Testing with Boost and Eclipse .他们的建议各不相同,并提出了将测试代码与生产代码分开或将它们放在一起的不同封装结构的推理。

在我开始研究 Boost Test 之前,我在 Eclipse 中创建了这个结构(可能是错误的):

-- ProjectName
   |-- Debug
   |-- src
   |-- test

我写了另一个运行测试功能的主要方法。 Eclipse 不喜欢这样,因为我在同一个项目中有两个主要方法。我在项目属性中摸索,并没有发现任何有用的东西可以在构建时将我的生产代码与测试代码分开(实际上是链接)。我的临时解决方法是只使用 g++在终端和临时编译我的“测试”代码。

我在 Boost::Test -- generation of Main()? 上发现了一些建议Boost 实际上生成了自己的 main 方法,所以这是我目前针对单元测试的攻击计划,特别是对于已经有一个可用的测试工具库。

  • What is the conventional way of organizing unit tests for C++?
  • How do I get started with Boost Test? (Boost is already installed)
  • Is there anything I need to change in Eclipse to be able to run my Boost unit tests separate from my production code within the IDE? (One of the nice things about IntelliJ, with Java, is how it'll automatically run any main method you like with a click) -- The goal here to be able to build and run my tests within Eclipse.
  • Should my tests be in a separate Eclipse project? (this was suggested in an answer to the second SO question I linked)


编辑 : 我找到了this Boost Test 简介的文章,但没有讨论如何在 IDE 设置中处理它。

最佳答案

我自己想出了如何做到这一点,我将为其他刚开始使用 C++ 并需要对其代码进行测试的其他人记录我的解决方案。目前在我能找到的任何地方都没有好的介绍。以下是我使用的资源(并且发现很有用):

  • C++ Unit Testing With Boost Test它以比 Boost 的文档更好的方式引入了 Boost Test。
  • Where do you put your unit test?其中讨论了使用 C++ 进行测试的最传统方法。
  • unit test in eclipse g++其中讨论了允许测试的 Eclipse 构建配置

  • 用于测试的 C++ 约定与其他编码语言的约定类似,只需将测试写入名为 test 的目录中即可。项目下。使用 Boost Test 需要链接单元测试框架:-l boost_unit_test_framework在 Eclipse 中:

    Right click on your project, go to Properties, C/C++ Build, Settings, Tool Settings, GCC C++ Linker, Libraries, and add the library name boost_unit_test_framework (add -mt to the name if you require multithreading; additionally, once the testing build configuration exists, you can go back and choose just that configuration to link the library -- it'll reduce the size of your executable for your other builds).



    为了能够在 Eclipse 中独立于您的 main 方法运行单元测试,我们需要建立一个新的构建配置。这样,Eclipse 就知道在您执行测试时使用 main 方法排除您的源文件。

    Click on Project, Build Configurations, Manage..., and select New... and call it Test (or something other than test). Choose your existing configuration so that we'll inherit properties from the production build.



    接下来,我们需要区分构建配置,以便在构建它们时,它们实际上对应于生产和测试构建。

    Right click on test, Resource Configurations, Exclude from Build..., and select the builds that represent your production build (i.e. Debug and or Release). Once done with that, right click on your source file with your main method, and exclude that from the Test build.



    还有一些事情我们需要改变。我们还没有任何测试代码,但我们仍然无法运行我们的测试构建,我们的测试构建也不知道 src 中存在的资源。因为 Eclipse 不会自动包含这些源文件。它们对您在 test 中的测试代码几乎是不可见的.

    Right click on your project, go to Properties, C/C++ Build, Settings, Tool Settings, GCC C++ Compiler, Includes, and add the path /.../workspace/ProjectName.



    现在为了能够在 Eclipse 中运行您的测试构建,它需要知道您希望 IDE 运行什么可执行文件。

    Click on Run, Run Configurations..., and looking at your current run configuration, consolidate these settings by giving, for example, a debug build the name "Debug Build", the C/C++ Application "Debug/Artifact_Name", and the Build Configuration "Debug". Next, create a new run configuration, and call it something like "Test Build", set C/C++ Application to "Test/Artifact_Name", and ensure the build configuration is Test.



    现在,您可以通过选择“事件”构建配置或运行正确的运行配置在运行生产代码和测试代码之间切换。

    最后,这是一个使用 Boost Test 进行单元测试的示例,一旦所有这些设置完成:
    //unit_tests.cpp
    #define BOOST_TEST_DYN_LINK
    #define BOOST_TEST_MODULE someModuleName
    #include <boost/test/unit_test.hpp>
    #include <src/some_object.h>
    
    struct template_objects {
        some_object x;
        template_objects() {
            BOOST_TEST_MESSAGE("Setting up testing objects");
        }
        ~template_objects() {
            BOOST_TEST_MESSAGE("Tearing down testing objects");
        }
    }
    
    BOOST_FIXTURE_TEST_SUITE(testSuiteName, template_objects)
    
    BOOST_AUTO_TEST_CASE(testCase1) {
        x.update();
        BOOST_CHECK(x.is_up_to_date());
    }
    BOOST_AUTO_TEST_CASE(testCase2) {
        BOOST_CHECK(x.is_not_up_to_date());
    }
    
    BOOST_AUTO_TEST_SUITE_END()
    

    这演示了有关使用 Boost Test 的一些关键事项:
  • 定义 BOOST_TEST_DYN_LINK被推荐;在包含 Boost 的任何库之前,您需要定义一种链接测试框架的方法
  • 您必须为“模块”命名,不一定是文件名
  • 为了在进入测试用例之前自动设置和拆卸对象,Boost Test 有 fixtures ,它允许您多次调用对象的预先存在状态
  • struct是什么对这些固定装置进行分组,这意味着您的对象应该有一个明确定义的构造函数和析构函数用于自 Action 用域(如果您没有调用 new ,则在拆卸时不需要 delete)
  • 测试套件只是对您的测试用例进行逻辑分组的一种方式(我还没有测试过,但您可能可以将您的套件分解为多个文件以获得更好的逻辑分离)

  • 附加花絮:要使 Boost Test 更详细,请转到测试构建的运行配置,并添加参数 --log_level=test_suite .

    关于eclipse - C++ Boost 测试、包结构和 Eclipse 项目设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44788731/

    相关文章:

    R - knitr - ShareLatex - install.packages(xyz)

    return - 为什么Rust在主函数中没有返回值,以及无论如何如何返回一个值?

    Eclipse 崩溃后无法启动

    java - 打开跟踪文件时出错。没有那个文件或目录 [2]

    c++ - 如何使用 Eclipse CDT 自动从命名空间中取出一个类?

    主子程序

    error-handling - 如何从主结果处理程序中删除默认的 “Error”

    java - 如何在 Eclipse 中安装 M2E - Maven Integration?

    sql-server - 从 NHibernate 调用 SQL Server 存储过程时,我可以将列表或自定义数据类型作为参数传递吗

    tfs - 团队基础服务器 : References Issue