Using REQUIRED_FILES for unit tests in cmake

2019-08-25 22:29发布

问题:

Very short example trying to use REQUIRED_FILES:

$ tree .
.
├── CMakeLists.txt
└── main.cxx
$ cat main.cxx 
int main() { return 0; }
$ cat CMakeLists.txt 
cmake_minimum_required(VERSION 3.10)

add_executable(a.out main.cxx)

include(CTest)
add_test(NAME a.out COMMAND a.out)
set_property(TEST a.out PROPERTY REQUIRED_FILES $<TARGET_FILE:a.out>)

One executable, which does nothing, and is a test that just requires itself to exist.

$ mkdir build && cd build && cmake .. && ctest
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/lib64/ccache/cc
-- Check for working C compiler: /usr/lib64/ccache/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/lib64/ccache/c++
-- Check for working CXX compiler: /usr/lib64/ccache/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /[...]/req/build
Test project /[...]/req/build
    Start 1: a.out
Could not find executable /[...]/req/build/a.out
Looked in the following places:
/[...]/req/build/a.out
/[...]/req/build/a.out
/[...]/req/build/Release/a.out
/[...]/req/build/Release/a.out
/[...]/req/build/Debug/a.out
/[...]/req/build/Debug/a.out
/[...]/req/build/MinSizeRel/a.out
/[...]/req/build/MinSizeRel/a.out
/[...]/req/build/RelWithDebInfo/a.out
/[...]/req/build/RelWithDebInfo/a.out
/[...]/req/build/Deployment/a.out
/[...]/req/build/Deployment/a.out
/[...]/req/build/Development/a.out
/[...]/req/build/Development/a.out
[...]/req/build/a.out
[...]/req/build/a.out
[...]/req/build/Release/a.out
[...]/req/build/Release/a.out
[...]/req/build/Debug/a.out
[...]/req/build/Debug/a.out
[...]/req/build/MinSizeRel/a.out
[...]/req/build/MinSizeRel/a.out
[...]/req/build/RelWithDebInfo/a.out
[...]/req/build/RelWithDebInfo/a.out
[...]/req/build/Deployment/a.out
[...]/req/build/Deployment/a.out
[...]/req/build/Development/a.out
[...]/req/build/Development/a.out
Unable to find required file: /[...]/req/build/a.out
1/1 Test #1: a.out ............................***Not Run   0.00 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.00 sec

The following tests FAILED:
      1 - a.out (Not Run)
Errors while running CTest

I thought the point of REQUIRED_FILES is to avoid running this test, because that file doesn't exist, and hence not have a failed test. What's the right way to aactually use this property?

回答1:

You are using the property correctly as the ctest didn't actually run the test. CTest can't marked the test as successful as it didn't run, and won't mark the test as failed due to the required files so it places the test in the Not Run bin.

As for why the required files are not found, that is due to the fact that a.out binary was not built. You are missing a step in your script:

mkdir build && cd build && cmake .. && cmake --build . && ctest

You could also simplify the script to:



标签: cmake ctest