简体   繁体   English

CMake + Boost测试:忽略无法构建的测试

[英]CMake + Boost test: ignore tests that fail to build

We have C++ project that has a relatively big number of test suites implemented in Boost/Test. 我们有一个C ++项目,在Boost / Test中实现了相对大量的测试套件。 All tests are kept out of main project's tree, every test suite is located in separate .cpp file. 所有测试都保留在主项目的树中,每个测试套件都位于单独的.cpp文件中。 So, our current CMakeLists.txt for tests looks like this: 因此,我们当前用于测试的CMakeLists.txt如下所示:

cmake_minimum_required(VERSION 2.6)

project(TEST_PROJECT)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)

set(SPEC_SOURCES
    main.cpp
    spec_foo.cpp
    spec_bar.cpp
    ...
)

set(MAIN_PATH some/path/to/our/main/tree)    
set(MAIN_SOURCES
    ${MAIN_PATH}/foo.cpp
    ${MAIN_PATH}/bar.cpp
    ...
)

add_executable (test_project
    ${SPEC_SOURCES}
    ${MAIN_SOURCES}
)

target_link_libraries(test_project
    ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
)

add_test(test_project test_project)

enable_testing()

It works ok, but the problem is SPEC_SOURCES and MAIN_SOURCES are fairly long lists and someone occasionally breaks something in either one of the files in main tree or spec sources. 它可以正常工作,但是问题是SPEC_SOURCESMAIN_SOURCES是相当长的列表,有时有人会破坏主树或spec源文件之一中的某些内容。 This, in turn, makes it impossible to build target executable and test the rest. 反过来,这使得无法构建目标可执行文件并测试其余部分。 One has to manually figure out what was broken, go into CMakeLists.txt and comment out parts that fail to compile. 必须手动找出损坏的部分,进入CMakeLists.txt并注释掉无法编译的部分。

So, the question : is there a way to ignore tests that fail to build automatically in CMake, compile, link and run the rest (ideally, marking up ones that failed as "failed to build")? 因此, 问题是:有没有办法忽略那些无法在CMake中自动构建的测试,编译,链接并运行其余的测试(理想情况下,将失败的测试标记为“构建失败”)?

Remotely related question Best practice using boost test and tests that should not compile suggests to try_compile command in CMake. 远程相关问题使用 CMake中的try_compile命令建议使用Boost测试和不应编译的测试的最佳实践 However, in its bare form it justs executes new ad hoc generated CMakeList (which will fail just as the original one) and doesn't have any hooks to remove uncompilable units. 但是,它仅以裸露的形式执行新生成的临时生成的CMakeList(它将像原始的那样失败),并且没有任何钩子来删除不可编译的单元。

I think you have some issues in your testing approach. 我认为您的测试方法存在一些问题。

One has to manually figure out what was broken, go into CMakeLists.txt and comment out parts that fail to compile. 必须手动找出损坏的部分,进入CMakeLists.txt并注释掉无法编译的部分。

If you have good coverage by unit-tests you should be able to identify and locate problems really quickly. 如果您具有良好的单元测试覆盖率,那么您应该能够真正快速地识别和定位问题。 Continuous integration (eg Jenkins, Buildbot, Travis (GitHub)) can be very helpful. 持续集成(例如Jenkins,Buildbot,Travis(GitHub))可能会非常有帮助。 They will run your tests even if some developers have not done so before committing. 他们将运行您的测试,即使某些开发人员在提交之前没有这样做。

Also you assume that a non-compiling class (and its test) would just have to be removed from the build. 另外,您还假定非编译类(及其测试)仅需从构建中删除。 But what about transitive dependencies, where a non-compiling class breaks compilation of other classes or leads to linking errors. 但是传递依赖呢?传递非依赖性类会导致非编译类中断其他类的编译或导致链接错误。 What about tests that break the build? 破坏构建的测试又如何呢? All these things happen during development. 所有这些事情都是在开发过程中发生的。

I suggest you separate your build into many libraries each having its own test runner. 我建议您将构建分为许多库,每个库都有自己的测试运行器。 Put together what belongs together (cohesion). 将所有内容放在一起(内聚)。 Try to minimize dependencies in your compilation also (dependency injection, interfaces, ...). 还要尽量减少编译中的依赖性(依赖性注入,接口等)。 This will allow to keep development going by having compiling libraries and test runners even if some libs do not compile (for some time). 即使某些库(一段时间)没有编译,这也将通过编译库和测试运行程序来保持开发的进行。

I guess you could create one test executable per spec source, (using a foreach() loop ) and then do something like: 我猜您可以为每个规范源创建一个测试可执行文件(使用foreach() loop ),然后执行以下操作:

make spec_foo &&  ./spec_foo

This will only try to build the binary matching the test you want to run 这只会尝试构建与要运行的测试匹配的二进制文件

But if your build often fails it may be a sign of some bad design in your production code ... 但是,如果您的构建经常失败,则可能表明生产代码中的某些不良设计...

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

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