1

我使用 Catch2 进行单元测试。我想在构建后运行测试。
所以我在 Catch 中使用了 'cath_discover_test' 函数。
但在构建时,不要打印出任何关于测试的内容。
如下所示:

> cmake --build .
blah ~
blah ~
PostBuildEvent:
  setlocal
  "C:\Program Files\CMake\bin\cmake.exe" -D TEST_TARGET=foo -D TEST_EXECUTABLE=C:/Users/MyName/workspace/someproject/build/
  Debug/foo.exe -D TEST_EXECUTOR= -D TEST_WORKING_DIR=C:/Users/MyName/workspace/someproject/build -D TEST_SPEC= -D TEST_EXT
  RA_ARGS= -D TEST_PROPERTIES= -D TEST_PREFIX= -D TEST_SUFFIX= -D TEST_LIST=foo_TESTS -D CTEST_FILE=C:/Users/MyName/workspace/a
  someproject/build/foo_tests-b858cb2.cmake -P "C:/Program Files (x86)/Catch2/lib/cmake/Catch2/CatchAddTests.cmake"
  if %errorlevel% neq 0 goto :cmEnd
  :cmEnd
  endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
  :cmErrorLevel
  exit /b %1
  :cmDone
  if %errorlevel% neq 0 goto :VCEnd
  :VCEnd
blah ~
blah ~
end

这是test.cmake文件源代码:

find_package(Catch2 REQUIRED)
add_executable(foo ${PROJECT_SOURCE_DIR}/test/test.cpp)
target_include_directories(foo PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(foo Catch2::Catch2)

include(CTest)
include(Catch)
catch_discover_tests(foo)

但是,我通过下面的 ctest 执行测试,测试工作。

>ctest
Test project C:/Users/MyNames/workspace/someproject/build
    Start 1: some class test
1/1 Test #1: some class test .............***Failed    0.02 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.04 sec

The following tests FAILED:
          1 - some class test (Failed)
Errors while running CTest

构建后如何运行测试并显示测试结果的输出?

4

1 回答 1

0

CMake 不希望您在构建的同时运行测试。您可以想象一个交叉编译器的情况,其中目标架构与您的主机构建机器不同,在这种情况下,在构建时运行测试是没有意义的。

我想将构建的测试作为 CMake 构建的一部分add_custom_command运行以运行POST_BUILD命令。

于 2019-01-27T23:11:44.090 回答