到目前为止我还没有使用过单元测试,我打算采用这个程序。 TDD 给我留下了深刻的印象,当然想尝试一下 - 我几乎可以肯定这是要走的路。
Boost 看起来是个不错的选择,主要是因为它正在维护中。话虽如此,我应该如何着手实现一个有效且优雅的文件结构和项目结构?我在 Win XP 中使用 VS 2005。我一直在谷歌上搜索这个,但比开悟更困惑。
最佳答案
我们基于 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++) 世界中,您必须权衡取舍。遗留问题、开发人员便利性、编译时间等。我认为我们的项目结构是一个很好的权衡。 :-)
关于c++ - 您最喜欢/推荐的使用 Boost 进行单元测试的项目结构和文件结构是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2965864/