[CMake] test depending on code compilation

Mario Werner mario.werner at iaik.tugraz.at
Fri Jan 12 03:38:50 EST 2018


On 2018-01-11 18:03, Franck Houssen wrote:
> 
> 
> ----- Mail original -----
>> De: "Mario Werner" <mario.werner at iaik.tugraz.at>
>> À: cmake at cmake.org
>> Envoyé: Jeudi 11 Janvier 2018 16:27:18
>> Objet: Re: [CMake] test depending on code compilation
>>
> [snip]
>
>>> 2) Also the verbosity is broken with check : make test ARGS="-V" is
>>> verbose , but, make check ARGS="-V" is not.
>>>     => how to deal with that ?
>>>
>>>
>>
>> Can't help you there. I usually simply use `make check` to build and run
>> the full test suite in order to get an overview. Afterwards, I directly
>> call `ctest` with the desired arguments, for example when triaging a bug.
>>
> 
> In travis a make check ARGS="-V" could be convenient to see output of tests while they run (understand what is wrong when if it fails - you can't get files from travis).
> Anyway, that not so blocking.
> 

Right, I usually have two custom targets in my projects to deal with
that in a generic way. Namely, "suite" for building the tests and
"check" for building+running the tests.

Developers can then use "make check" as usual to build and run the tests
without providing any arguments to ctest. On the other hand, the CI
system can build using the "suite" target and execute ctest directly
like in the following example. (Of course developers can do the same if
needed)

```

    - mkdir _build
    - cd _build
    - cmake -DCMAKE_BUILD_TYPE=Release ..
    - cmake --build . --target suite -- -j2
    - ctest -V

```

I use the following helper macro to generate the needed boilerplate
code. Usage is then as simple as calling `add_to_suite` for every target.

```

macro(add_to_suite target)
  # add suite target which is used as meta target for building the tests
  if(NOT TARGET "suite")
    add_custom_target("suite")
  endif()

  # add check command which calls ctest
  # it additionally depends on the suite target to build the test cases
  if(NOT TARGET "check")
    add_custom_target("check" COMMAND ${CMAKE_CTEST_COMMAND}
                              WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
                              COMMENT "Executing test suite.")
    add_dependencies("check" "suite")
  endif()

  add_dependencies("suite" ${target})
endmacro()

# usage example (repeat for any number of tests you have
add_executable(testexe EXCLUDE_FROM_ALL ${test_source_files})
target_link_libraries(testexe ${link_dependencies})
add_to_suite(testexe)

```

Note that, instead of specifying EXCLUDE_FROM_ALL on every
add_executable, I usually have all my tests in one directory and specify
EXCLUDE_FROM_ALL on the add_subdirectory call.



More information about the CMake mailing list