3

I'm using CTest with CMake to run some tests. I use the enable_testing() command which provides me with a default command for make test. All of the tests in my subdirectory are accounted for (by doing an add_test command) and make test works great, except one problem.

There is a certain test, which I've named skip_test, that I do NOT want being run when I do make test. I would like to add a custom target so I can run make skip_test and it will run that test.

I can do this by doing add_custom_target(skip_test ...) and providing CTest with the -R flag and telling it to look for files containing "skip_test" in their name. This also seems to work. My problem now is: how can I get the make test command to ignore skip_test?

If I try commenting out enable_testing and adding my own add_custom_target(test ....), I get "No tests found!!!" now for either make test or make skip_test. I also tried making a Custom CTest file and adding set(CTEST_CUSTOM_TESTS_IGNORE skip_test). This worked so that now make test ignored "skip_test", but now running make skip_test responds with "no tests found!!!".

Any suggestions would be appreciated!

4

2 回答 2

2

我实际上使用了不同的解决方案。这就是我所做的。对于我想排除的测试,我在添加它们时使用了以下命令:

"add_test( ..... CONFIGURATIONS ignore_flag)" 其中ignore_flag 是你想要的任何短语。然后,在我的 CMakeLists.txt 中,当我定义一个自定义目标 add_custom_target(ignore_tests ...) 我给它 ctest .... -C ignore_flag

现在,make test 将跳过这些测试!make ignore_Tests 将运行忽略的测试+未忽略的测试,我可以接受。

于 2015-07-22T23:14:08.993 回答
1

我不确定完全通过 CTest 完成此操作的方法,但由于您已使用“googletest”标记此问题,我假设您将其用作您的测试框架。因此,您也许可以利用 Gtest禁用测试运行禁用测试的能力。

通过将有问题的测试更改为DISABLED_在其名称中包含前导,默认情况下这些测试不会在您这样做时运行make test

然后,您可以添加自定义目标,该目标将使用适当的 Gtest 标志调用您的测试可执行文件,以仅运行禁用的测试:

add_custom_target(skip_test
    MyTestBinary --gtest_filter=*DISABLED_* --gtest_also_run_disabled_tests VERBATIM)

这有点滥用 Gtest 功能 - 它实际上是用来暂时禁用测试,同时重构任何东西以使测试再次通过。这比仅仅注释掉测试要好,因为它会继续编译它,并且在运行套件后它会发出一个唠叨的提醒,说明你已经禁用了测试。

于 2015-07-07T19:08:35.540 回答