Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a wrapper module for add_test #35

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

FeignClaims
Copy link
Contributor

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...>]
    )

@aminya
Copy link
Owner

aminya commented Oct 2, 2023

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

@FeignClaims
Copy link
Contributor Author

Finished the move except for changing the version of project_options

@FeignClaims FeignClaims marked this pull request as draft October 17, 2023 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants