Commit 43bf6cab authored by Lukas Riedel's avatar Lukas Riedel

Merge branch '163-add-cmake-option-to-enable-coverage-on-unit-tests' into 'master'

Resolve "Add CMake option to enable coverage on unit tests"

Closes #163

See merge request !172
parents a1bfe8b5 f987667f
variables:
DUNE_CONTROL_PATH: /opt/dune:$CI_PROJECT_DIR
DUNE_DIR: /opt/dune
DUNE_CONTROL_PATH: $DUNE_DIR:$CI_PROJECT_DIR
DUNECONTROL: ./dune-common/bin/dunecontrol
CPUS_MULTICORE: 4
CPUS_DIND: 2
......@@ -30,8 +31,9 @@ variables:
image: $DUNE_ENV_IMAGE
# Move into top-level DUNE directory
before_script:
- cd /opt/dune
- cd $DUNE_DIR
# Provide SSH access via the SSH_PRIVATE_KEY and SSH_KNOWN_HOSTS variables
.ssh-access: &ssh_access
......@@ -123,7 +125,7 @@ prep:update-dune-clang:
build:system-tests: &build-tests
stage: build
script:
- CMAKE_FLAGS="$CMAKE_FLAGS"
- CMAKE_FLAGS="$CMAKE_FLAGS -DCOVERAGE_REPORT=On"
$DUNECONTROL --only=dorie configure
- $DUNECONTROL --only=dorie make $MAKE_FLAGS dorie-rfg
- $DUNECONTROL --only=dorie make $MAKE_FLAGS build_system_tests
......@@ -136,20 +138,22 @@ build:system-tests: &build-tests
build:unit-tests:
<<: *build-tests
script:
- CMAKE_FLAGS="$CMAKE_FLAGS
-DCMAKE_BUILD_TYPE=Debug"
- CMAKE_FLAGS="$CMAKE_FLAGS -DCOVERAGE_REPORT=On
-DCMAKE_BUILD_TYPE=None"
$DUNECONTROL --only=dorie configure
- $DUNECONTROL --only=dorie make $MAKE_FLAGS build_unit_tests
build:debug: &debug
stage: build
allow_failure: true
script:
before_script:
- cd $DUNE_DIR
- CMAKE_FLAGS="
$CMAKE_FLAGS
-DCMAKE_BUILD_TYPE=Debug
-DCMAKE_CXX_FLAGS_DEBUG='-Werror'"
$DUNECONTROL --only=dorie configure
script:
- $DUNECONTROL --only=dorie make $MAKE_FLAGS dorie-rfg
- $DUNECONTROL --only=dorie make $MAKE_FLAGS build_unit_tests
- $DUNECONTROL --only=dorie make $MAKE_FLAGS richards_d2_r1 transport_d2_r0_t0
......@@ -157,7 +161,9 @@ build:debug: &debug
build:debug-clang:
<<: *debug
image: ${DUNE_ENV_IMAGE}-clang
script:
before_script:
- cd $DUNE_DIR
# Choose Clang compiler explicitly
- CMAKE_FLAGS="
$CMAKE_FLAGS
-DCMAKE_BUILD_TYPE=Debug
......@@ -165,9 +171,6 @@ build:debug-clang:
-DCMAKE_CXX_COMPILER=clang++
-DCMAKE_CXX_FLAGS_DEBUG='-Werror'"
$DUNECONTROL --only=dorie configure
- $DUNECONTROL --only=dorie make $MAKE_FLAGS dorie-rfg
- $DUNECONTROL --only=dorie make $MAKE_FLAGS build_unit_tests
- $DUNECONTROL --only=dorie make $MAKE_FLAGS transport_d2_r0_t0 transport_d2_r1_t0
build:docs:
stage: build
......@@ -186,50 +189,53 @@ test:system-tests: &test
stage: test
dependencies:
- build:system-tests
needs: ["build:system-tests"]
variables:
GCOVR_PATHS:
--root $CI_PROJECT_DIR
--exclude $CI_PROJECT_DIR/dune/dorie/test
--exclude $CI_PROJECT_DIR/plugins
script:
- $DUNECONTROL --only=dorie configure
- $DUNECONTROL --only=dorie make system_tests
# Run coverage utility (will also run after failed job)
after_script:
- source $CI_PROJECT_DIR/build-cmake/activate
- pip3 install gcovr
- cd $CI_PROJECT_DIR/build-cmake
- mkdir coverage
- gcovr $GCOVR_PATHS --html --html-details -o coverage/report.html
# run again for coverage report in GitLab
- gcovr $GCOVR_PATHS
coverage: '/^TOTAL.*\s+(\d+\%)$/'
artifacts:
name: "$CI_JOB_NAME"
paths:
- $CI_PROJECT_DIR/build-cmake/test
- $CI_PROJECT_DIR/build-cmake/coverage
expire_in: 1 day
test:unit-tests:
<<: *test
dependencies:
- build:unit-tests
needs: ["build:unit-tests"]
script:
# install coverage reporter
- $DUNECONTROL --only=dorie bexec
"./run-in-dune-env python3 -m pip install gcovr"
# perform the tests
- $DUNECONTROL --only=dorie configure
- $DUNECONTROL --only=dorie make unit_tests
# report coverage
- cd $CI_PROJECT_DIR/build-cmake
- mkdir -p dune/dorie/test/coverage
- ./run-in-dune-env gcovr
--root ../
--exclude ../dune/dorie/test
--exclude ../plugins
--html --html-details
-o dune/dorie/test/coverage/coverage.html
# run again for coverage report in GitLab
- ./run-in-dune-env gcovr
--root ../
--exclude ../dune/dorie/test
--exclude ../plugins
coverage: '/^TOTAL.*\s+(\d+\%)$/'
artifacts:
name: "$CI_JOB_NAME"
paths:
- $CI_PROJECT_DIR/build-cmake/dune/dorie/test
- $CI_PROJECT_DIR/build-cmake/coverage
expire_in: 1 day
test:python-tests:
<<: *test
dependencies: []
# TODO: Make empty once https://gitlab.com/gitlab-org/gitlab/issues/30631
# is resolved
needs: ["build:docs"] # Any quick job from previous stage
script:
- $DUNECONTROL --only=dorie configure
- $DUNECONTROL --only=dorie make test_python
......
......@@ -32,6 +32,7 @@
* ParaView tutorial for analyzing DORiE output files !173
* Parameter XML files support tags for version changes !171
* Unit test for Parameter XML file parser !171
* CMake option to enable code coverage flags on all targets !172
### Changed
* Data structures for storing and accessing parameter information !55
......@@ -57,6 +58,7 @@
* `SimulationBase` unit test now uses Google Test !159
* Deploy online documentation for each branch to private server !163
* Use YAML (instead of muPhi `.bcdat`) files for specifying BCs !121
* Unit and system tests report code coverage separately !172
### Fixed
* Allow meta-ini files for unit tests !101
......
cmake_minimum_required(VERSION 3.10)
project(dorie C CXX)
# Configuration options
option(COVERAGE_REPORT "Enable coverage compiler flags")
# set build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_BUILD_TYPE "Release")
endif()
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UPPER)
if(CMAKE_BUILD_TYPE_UPPER MATCHES DEBUG)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_VERBOSE_MAKEFILE ON)
endif()
# add extra flags to debug compiler flags
......
......@@ -432,9 +432,15 @@ Additionaly, there is a set of tests for the Python module.
The `make` commands are to be executed from within the `build-cmake` directory.
The unit tests automatically include compiler flags for code coverage reports.
After building them with `Debug` flags and executing them, you can
retrieve code coverage information using the
#### Code Coverage Report
To enable code coverage reports, configure DORiE with the CMake option
`COVERAGE_REPORT` enabled, like so (from the `build-cmake` directory):
cmake -DCOVERAGE_REPORT=On ..
This will add the appropriate compiler flags to _all_ targets. You then have to
re-build all binaries. After running tests or executing the application, you
can retrieve code coverage information using the
[`gcovr`](https://gcovr.com/index.html) utility.
### Further Help
......
......@@ -100,6 +100,12 @@ function(dorie_compile_instance)
add_executable(${exe_name} EXCLUDE_FROM_ALL ${src_file})
target_link_libraries(${exe_name} PUBLIC ${lib_name})
# Coverage links if enabled
if(COVERAGE_REPORT)
target_compile_options(${exe_name} PUBLIC --coverage)
target_link_libraries(${exe_name} PUBLIC --coverage)
endif()
# Add the executable to the default targets
if ((ARGS_RORDER LESS_EQUAL DORIE_MAX_DEFAULT_RORDER_${ARGS_DIMENSION})
AND ((NOT ARGS_TORDER)
......
......@@ -100,6 +100,10 @@ function(dorie_add_unit_test)
GTEST
)
if(COVERAGE_REPORT)
add_coverage_links(${UNIT_TEST_TARGET})
endif()
if (UNIT_TEST_CUSTOM_MAIN)
target_link_libraries(${UNIT_TEST_TARGET} gtest)
else ()
......@@ -226,15 +230,20 @@ function(dorie_add_metaini_test)
# report created targets to parent scope
set(${SYSTEM_TEST_CREATED_TARGETS} ${created_targets} PARENT_SCOPE)
# Link to dependencies
# Set properties for new target
if(NOT SYSTEM_TEST_TARGET)
# Link to dependencies
target_link_libraries(${created_targets}
muparser::muparser hdf5 yaml-cpp spdlog)
# Add coverage flags if enabled
if(COVERAGE_REPORT)
add_coverage_links(${created_targets})
endif()
endif()
# add dependencies and flags
if(SYSTEM_TEST_UNIT_TEST)
# add_coverage_links(${created_targets})
add_dependencies(build_unit_tests ${created_targets})
target_compile_definitions(${created_targets}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment