0

I'm using a CMakeLists.txt file to generate (among others) some tests. Therefore I have something like

ENABLE_TESTING() ADD_TEST(NAME my_name COMMAND my_command)

I'd like to add somme commands after the test is run. I found ADD_CUSTOM_COMMAND which seems to do exactly what I need but unfortunately it doesn't work. Here is what I have tried

ADD_CUSTOM_COMMAND(
     TARGET my_name
     POST_BUILD
     COMMAND my_other_command
     )

It seems that I'm not using the right TARGET. Could you help me by telling me what I'm supposed to do?

Many thanks in advance,

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Ezor
  • 135
  • 2
  • 18
  • Difficult to say without knowing what you want to do with the custom command. Would the [`FIXTURES_CLEANUP`](https://cmake.org/cmake/help/latest/prop_test/FIXTURES_CLEANUP.html) test property be what you are looking for? You just define another test e.g. for the cleanup task and add the dependencies with this property accordingly. – Florian Oct 10 '17 at 08:41
  • thank you for your answer. Actually, I need to lauch commands to genererate code coverage report (with gcov and lcov) It can't be done before the test is run, so I'm looking to define new commands that are executed in addition to the main test. Therefore I don't think FIXTURES_CLEANUP could be used. I hope I made it clearer. – Ezor Oct 10 '17 at 08:41
  • One option is to generate a CMake script and then have the test command execute your CMake script. The generated script would invoke your test and then do the coverage report. If you don't care about platform portability, just write a bash script (or whatever) that does the same thing based on arguments and have your test command invoke your script with suitable arguments. – legalize Oct 11 '17 at 20:00
  • What do you mean by 'a script that execute CMake script'? What I need is a way to make the command "make test" run the test descritbe in `ADD_TEST` but also those in `ADD_CUSTOM_COMMAND` – Ezor Oct 12 '17 at 08:41

1 Answers1

0

My goal was to execute a diff command after the test execution finished. This is very similar to the problem you are facing. I used this cmake script technique. Leaving it as an answer in case it helps someone else.

In the original CMakeLists.txt file I added a test

add_test(NAME testCommand
COMMAND ${CMAKE_COMMAND}
        -DCMD1=$<TARGET_FILE:target>
        -DTEST_DATA_DIR=${CMAKE_SOURCE_DIR}
-P ${CMAKE_SOURCE_DIR}/runtests.cmake)

Create a file called runtests.cmake in the directory which has the CMakeLists.txt file. This new file will hold the commands to execute the test and run commands after

include(FindUnixCommands)

macro(EXEC_CHECK CMD)
    execute_process(COMMAND ${CMD} ${TEST_DATA_DIR}/test_input.txt RESULT_VARIABLE CMD_RESULT)
if(CMD_RESULT)
    message(FATAL_ERROR "Error running ${CMD}")
else()
    if (BASH)
        execute_process(COMMAND ${BASH} -c "diff -b ${TEST_DATA_DIR}/test_input.txt ${TEST_DATA_DIR}/test_output.txt" RESULT_VARIABLE RES)
        if(RES)
            message(FATAL_ERROR "Diff is not clean")
        endif()
    else(BASH)
        message(FATAL_ERROR "BASH not found : no diff script run")
    endif(BASH)
endif()
endmacro()
exec_check(${CMD1})

From the build directory I can now issue make test and it picks up the cmake script properly.