DorieTesting.cmake 5.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# Add the unit test build target and test command
add_custom_target(build_unit_tests)
add_custom_target(unit_tests
    COMMAND ctest --output-on-failure --tests-regex ^ut.+$
)

# Add the system test build target and test command
add_custom_target(build_system_tests)
add_custom_target(system_tests
    COMMAND ctest --output-on-failure --exclude-regex ^ut.+$
)

Lukas Riedel's avatar
Lukas Riedel committed
13 14 15 16 17 18 19 20
#
# .. cmake_function:: add_coverage_links
#
#   This function adds the appropriate compiler and linker flags for creating
#   a coverage report from the resulting object files of the specified targets.
#   Signature:
#       `add_coverage_links(<target>...)`
#
21 22 23
function(add_coverage_links)
    foreach(target ${ARGV})
        target_compile_options(${target} PRIVATE --coverage)
Lukas Riedel's avatar
Lukas Riedel committed
24
        target_link_libraries(${target} --coverage)
25 26 27
    endforeach()
endfunction()

Lukas Riedel's avatar
Lukas Riedel committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#
# .. cmake_function:: dorie_add_unit_test
#
#   .. cmake_param:: NAME
#       :single:
#       :required:
#
#       The name of the resulting test executable and target.
#
#   .. cmake_param:: TARGET
#       :single:
#
#       The target this test applies to. This is only required if no SOURCES
#       are specified.
#
#   This function serves as wrapper around the function `dune_add_test` which
#   registers test for existing targets or adds new test executables from the
#   given source files. This function additionally registers the tests as unit
#   tests within DORiE and adds flags for coverage reports. Notice that
#   `dune_add_test` requires more parameters than this function alone.
#
#   Use this function exactly like `dune_add_test`.
#
51 52 53 54 55 56 57
function(dorie_add_unit_test)
    set(SINGLE NAME TARGET)
    cmake_parse_arguments(UNIT_TEST "" "${SINGLE}" "" ${ARGN})

    # use name prefix for test
    if(NOT UNIT_TEST_NAME)
        message(SEND_ERROR "No unit test name specified!")
Lukas Riedel's avatar
Lukas Riedel committed
58
    endif()
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    string(PREPEND UNIT_TEST_NAME ut-)

    # forward to dune function
    dune_add_test(NAME ${UNIT_TEST_NAME}
                  ${UNIT_TEST_UNPARSED_ARGUMENTS})

    # get (build) target name if no target was given
    if(NOT UNIT_TEST_TARGET)
        set(UNIT_TEST_TARGET ${UNIT_TEST_NAME})
    endif()

    # add to build target and employ compile options
    add_coverage_links(${UNIT_TEST_TARGET})
    add_dependencies(build_unit_tests ${UNIT_TEST_TARGET})
endfunction()

Lukas Riedel's avatar
Lukas Riedel committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
#
# .. cmake_function:: dorie_add_metaini_test
#
#   .. cmake_param:: UNIT_TEST
#       :option:
#
#       Registers the created tests as unit tests, including coverage flags.
#       If not specified, the tests are registered as system tests.
#
#   .. cmake_param:: TARGET
#       :single:
#
#       The existing target to apply these tests to. This is incompatible to
#       the option `UNIT_TEST` and the parameter `BASENAME`, because the base
#       name of the tests will automatically be set to the target name.
#
#   .. cmake_param:: METAINI
#       :single:
#       :required:
#
#       The meta-ini _input_ file for this test.
#
#   .. cmake_param:: SCRIPT
#       :single:
#
#       The Python script to call for this test. The script has to be installed
#       into the CMake `virtualenv`. Defaults to `dune_execute.py` for unit
#       tests and `test_dorie.py` for system tests.
#
#   .. cmake_param:: BASENAME
#       :single:
#
#       The basename for tests created from source files. This option is ignored
#       (by `dune_add_system_test`) if `TARGET` is specified.
#
#   .. cmake_param:: CREATED_TARGETS
#       :single:
#
#       The variable to hold the created CMake targets for post-processing.
#
#   This function serves as wrapper around the function `dune_add_system_test`
#   which registers test for existing targets or adds new test executables from
#   the given source files by expanding meta ini files.
#   This function can register the tests as unit or as system tests within
#   DORiE. Notice that `dune_add_system_test` requires more parameters
#   than this function alone.
#
#   Use this function like `dune_add_system_test`, considering the options
#   given above.
#
125 126
function(dorie_add_metaini_test)
    set(OPTIONS UNIT_TEST)
Lukas Riedel's avatar
Lukas Riedel committed
127
    set(SINGLE TARGET METAINI SCRIPT BASENAME CREATED_TARGETS)
128 129 130 131 132
    cmake_parse_arguments(SYSTEM_TEST "${OPTIONS}" "${SINGLE}" "" ${ARGN})

    if(NOT SYSTEM_TEST_METAINI)
        message(SEND_ERROR "No meta ini file given!")
    endif()
Lukas Riedel's avatar
Lukas Riedel committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

    # configure meta ini file or just copy.
    get_filename_component(metaini-name ${SYSTEM_TEST_METAINI} NAME_WE)
    get_filename_component(metaini-extension ${SYSTEM_TEST_METAINI} EXT)
    if(metaini-extension EQUAL ".mini.in")
        configure_file(${SYSTEM_TEST_METAINI} ${metaini-name}.mini)
        set(SYSTEM_TEST_METAINI "${metaini-name}.mini")
    else()
        configure_file(${SYSTEM_TEST_METAINI} ${SYSTEM_TEST_METAINI})
    endif()

    if(SYSTEM_TEST_TARGET AND SYSTEM_TEST_UNIT_TEST)
        message(SEND_ERROR "Specifying TARGET is incompatible to option UNIT_TEST!")
    endif()

    # Set at least the prefix as basename
    if(NOT SYSTEM_TEST_TARGET)
        if(SYSTEM_TEST_UNIT_TEST)
            string(PREPEND SYSTEM_TEST_BASENAME ut-)
        else()
            string(PREPEND SYSTEM_TEST_BASENAME st-)
        endif()
    endif()
156 157 158 159 160 161 162 163 164 165 166

    # default script for system tests
    if(NOT SYSTEM_TEST_SCRIPT)
        if(NOT SYSTEM_TEST_UNIT_TEST)
            set(SYSTEM_TEST_SCRIPT test_dorie.py)
        endif()
    endif()

    # forward to DUNE function
    dune_add_system_test(
        ${SYSTEM_TEST_UNPARSED_ARGUMENTS}
Lukas Riedel's avatar
Lukas Riedel committed
167
        TARGET ${SYSTEM_TEST_TARGET}
168
        BASENAME ${SYSTEM_TEST_BASENAME}
Lukas Riedel's avatar
Lukas Riedel committed
169
        INIFILE ${CMAKE_CURRENT_BINARY_DIR}/${SYSTEM_TEST_METAINI}
170 171 172 173 174 175 176 177 178 179 180 181 182 183
        SCRIPT ${SYSTEM_TEST_SCRIPT}
        CREATED_TARGETS created_targets
    )
    # report created targets to parent scope
    set(${SYSTEM_TEST_CREATED_TARGETS} ${created_targets} PARENT_SCOPE)

    # add dependencies and flags
    if(SYSTEM_TEST_UNIT_TEST)
        add_coverage_links(${created_targets})
        add_dependencies(build_unit_tests ${created_targets})
    else()
        add_dependencies(build_system_tests ${created_targets})
    endif()
endfunction()