I run the following code with gcovr
, and get 100% line-coverage, despite bar
not being called.
What am I doing wrong?
#include <iostream>
struct A{
static void foo(){
std::cout << "foo" << std::endl;
}
static void bar(){
std::cout << "bar" << std::endl;
}
};
int main() {
A::foo();
// A::bar();
}
The HTML report looks like this:
I use the following CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
set(PROJECT_NAME test_gcovr)
project(${PROJECT_NAME})
if(NOT CMAKE_COMPILER_IS_GNUCXX)
message("must use gnu compiler")
EXIT()
endif()
set(CMAKE_CXX_FLAGS "-g -Wall -O0")
add_executable(run_tests ${PROJECT_SOURCE_DIR}/test.cpp)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeModules)
include(CodeCoverage)
append_coverage_compiler_flags()
setup_target_for_coverage_gcovr_html(NAME run_tests_coverage
EXECUTABLE run_tests
DEPENDENCIES run_tests)
With this CodeCoverage.cmake.
I compile and run with a call to cmake
, followed by make run_tests_coverage
.
EDIT: It appears that in the example above, bar
is not part of the executable, despite -O0
...
I fixed this with:
...
bool x = false;
if (x){
A::bar();
};
...
and now I get a correct coverage report.
Now the question remains - how to check coverage of tests for header-only libraries (without going through the report in detail)?
EDIT #2:
see this Getting useful GCov results for header-only libraries