使用 CMake,如何从 CTest 获取详细输出?
我正在使用 CMake 来构建我的项目。我添加了一个使用 Boost 单元测试框架的单元测试二进制文件。这一二进制文件包含所有单元测试。我已添加要由 CTest 运行的二进制文件:
ADD_EXECUTABLE( tftest test-main.cpp )
ENABLE_TESTING()
ADD_TEST( UnitTests tftest)
但是 Visual Studio 中的构建输出仅显示运行 CTest 的结果:
Start 1: UnitTests
1/1 Test #1: UnitTests ................***Failed 0.05 sec
0% tests passed, 1 tests failed out of 1
这不是很有帮助,因为我看不到哪个测试失败了。如果我使用 --verbose
从命令行手动运行 ctest,我会得到 Boost 单元测试的输出,它告诉我们实际失败的内容:
1: Test command: tftest.exe
1: Test timeout computed to be: 9.99988e+006
1: Running 4 test cases...
1: test-main.cpp(20): error in "sanity_check3": check 1 == 2 failed
1:
1: *** 1 failure detected in test suite "Master Test Suite"
1/1 Test #1: UnitTests ................***Failed 0.00 sec
那么,我需要在 CMakeLists.txt 中更改什么才能获得CTest 始终以 --verbose
运行?有没有更好的方法将 Boost 单元测试与 CMake/CTest 结合使用?
I'm using CMake to build my project. I have added a unit test binary which is using the Boost unit testing framework. This one binary contains all of the unit tests. I've added that binary to be run by CTest:
ADD_EXECUTABLE( tftest test-main.cpp )
ENABLE_TESTING()
ADD_TEST( UnitTests tftest)
But the build output in Visual Studio only shows the result of running CTest:
Start 1: UnitTests
1/1 Test #1: UnitTests ................***Failed 0.05 sec
0% tests passed, 1 tests failed out of 1
This is not very helpful, because I can't see which test failed. If I run ctest manually from the command line with --verbose
I get the output from a Boost unit test which tells what actually failed:
1: Test command: tftest.exe
1: Test timeout computed to be: 9.99988e+006
1: Running 4 test cases...
1: test-main.cpp(20): error in "sanity_check3": check 1 == 2 failed
1:
1: *** 1 failure detected in test suite "Master Test Suite"
1/1 Test #1: UnitTests ................***Failed 0.00 sec
So, what do I need to change in the CMakeLists.txt to have CTest run with --verbose
at all times? Is there a better way to use Boost unit tests with CMake/CTest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
对于使用 Visual Studio 的人来说,这里是该主题的另一个变体(黑客):
For people using Visual Studio, here another variation (hack) on the theme:
要使用 XML 文件显示结果,您必须使用以下命令执行测试
,我们在 Testing/1234123432/test.xml 中找到结果
测试文件夹中也会生成其他文件
to show the result with XML file you have to execute the test with the following command
and we found the result in the Testing/1234123432/test.xml
and other files are generated too in Testing Folder
您可以使用
ctest --output-on-failure
选项,或设置环境变量CTEST_OUTPUT_ON_FAILURE
,每当测试失败时,它将显示测试程序的任何输出。使用 Makefile 和命令行时执行此操作的一种方法如下:此堆栈溢出问题和解答展示了如何在 Visual Studio 中设置环境变量。
You can use the
ctest --output-on-failure
option, or set the environment variableCTEST_OUTPUT_ON_FAILURE
, which will show you any output from the test program whenever the test fails. One way to do this when using Makefiles and the command line would be as follows:This Stack Overflow question and answer shows how to set environment variables in Visual Studio.
在 cmaking 和制作项目之后,您可以直接调用
ctest
。You could call
ctest
directly, after cmaking and making your project.有一个非常简单的解决方案(由于某种原因很难通过 Google 搜索找到):
如果您将 CMake 与 Visual Studio 的打开文件夹功能一起使用,您可以将该
设置添加到构建配置中。
There is a very simple solution (which for some reason is difficult to find via Google Search):
If you use CMake with Visual Studio's open folder function you can add the
setting to your build configuration.
您可以检查
Testing/Temporary
子文件夹。运行 make test 后会自动创建。此文件夹包含两个文件:LastTest.log
和LastTestsFailed.log
。LastTest.log
包含运行测试所需的输出。LastTestFailed.log
包含失败测试的名称。因此,您可以在执行make test
后手动检查它们。第二种方法是让 ctest 在运行测试后向您显示日志文件的内容:
放置在构建目录(从中运行
make test
)文件CTestCustom.ctest(您可以使用配置文件来完成> 命令,例如)包含以下内容CTEST_CUSTOM_POST_TEST(“猫测试/临时/LastTest.log”)
您可以使用执行类似操作的任何Windows cmd命令而不是cat 。
make test
并获得利润!有关自定义 ctest 的其他信息,您可以在此处找到。只需进入“自定义 cmake”部分即可。
祝你好运!
You can check the
Testing/Temporary
subfolder. It is automatically created after running make test. This folder contains two files:LastTest.log
andLastTestsFailed.log
.LastTest.log
contains desired output for run tests.LastTestFailed.log
contains names of failed tests. So you can check them manually after executingmake test
.The second way is to get ctest to show you the content of log files after running tests:
place in build dir (from which you run
make test
) file CTestCustom.ctest (you can do it with configure file command, for example) with following contentsCTEST_CUSTOM_POST_TEST("cat Testing/Temporary/LastTest.log")
Instead of cat you may use whatever Windows cmd command that does similar things.
make test
again and get profit!additional info about customizing ctest you can find here. Just step to "Customizing cmake" section.
Good luck!
这使得测试输出更加详细:
This makes test output more verbose:
我必须自己添加“检查”目标。由于某种原因,“进行测试”什么也没做。所以我做了什么(正如 stackoverflow 上某处的建议) - 我手动添加了这个目标。为了获得详细的输出,我只是这样写:
I had to add "check" target by myself. "make tests" does nothing by some reason. So what I did (as was suggest somewhere on stackoverflow) - I added this target manually. To get verbose output I just wrote it like:
检查 CTEST_OUTPUT_ON_FAILURE=TRUE
make check CTEST_OUTPUT_ON_FAILURE=TRUE
我的方法是仅来自、来自 zbyszek,以及 来自 tarc。我使用
${CMAKE_COMMAND}
变量(设置为调用的 cmake 可执行文件的绝对路径)和-E env CTEST_OUTPUT_ON_FAILURE=1
参数来调用实际的 ctest使用${CMAKE_CTEST_COMMAND} -C $
命令。为了帮助阐明发生的情况,我首先使用三个cmake -E echo
命令来显示当前工作目录和要调用的 ctest 命令。以下是我调用add_custom_target
的方法。这与 MSVC IDE 配合得很好,其中任何测试错误都显示为可单击的编译错误。请参阅 cmake -E env< /a> 有关
cmake -E
便携式命令行工具模式的文档。我还添加了对ALL_BUILD
的依赖项,以便在调用check
目标之前构建所有项目。 (在 Linux 版本上,可能需要将ALL_BUILD
替换为ALL
;我尚未在 Linux 上测试过这一点。)My approach is a combination of the answers from ony, from zbyszek, and from tarc. I use the
${CMAKE_COMMAND}
variable (which is set to the absolute path to the invoked cmake executable) with the-E env CTEST_OUTPUT_ON_FAILURE=1
argument to invoke the actual ctest command using${CMAKE_CTEST_COMMAND} -C $<CONFIG>
. To help clarify what is going on, I start with threecmake -E echo
commands to show the current working directory and the ctest command to be invoked. Here is how I calladd_custom_target
.This plays nice with the MSVC IDE where any test errors are shown as clickable compilation errors. See cmake -E env for documentation of the
cmake -E
portable command line tool mode. I also add a dependency onALL_BUILD
so that all projects will be built before invoking thecheck
target. (On Linux builds, one may need to replaceALL_BUILD
withALL
; I have not tested this on Linux yet.)现在有一个 CMake 变量允许您修改
make test
的行为。 CMAKE_CTEST_ARGUMENTS 允许您设置在运行时传递给 ctest 的参数列表进行测试
。因此,将其添加到您的 CMake 文件中(必须位于
enable_testing()
之上):意味着 CTest 将始终以详细方式运行。或者仅获取失败测试的输出,请使用:
编辑:
正如 RobLoach 所建议的,由于它是一个参数列表,因此您需要将其附加到该列表中。
There's now a CMake variable that allows you to modify the behaviour of
make test
. CMAKE_CTEST_ARGUMENTS lets you set a list of arguments to pass to ctest when run viamake test
.So adding this to your CMake file (must be above
enable_testing()
):Means CTest will always run verbose. Or for just the output of the failed tests, use:
Edit:
As suggested by RobLoach, since it's a list of arguments, you'll want to append to the list instead.
ctest -VV
或ctest --extra-verbose
来自 文档:
ctest -VV
orctest --extra-verbose
From documentation: