使用Boost进行单元测试时,您最喜欢/推荐的项目结构和文件结构是什么?

sev*_*axx 7 c++ tdd boost unit-testing visual-studio

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

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

Mar*_* Ba 2

我们基于 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
      ...
Run Code Online (Sandbox Code Playgroud)

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

注意事项:

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

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