简体   繁体   English

如何使用Boost实现测试套件和案例的组织?

[英]How to achieve organisation of test suites and cases with Boost?

Preface 前言

I'm new to Unit Testing and rather new to C++. 我是单元测试的新手,而不是C ++的新手。 Recently I had some experience with test driven development in Ruby using RSpec. 最近我在使用RSpec的Ruby中进行了测试驱动开发的一些经验。 Now I'm trying getting similar working in C++ with Boost's unit testing framework. 现在我正在尝试使用Boost的单元测试框架在C ++中进行类似的工作。

Scenario 脚本

I'm organising my header and source files of the application in a directory /src under the project's root folder. 我正在项目的根文件夹下的目录/src组织应用程序的头文件和源文件。 As I've seen it multiple times around in other C++ programs, having the tests in a directory /tests in the project's root directory seems reasonable. 正如我在其他C ++程序中多次看到的那样,在项目的根目录中的目录/tests中进行/tests似乎是合理的。

Now I want to replicate the directory structure of the source files as well in the tests. 现在我想在测试中复制源文件的目录结构。 Thus, assume I've got the following source/header file structure: 因此,假设我有以下源/头文件结构:

/src
  /controller
    controller_class.h
    controller_class.cpp
  /model
    model_a.h
    model_a.cpp
    model_b.h
    model_b.cpp
  /view
    simple_view.h
    simple_view.cpp

And thus the tests are organised as followed 因此,测试组织如下

/tests
  TestRunner.cpp
  /controller
    controller_class_test.cpp
  /model
    model_a_test.cpp
    model_b_test.cpp
  /view
    simple_view_test.cpp

For the TestRunner.cpp I took the example from this blog post : 对于TestRunner.cpp我从这个博客文章中拿了示例:

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE "MyProgram Unit Tests"
#include <boost/test/unit_test.hpp>

Problem 问题

I now thought to continue in TestRunner.cpp with the creation of the basic test suits (for controller, model and view) as follows 我现在想继续在TestRunner.cpp中创建基本测试套件(用于控制器,模型和视图),如下所示

BOOST_AUTO_TEST_SUITE ( controller )
  //some stuff here
BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE ( model )
  //some stuff here
BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE ( view )
  //some stuff here
BOOST_AUTO_TEST_SUITE_END()

Desire 欲望

But how can I now integrate further nested test suites and cases into these top-level test suits? 但是,我现在如何将更多嵌套测试套件和案例集成到这些顶级测试套件中? I finally want to have the actual test cases only appear in the *_test.cpp files. 我终于希望实际的测试用例只出现在*_test.cpp文件中。 While each of those files wrap the test cases into one additional test suite: 虽然每个文件都将测试用例包装到一个额外的测试套件中:

  • Master Test Module ( TestRunner.cpp ) 主测试模块( TestRunner.cpp
    • Controller Test Suite ( TestRunner.cpp ) 控制器测试套件( TestRunner.cpp
      • Controller Class Test Suite ( controller_class_test.cpp ) 控制器类测试套件( controller_class_test.cpp
        • Controller Class Test Cases ( controller_class_test.cpp ) 控制器类测试用例( controller_class_test.cpp
    • Model Test Suite ( TestRunner.cpp ) 模型测试套件( TestRunner.cpp
      • Model A Test Suite ( model_a_test.cpp ) 模型A测试套件( model_a_test.cpp
        • Model A Test Cases ( model_a_test.cpp ) 模型A测试用例( model_a_test.cpp
      • Model B Test Suite ( model_b_test.cpp ) B型测试套件( model_b_test.cpp
        • Model B Test Cases ( model_B_test.cpp ) B型测试用例( model_B_test.cpp
    • View Test Suite ( TestRunner.cpp ) 查看测试套件( TestRunner.cpp
      • Simple View Test Suite ( simple_view_test.cpp ) 简单视图测试套件( simple_view_test.cpp
        • Simple View Test Cases ( simple_view_test.cpp ) 简单查看测试用例( simple_view_test.cpp

Question

How do I have to include the nested suits and cases in the respective higher level suite? 如何在相应的更高级别套件中包含嵌套套装和案例? I could not find anything in the Boost documentation, though The Unit Test Framework > User's guide > Test organization > Test suite > Automated registration came pretty close. 虽然单元测试框架>用户指南>测试组织>测试套件>自动注册非常接近,但我在Boost文档中找不到任何内容。
In Ruby with RSpec one just needs to place the test files (read: *_spec.rb ) the way I did and its automatically iterating through them. 在使用RSpec的Ruby中,只需要按照我的方式放置测试文件(读取: *_spec.rb ),并自动迭代它们。 I guess, with Boost I have to explicitly declare it that way. 我想,有了Boost,我必须明确地声明它。

Boost.Test test suites are similar to C++ namespaces. Boost.Test测试套件类似于C ++名称空间。 You can restart them any time. 您可以随时重启它们。 Each of your test files just need to place test cases in correct test suites: 每个测试文件只需要将测试用例放在正确的测试套件中:

controller_class_a_test.cpp:

 BOOST_AUTO_TEST_SUITE( controller )

 BOOST_AUTO_TEST_CASE( test_class_a )
 {
 }

 BOOST_AUTO_TEST_SUITE_END()


controller_class_b_test.cpp:

 BOOST_AUTO_TEST_SUITE( controller )

 BOOST_AUTO_TEST_CASE( test_class_b )
 {
 }

 BOOST_AUTO_TEST_SUITE_END()

The same concept applies to the test tree of any depth. 相同的概念适用于任何深度的测试树。 Also you do not need top level TestRunner.cpp at all. 此外,您根本不需要顶级TestRunner.cpp。 Just combine all your test files into a single test module and Boost.Test will take care about the rest. 只需将所有测试文件合并到一个测试模块中,Boost.Test将关注其余部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM