CMake:带有单元测试的项目结构

2021-12-09 00:00:00 unit-testing cmake c++ boost boost-test

我正在尝试构建我的项目以包含生产源(在 src 子文件夹中)和测试(在 test 子文件夹中).我正在使用 CMake 来构建它.作为一个最小的例子,我有以下文件:

I am trying to structure my project to include the production sources (in src subfolder) and tests (in test subfolder). I am using CMake to build this. As a minimal example I have the following files:

CMakeLists.txt:

CMakeLists.txt:

cmake_minimum_required (VERSION 2.8) 
project (TEST) 

add_subdirectory (src) 
add_subdirectory (test) 

src/CMakeLists.txt:

src/CMakeLists.txt:

add_executable (demo main.cpp sqr.cpp) 

src/sqr.h

#ifndef SQR_H
#define SQR_H
double sqr(double);    
#endif // SQR_H

src/sqr.cpp

src/sqr.cpp

#include "sqr.h"
double sqr(double x) { return x*x; }

src/main.cpp - 使用 sqr,无所谓

src/main.cpp - uses sqr, doesn't really matter

测试/CMakeLists.txt:

test/CMakeLists.txt:

find_package(Boost COMPONENTS system filesystem unit_test_framework REQUIRED)

include_directories (${TEST_SOURCE_DIR}/src) 

ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK) 

add_executable (test test.cpp ${TEST_SOURCE_DIR}/src/sqr.cpp) 

target_link_libraries(test
                      ${Boost_FILESYSTEM_LIBRARY}
                      ${Boost_SYSTEM_LIBRARY}
                      ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
                      )

enable_testing()
add_test(MyTest test)

test/test.cpp:

test/test.cpp:

#define BOOST_TEST_MODULE SqrTests
#include <boost/test/unit_test.hpp>

#include "sqr.h"

BOOST_AUTO_TEST_CASE(FailTest)
{
    BOOST_CHECK_EQUAL(5, sqr(2));
}

BOOST_AUTO_TEST_CASE(PassTest)
{
    BOOST_CHECK_EQUAL(4, sqr(2));
}

几个问题:

  1. 这种结构有意义吗?构建此代码时的最佳实践是什么?(我来自 C# 和 java,从某种意义上说它更容易)
  2. 我不喜欢我必须在 test/CMakeLists.txt 文件中列出 src 文件夹中的所有文件的事实.如果这是一个图书馆项目,我只会链接图书馆.有没有办法避免列出其他项目中的所有 cpp 文件?
  3. enable_testing()add_test(MyTest test) 这两条线在做什么?我没有看到任何效果.如何从 CMake(或 CTest)运行测试?
  4. 到目前为止,我只是在根文件夹中运行了 cmake . ,但这造成了到处都是临时文件的混乱.如何以合理的结构获得编译结果?
  1. Does this structure make sense? What are the best practises when structuring this code? (I am coming from C# and java, and there it is easier in a sense)
  2. I don't like the fact that I have to list all the files from the src folder in the test/CMakeLists.txt file. If this was a library project, I would just link the library. Is there a way to avoid listing all the cpp files from the other project?
  3. What are the lines enable_testing() and add_test(MyTest test) doing? I haven't seen any effect. How can I run the tests from CMake (or CTest)?
  4. So far I just ran cmake . in the root folder, but this created a mess with temporary files everywhere. How can I get the compilation results in a reasonable structure?

推荐答案

对于问题 1 &2,我建议从你的非测试文件中创建一个库,不包括 main.cpp(在这种情况下只是 src/sqr.cpp 和 src/sqr.h),然后你可以避免列出(更重要的是重新编译)所有来源两次.

For questions 1 & 2, I would recommend making a library from your non-test files excluding main.cpp (in this case just src/sqr.cpp and src/sqr.h), and then you can avoid listing (and more importantly re-compiling) all the sources twice.

对于问题 3,这些命令添加了一个名为MyTest"的测试,它不带任何参数调用您的可执行文件test".但是,由于您已将这些命令添加到 test/CMakeLists.txt 而不是顶级 CMakeLists.txt,因此您只能从构建树的test"子目录中调用测试(尝试 cd test && ctest -N).如果您希望测试可从顶级构建目录运行,则需要从顶级 CMakeLists.txt 调用 add_test.这也意味着您必须使用更详细的形式 add_test 因为您的测试 exe 未在同一个 CMakeLists.txt 中定义

For question 3, these commands add a test called "MyTest" which invokes your executable "test" without any arguments. However, since you've added these commands to test/CMakeLists.txt and not your top-level CMakeLists.txt, you can only invoke the test from within the "test" subdirectory of your build tree (try cd test && ctest -N). If you want the test to be runnable from your top-level build directory, you'd need to call add_test from the top-level CMakeLists.txt. This also means you have to use the more verbose form of add_test since your test exe isn't defined in the same CMakeLists.txt

就您而言,由于您在根文件夹中运行 cmake,因此您的构建树和源代码树是一回事.这被称为源内构建,并不理想,从而导致问题 4.

In your case, since you're running cmake in the root folder, your build tree and your source tree are one and the same. This is known as an in-source build and isn't ideal, which leads to question 4.

生成构建树的首选方法是进行源外构建,即在源树之外的某处创建一个目录并从那里执行 cmake.即使在项目的根目录中创建一个build"目录并执行 cmake .. 也会提供一个干净的结构,不会干扰你的源代码树.

The preferred method for generating the build tree is to do an out-of-source build, i.e. create a directory somewhere outside of your source tree and execute cmake from there. Even creating a "build" directory in the root of your project and executing cmake .. would provide a clean structure which won't interfere with your source tree.

最后一点是避免调用可执行文件测试"(区分大小写).有关原因,请参阅此答案.

One final point is to avoid calling executables "test" (case-sensitive). For reasons why, see this answer.

要实现这些更改,我会执行以下操作:

To achieve these changes, I'd do the following:

CMakeLists.txt:

CMakeLists.txt:

cmake_minimum_required (VERSION 2.8)
project (TEST)
add_subdirectory (src) 
add_subdirectory (test)
enable_testing ()
add_test (NAME MyTest COMMAND Test)


src/CMakeLists.txt:


src/CMakeLists.txt:

add_library (Sqr sqr.cpp sqr.h)
add_executable (demo main.cpp)
target_link_libraries (demo Sqr)


测试/CMakeLists.txt:


test/CMakeLists.txt:

find_package (Boost COMPONENTS system filesystem unit_test_framework REQUIRED)
include_directories (${TEST_SOURCE_DIR}/src
                     ${Boost_INCLUDE_DIRS}
                     )
add_definitions (-DBOOST_TEST_DYN_LINK)
add_executable (Test test.cpp)
target_link_libraries (Test
                       Sqr
                       ${Boost_FILESYSTEM_LIBRARY}
                       ${Boost_SYSTEM_LIBRARY}
                       ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
                       )

相关文章