Run tests before build

Completed

Hello,

I have a shared library project with 2 configurations Debug | Release. I have also implemented google tests. When the configuration is Release I would like to run some tests (or all) before the build. Also it would be nice to check if all the tests passed and then start/continue the build. Is this possible? If so how?

0
3 comments

What about cmake's add_custom_target or add_custom_command?

 

 

1

Thank you Shkodnikpavel. After your suggestion I managed to do it. Here is what I did:

In my main CMakeLists.txt I have this:

if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
   #Rebuild the tests just in case some tests has changed and the executable was not rebuild
   add_custom_command(TARGET ${PROJECT_NAME} PRE_BUILD
           COMMAND ${CMAKE_COMMAND} --build "${CMAKE_BINARY_DIR}" --target tests --
           )

   if(${WIN32})
      set(TESTS_BIN tests.exe)
   else()
      set(TESTS_BIN tests)
   endif()
   #Run the tests executable
   add_custom_command(TARGET ${PROJECT_NAME} PRE_BUILD
           COMMAND "${CMAKE_BINARY_DIR}/${TESTS_BIN}"
           )
endif()

The add_custom_command() is smart therefore when the tests executable doesn't return 0 (all test have passed successfully) the build will fail and the library will not be build.

1

Please sign in to leave a comment.