使用 CMake,如何从 CTest 获取详细输出?

发布于 2024-11-02 07:35:08 字数 954 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

千寻… 2024-11-09 07:35:09

对于使用 Visual Studio 的人来说,这里是该主题的另一个变体(黑客):

cmake -E env CTEST_OUTPUT_ON_FAILURE=1 cmake --build . --target RUN_TESTS

For people using Visual Studio, here another variation (hack) on the theme:

cmake -E env CTEST_OUTPUT_ON_FAILURE=1 cmake --build . --target RUN_TESTS
不寐倦长更 2024-11-09 07:35:09

要使用 XML 文件显示结果,您必须使用以下命令执行测试

~$ ctest -T Test

,我们在 Testing/1234123432/test.xml 中找到结果
测试文件夹中也会生成其他文件

to show the result with XML file you have to execute the test with the following command

~$ ctest -T Test

and we found the result in the Testing/1234123432/test.xml
and other files are generated too in Testing Folder

木格 2024-11-09 07:35:08

您可以使用ctest --output-on-failure选项,或设置环境变量CTEST_OUTPUT_ON_FAILURE,每当测试失败时,它将显示测试程序的任何输出。使用 Makefile 和命令行时执行此操作的一种方法如下:

env CTEST_OUTPUT_ON_FAILURE=1 make check

此堆栈溢出问题和解答展示了如何在 Visual Studio 中设置环境变量。

You can use the ctest --output-on-failure option, or set the environment variable CTEST_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:

env CTEST_OUTPUT_ON_FAILURE=1 make check

This Stack Overflow question and answer shows how to set environment variables in Visual Studio.

清风不识月 2024-11-09 07:35:08

在 cmaking 和制作项目之后,您可以直接调用 ctest

ctest --verbose

You could call ctest directly, after cmaking and making your project.

ctest --verbose
尝蛊 2024-11-09 07:35:08

有一个非常简单的解决方案(由于某种原因很难通过 Google 搜索找到):

ctest --output-on-failure

如果您将 CMake 与 Visual Studio 的打开文件夹功能一起使用,您可以将该

"ctestCommandArgs": "--output-on-failure"

设置添加到构建配置中。

There is a very simple solution (which for some reason is difficult to find via Google Search):

ctest --output-on-failure

If you use CMake with Visual Studio's open folder function you can add the

"ctestCommandArgs": "--output-on-failure"

setting to your build configuration.

南烟 2024-11-09 07:35:08
  1. 您可以检查Testing/Temporary子文件夹。运行 make test 后会自动创建。此文件夹包含两个文件:LastTest.logLastTestsFailed.logLastTest.log 包含运行测试所需的输出。 LastTestFailed.log 包含失败测试的名称。因此,您可以在执行make test后手动检查它们。

  2. 第二种方法是让 ctest 在运行测试后向您显示日志文件的内容:

    1. 放置在构建目录(从中运行make test)文件CTestCustom.ctest(您可以使用配置文件来完成> 命令,例如)包含以下内容

      CTEST_CUSTOM_POST_TEST(“猫测试/临时/LastTest.log”)

您可以使用执行类似操作的任何Windows cmd命令而不是cat 。

  1. 再次运行make test并获得利润!

有关自定义 ctest 的其他信息,您可以在此处找到。只需进入“自定义 cmake”部分即可。
祝你好运!

  1. You can check the Testing/Temporary subfolder. It is automatically created after running make test. This folder contains two files: LastTest.log and LastTestsFailed.log. LastTest.log contains desired output for run tests. LastTestFailed.log contains names of failed tests. So you can check them manually after executing make test.

  2. The second way is to get ctest to show you the content of log files after running tests:

    1. 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 contents

      CTEST_CUSTOM_POST_TEST("cat Testing/Temporary/LastTest.log")

Instead of cat you may use whatever Windows cmd command that does similar things.

  1. run make test again and get profit!

additional info about customizing ctest you can find here. Just step to "Customizing cmake" section.
Good luck!

子栖 2024-11-09 07:35:08

这使得测试输出更加详细:

make test ARGS="-V"

This makes test output more verbose:

