【问题标题】:What is your favorite/recommended project structure and file structure for Unit Testing using Boost?您最喜欢/推荐的使用 Boost 进行单元测试的项目结构和文件结构是什么?
【发布时间】:2011-02-27 07:05:14
【问题描述】:

到目前为止我还没有使用过单元测试,我打算采用这个程序。 TDD 给我留下了深刻的印象,当然想试一试 - 我几乎可以肯定这是要走的路。

Boost 看起来是一个不错的选择,主要是因为它正在维护中。话虽如此,我应该如何去实现一个工作和优雅的文件结构和项目结构?我在 Win XP 中使用 VS 2005。我一直在谷歌上搜索这个问题,但更多的是困惑而不是开明。

【问题讨论】:

    标签: c++ visual-studio unit-testing boost tdd


    【解决方案1】:

    我们基于 Boost 的测试结构如下所示:

    ProjectRoot/
      Library1/
        lib1.vcproj
        lib1.cpp
        classX.cpp
        ...
      Library2/
        lib2.vcproj
        lib2.cpp
        toolB.cpp
        classY.cpp
        ...
      MainExecutable/
        main.cpp
        toolA.cpp
        toolB.cpp
        classZ.cpp
        ...
      Tests/
        unittests.sln
        ut_lib1/
          ut_lib1.vcproj (referencing the lib1 project)
          ut_lib1.cpp (with BOOST_AUTO_TEST_CASE) - testing public interface of lib1
          ut_classX.cpp - testing of a class or other entity might be split 
                          into a separate test file for size reasons or if the entity
                          is not part of the public interface of the library
          ...
        ut_lib2/
          ut_lib2.vcproj (referencing the lib2 project)
          ut_lib2.cpp (with BOOST_AUTO_TEST_CASE) - testing public interface of lib2
          ...
        ut_toolA/
          ut_toolA.vcproj (referencing the toolA.cpp file)
          ut_toolA.cpp - testing functions of toolA
        ut_toolB/
          ut_toolB.vcproj (referencing the toolB.cpp file)
          ut_toolB.cpp - testing functions of toolB
        ut_main/
          ut_main.vcproj (referencing all required cpp files from the main project)
          ut_classZ.cpp - testing classZ
          ...
    

    这种结构是为遗留项目选择的,我们必须根据具体情况决定要添加哪些测试以及如何为现有源代码模块对测试项目进行分组。

    注意事项:

    • 单元测试代码始终与生产代码分开编译。
    • 生产项目不引用单元测试代码。
    • 单元测试项目直接包含源文件或仅包含参考库,具体取决于使用特定代码文件时的意义。
    • 运行单元测试是通过每个 ut_*.vcproj 中的构建后步骤完成的
    • 我们所有的生产版本都会自动运行单元测试。 (在我们的构建脚本中。)

    在我们真实的 (C++) 世界中,您必须做出权衡。遗留问题、开发人员便利性、编译时间等。我认为我们的项目结构是一个很好的权衡。 :-)

    【讨论】:

      【解决方案2】:

      我将核心代码溢出到 .libs 或 .dlls 中,然后让我的 Boost 测试项目依赖于这些 lib/dll 项目。所以我可能会得到:

      ProjectRoot
        Lib1Source
          Lib1Tests
        Lib2Source
          Lib2Tests
      

      另一种方法是将您的源代码存储在一个单独的文件夹中,并将文件添加到您的主应用程序项目和单元测试项目中,但我觉得这有点混乱。 YMMV。

      【讨论】:

      • ProjectRoot 依赖项怎么样? ProjectRoot 是否有依赖于所有其他测试的 ProjectRootTests?
      猜你喜欢
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      相关资源
      最近更新 更多