cpp_vcpkg_project icon indicating copy to clipboard operation
cpp_vcpkg_project copied to clipboard

Add a wrapper module for add_test

Open FeignClaims opened this issue 2 years ago • 2 comments

Add a cmake module cmake/AddTest.cmake that provides wrappers to simplify test writing, especially when many tiny tests are required (learnt the idea from range-v3).

I can't measure how general this module will be, so I propose to add the module here instead of project_options.

Before: (even already abstract the common part out as catch2_test_common)

# test dependencies
include(CTest)

# ----------------------------------------------------------------------------------------------------------------------
# test the executable part
add_test(
  # calling my_exe executable directly
  NAME my_exe_test
  COMMAND my_exe ""
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

# ----------------------------------------------------------------------------------------------------------------------
# test the library part
add_executable(my_exe_lib_tests "./tests.cpp")
target_link_libraries(
  my_exe_lib_tests
  PRIVATE my_project_warnings
          my_project_options
          catch2_test_common
          my_exe_lib)

# use xml reporter if coverage is enabled
if(${ENABLE_COVERAGE})
  set(COVERAGE_ARGS REPORTER xml)
endif()

# automatically discover tests that are defined in catch based test files you can modify the tests
catch_discover_tests(my_exe_lib_tests ${COVERAGE_ARGS})

After:

add_library_test(my_exe_lib tests CONFIGS common SOURCES tests.cpp)
add_executable_test(my_exe no_arg)

Users can also compose CONFIGS for reuse:

add_test_config(relaxed_constexpr
  COMPILE_DEFINITIONS
  -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE
)

add_library_test(my_lib relaxed_constexpr_tests CONFIGS common relaxed_constexpr SOURCES constexpr_tests.cpp)

Functions:

  • add_test_config: generate a test config target that can be used by linking it, the name of this config target will be test_config.<config_name>.

    add_test_config(<config_name>  # target will be named as `test_config.${config_name}`
      [SOURCES <arg1...>]
      [INCLUDES <arg1...>]
      [SYSTEM_INCLUDES <arg1...>]
      [DEPENDENCIES_CONFIG <arg1...>]
      [DEPENDENCIES <arg1...>]
      [LIBRARIES <arg1...>]
      [SYSTEM_LIBRARIES <arg1...>]
      [COMPILE_DEFINITIONS <arg1...>]
      [COMPILE_OPTIONS <arg1...>]
      [COMPILE_FEATURES <arg1...>]
      [EXECUTE_ARGS <arg1...>]
    )
    
  • add_library_test: add a test for the <library>, the name of this target will be test.<library>.<test_name>.

    add_library_test(<library> <test_name>
      [CONFIGS <arg1...>]  # accepts both `${config_name}` and `test_config.${config_name}`
      [SOURCES <arg1...>]
      [INCLUDES <arg1...>]
      [SYSTEM_INCLUDES <arg1...>]
      [DEPENDENCIES_CONFIG <arg1...>]
      [DEPENDENCIES <arg1...>]
      [LIBRARIES <arg1...>]
      [SYSTEM_LIBRARIES <arg1...>]
      [COMPILE_DEFINITIONS <arg1...>]
      [COMPILE_OPTIONS <arg1...>]
      [COMPILE_FEATURES <arg1...>]
      [EXECUTE_ARGS <arg1...>]
    )
    
  • add_executable_test: add a test for the <executable> by running it, the name of this target will be test.<executable>.<test_name>.

    add_executable_test(<executable> <test_name>
      [CONFIGS <arg1...>]  # accepts both `${config_name}` and `test_config.${config_name}`
      [EXECUTE_ARGS <arg1...>]
    )
    

FeignClaims avatar Oct 01 '23 06:10 FeignClaims

I like this feature. However, I think it can be moved to project_options itself

aminya avatar Oct 02 '23 01:10 aminya

Finished the move except for changing the version of project_options

FeignClaims avatar Oct 02 '23 03:10 FeignClaims