make test ARGS="-V"
贱人配狗天长地久 2024-11-09 07:35:08

我必须自己添加“检查”目标。由于某种原因,“进行测试”什么也没做。所以我做了什么(正如 stackoverflow 上某处的建议) - 我手动添加了这个目标。为了获得详细的输出,我只是这样写:

add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --verbose)

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:

add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --verbose)
就此别过 2024-11-09 07:35:08

检查 CTEST_OUTPUT_ON_FAILURE=TRUE

make check CTEST_OUTPUT_ON_FAILURE=TRUE

温柔戏命师 2024-11-09 07:35:08

我的方法是仅来自来自 zbyszek,以及 来自 tarc。我使用 ${CMAKE_COMMAND} 变量(设置为调用的 cmake 可执行文件的绝对路径)和 -E env CTEST_OUTPUT_ON_FAILURE=1 参数来调用实际的 ctest使用 ${CMAKE_CTEST_COMMAND} -C $ 命令。为了帮助阐明发生的情况,我首先使用三个 cmake -E echo 命令来显示当前工作目录和要调用的 ctest 命令。以下是我调用 add_custom_target 的方法。

add_custom_target(check 
        ${CMAKE_COMMAND} -E echo CWD=${CMAKE_BINARY_DIR}
        COMMAND ${CMAKE_COMMAND} -E echo CMD=${CMAKE_CTEST_COMMAND} -C 
lt;CONFIG>
        COMMAND ${CMAKE_COMMAND} -E echo ----------------------------------
        COMMAND ${CMAKE_COMMAND} -E env CTEST_OUTPUT_ON_FAILURE=1
            ${CMAKE_CTEST_COMMAND} -C 
lt;CONFIG>
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    DEPENDS ALL_BUILD
    )

这与 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 three cmake -E echo commands to show the current working directory and the ctest command to be invoked. Here is how I call add_custom_target.

add_custom_target(check 
        ${CMAKE_COMMAND} -E echo CWD=${CMAKE_BINARY_DIR}
        COMMAND ${CMAKE_COMMAND} -E echo CMD=${CMAKE_CTEST_COMMAND} -C 
lt;CONFIG>
        COMMAND ${CMAKE_COMMAND} -E echo ----------------------------------
        COMMAND ${CMAKE_COMMAND} -E env CTEST_OUTPUT_ON_FAILURE=1
            ${CMAKE_CTEST_COMMAND} -C 
lt;CONFIG>
    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    DEPENDS ALL_BUILD
    )

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 on ALL_BUILD so that all projects will be built before invoking the check target. (On Linux builds, one may need to replace ALL_BUILD with ALL; I have not tested this on Linux yet.)

许一世地老天荒 2024-11-09 07:35:08

现在有一个 CMake 变量允许您修改 make test 的行为。 CMAKE_CTEST_ARGUMENTS 允许您设置在运行时传递给 ctest 的参数列表进行测试

因此,将其添加到您的 CMake 文件中(必须位于 enable_testing() 之上):

set(CMAKE_CTEST_ARGUMENTS "--verbose")

意味着 CTest 将始终以详细方式运行。或者仅获取失败测试的输出,请使用:

set(CMAKE_CTEST_ARGUMENTS "--output-on-failure")

编辑:
正如 RobLoach 所建议的,由于它是一个参数列表,因此您需要将其附加到该列表中。

list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure")

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 via make test.

So adding this to your CMake file (must be above enable_testing()):

set(CMAKE_CTEST_ARGUMENTS "--verbose")

Means CTest will always run verbose. Or for just the output of the failed tests, use:

set(CMAKE_CTEST_ARGUMENTS "--output-on-failure")

Edit:
As suggested by RobLoach, since it's a list of arguments, you'll want to append to the list instead.

list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure")
深海少女心 2024-11-09 07:35:08

ctest -VVctest --extra-verbose

来自 文档

启用测试的更详细输出。

测试输出通常被抑制,仅显示摘要信息
显示。此选项将显示更多测试输出。

ctest -VV or ctest --extra-verbose

From documentation:

Enable more verbose output from tests.

Test output is normally suppressed and only summary information is
displayed. This option will show even more test output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文