Skip to content
Snippets Groups Projects
Commit 866e208d authored by Alexis Girault's avatar Alexis Girault
Browse files

TEST: Use custom GTEST_ADD_TESTS to register all tests

ctests would currently only be created for each test file,
but the GTEST_ADD_TESTS function[1] could allow to register
all gtests as independent tests.

[1] https://cmake.org/cmake/help/v3.0/module/FindGTest.html

However, that function would not allow the use of a driver
executable, since it would force the `gtest_filter` flag to
appear right after the executable name in the command, while
a driver executable would need the name of the test file
beforehand.

This commit introduces a custom version of GTEST_ADD_TESTS
where `extra_args` is called before the `gtest_filter` flag
to allow the use of an executable driver.

Note: as noted in the doc above[1], testing each gtest is
slow since they all necessite the startup of an executable.
The previous option of testing per file is therefore kept
but commented. A future focus of interest could be to
customize `add_test` to then run all gtests at once like
GoogleTest does by default.
parent 661b052a
No related branches found
No related tags found
No related merge requests found
# gtest_add_tests
#
# Taken from https://github.com/Kitware/CMake/blob/master/Modules/FindGTest.cmake
#
# Only changed the 'add_test' line to add 'extra_args' before the 'gtest_filter' flag.
# Doing so allows to use gtest with a ctest driver created after create_test_sourcelist,
# by providing the test file name as the first argument to the driver.
#
# Changes should become a part of CMake. Better improvements could be made to bring
# ctest and gtest together.
#
function(GTEST_ADD_TESTS executable extra_args)
if(NOT ARGN)
message(FATAL_ERROR "Missing ARGN: Read the documentation for GTEST_ADD_TESTS")
endif()
if(ARGN STREQUAL "AUTO")
# obtain sources used for building that executable
get_property(ARGN TARGET ${executable} PROPERTY SOURCES)
endif()
set(gtest_case_name_regex ".*\\( *([A-Za-z_0-9]+) *, *([A-Za-z_0-9]+) *\\).*")
set(gtest_test_type_regex "(TYPED_TEST|TEST_?[FP]?)")
foreach(source ${ARGN})
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${source})
file(READ "${source}" contents)
string(REGEX MATCHALL "${gtest_test_type_regex} *\\(([A-Za-z_0-9 ,]+)\\)" found_tests ${contents})
foreach(hit ${found_tests})
string(REGEX MATCH "${gtest_test_type_regex}" test_type ${hit})
# Parameterized tests have a different signature for the filter
if("x${test_type}" STREQUAL "xTEST_P")
string(REGEX REPLACE ${gtest_case_name_regex} "*/\\1.\\2/*" test_name ${hit})
elseif("x${test_type}" STREQUAL "xTEST_F" OR "x${test_type}" STREQUAL "xTEST")
string(REGEX REPLACE ${gtest_case_name_regex} "\\1.\\2" test_name ${hit})
elseif("x${test_type}" STREQUAL "xTYPED_TEST")
string(REGEX REPLACE ${gtest_case_name_regex} "\\1/*.\\2" test_name ${hit})
else()
message(WARNING "Could not parse GTest ${hit} for adding to CTest.")
continue()
endif()
add_test(NAME ${test_name} COMMAND ${executable} ${extra_args} --gtest_filter=${test_name}) # changed here
endforeach()
endforeach()
endfunction()
# imstk_add_test
#
# Description: Will create tests for the given iMSTK target.
......@@ -82,11 +127,19 @@ function(imstk_add_test target)
foreach(test_file ${test_files})
get_filename_component(test_name ${test_file} NAME_WE)
include(ExternalData)
ExternalData_add_test( ${PROJECT_NAME}Data
NAME ${test_name}
COMMAND $<TARGET_FILE:${test_driver_executable}> ${test_name} --gtest_filter=${test_name}.*
)
# A. Registers tests per gTests
# [aka] a lot of tests - Google Testing standards
gtest_add_tests(${test_driver_executable} ${test_name} ${test_file})
# ... or ...
# B. Registers tests per test files
# [aka] less tests - CTest standards
# include(ExternalData)
# ExternalData_add_test( ${PROJECT_NAME}Data
# NAME ${test_name}
# COMMAND $<TARGET_FILE:${test_driver_executable}> ${test_name} --gtest_filter=${test_name}.*
# )
endforeach()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment