lundi 5 août 2019

Code coverage ( *.cpp.gcda: CANNOT OPEN) is not working

I want to code coverage with gcov & lcov. But I got a few error.I think My CMakeLists is not providing ".gcda" files or is not finding ".gcda" files.

I tried a lot of solutions. Such as; - I looked at similar projects and rearranged: For example: https://jhbell.com/using-cmake-and-gcov - I changed object output direcory: set(OBJECT_OUTPUT_DIR ${CMAKE_BINARY_DIR}/test/CMakeFiles/*****rpmsgtobetested.dir)

But I didn't solve this problem.

CMAKE_MINIMUM_REQUIRED(VERSION 3.8)

option(COMPILE_TESTS "Compile the tests" OFF)

if(COMPILE_TESTS)

enable_language(C)
enable_language(CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

if(DEFINED ENV{CPPUTEST_HOME})
    message(STATUS "Using CppUTest home: $ENV{CPPUTEST_HOME}")
    set(CPPUTEST_INCLUDE_DIRS $ENV{CPPUTEST_HOME}/include)
    set(CPPUTEST_LIBRARIES $ENV{CPPUTEST_HOME}/lib)
    set(CPPUTEST_LDFLAGS CppUTest CppUTestExt)
else()
    find_package(PkgConfig REQUIRED)
    pkg_search_module(CPPUTEST REQUIRED cpputest>=3.8)
    message(STATUS "Found CppUTest version ${CPPUTEST_VERSION}")
endif()

pkg_check_modules(******rpmsgtest_depends json-c)

add_definitions(-DCPPUTEST_USE_MEM_LEAK_DETECTION=0)
add_definitions("-std=c++11")

# Coverage bilgisi elde etmek için CXX flagları
# 
set(CMAKE_CXX_FLAGS "-g -O0 -Wall -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE OFF)
set(OBJECT_OUTPUT_DIR ${CMAKE_BINARY_DIR}/test/CMakeFiles/******rpmsgtobetested.dir)

# (1) set sources to be tested
set(TARGET_LIBTOBETESTED ******rpmsgtobetested)
set(libtobetested_sources 
    ../src/rpmsg_device.cpp
    ../src/rpmsg_sm_messages.cpp
    ../src/thread_interface.cpp)

add_library(${TARGET_LIBTOBETESTED} ${libtobetested_sources})

# (2) Our unit tests sources
set(TEST_APP_NAME ${TARGET_LIBTOBETESTED}_tests)
set(TEST_SOURCES
    test_rpmsg_device.cpp
    main.cpp
    fake_sdevent.cpp
    fake_syscalls.cpp
)

# (3) Take care of include directories
include_directories(${CPPUTEST_INCLUDE_DIRS} 
                                            ../include 
                                            /usr/include/glib-2.0 
                                            /usr/include/gio-unix-2.0
                                            ${******rpmsgtest_depends_INCLUDE_DIRS})

link_directories(${CPPUTEST_LIBRARIES})
link_libraries(${TARGET_LIBTOBETESTED})

# (4) Build the unit tests objects and link then with the app library
add_executable(${TEST_APP_NAME} ${TEST_SOURCES})

target_compile_options(${TEST_APP_NAME} PUBLIC ${******rpmsgtest_depends_CFLAGS})
target_link_libraries(${TEST_APP_NAME} ${link_libraries} 
                                        ${CPPUTEST_LDFLAGS} 
                                        ${******rpmsgtest_depends_LIBRARIES}
                                        )

# (5) Run the test once the build is done
add_custom_command(TARGET ${TEST_APP_NAME} COMMAND ./${TEST_APP_NAME} POST_BUILD)

############################################################################
################################### LCOV ###################################
############################################################################

add_custom_target(lcov

COMMAND mkdir -p coverage

COMMAND mkdir -p coverage/html

COMMAND ${CMAKE_MAKE_PROGRAM}

WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

add_custom_command(TARGET lcov

COMMAND echo "################# LCOV #################"

COMMAND lcov -c -d ${COVERAGE_DATA_DIR} -o ${CMAKE_BINARY_DIR}/coverage.info

COMMAND lcov --remove ${CMAKE_BINARY_DIR}/coverage.info '**/include/**' -o ${CMAKE_BINARY_DIR}/coverage_filtered.info

COMMAND genhtml -o ${CMAKE_BINARY_DIR}/coverage/html ${CMAKE_BINARY_DIR}/coverage_filtered.info --ignore-errors source

COMMAND echo "-- Coverage report has been output to ${CMAKE_BINARY_DIR}/coverage/html"

WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

add_dependencies(lcov ${TARGET_LIBTOBETESTED})

############################################################################
################################### GCOV ###################################
############################################################################

add_custom_target(gcov

COMMAND mkdir -p coverage

COMMAND ${CMAKE_MAKE_PROGRAM}

WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)

add_custom_command(TARGET gcov

    COMMAND echo "################# GCOV #################"

    COMMAND gcov -b ${libtobetested_sources} -o ${OBJECT_OUTPUT_DIR} | grep -A 5 ".cpp$" > CoverageSummary.tmp

    COMMAND cat CoverageSummary.tmp

    COMMAND echo "-- Coverage files have been output to ${CMAKE_BINARY_DIR}/coverage"

    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/coverage
)

add_dependencies(gcov ${TARGET_LIBTOBETESTED})

else() message(STATUS "Cross compile environment set") endif()

Like below the last of my code output.

TEST(RpmsgDevice_test, create_new_object_with_null_param) - 0 ms TEST(RpmsgDevice_test, create_new_object_cannot_open_device) - 0 ms TEST(RpmsgDevice_test, create_new_object_with_thread) - 0 ms

OK (10 tests, 10 ran, 20 checks, 0 ignored, 0 filtered out, 0 ms)

profiling:/home/msergenergen/Desktop/*****/IP/*****-lib-rpmsg/build-test/test/CMakeFiles/*****rpmsgtobetested.dir//src/rpmsg_sm_messages.cpp.gcda:Cannot open profiling:/home/msergenergen/Desktop/*****/IP/*****-lib-rpmsg/build-test/test/CMakeFiles/*****rpmsgtobetested.dir//src/rpmsg_device.cpp.gcda:Cannot open profiling:/home/msergenergen/Desktop/*****/IP/*****-lib-rpmsg/build-test/test/CMakeFiles/*****rpmsgtobetested_tests.dir/fake_syscalls.cpp.gcda:Cannot open profiling:/home/msergenergen/Desktop/*****/IP/*****-lib-rpmsg/build-test/test/CMakeFiles/*****rpmsgtobetested_tests.dir/fake_sdevent.cpp.gcda:Cannot open profiling:/home/msergenergen/Desktop/*****/IP/******-lib-rpmsg/build-test/test/CMakeFiles/*****rpmsgtobetested_tests.dir/main.cpp.gcda:Cannot open profiling:/home/msergenergen/Desktop/*****/IP/*****-lib-rpmsg/build-test/test/CMakeFiles/*****rpmsgtobetested_tests.dir/test_rpmsg_device.cpp.gcda:Cannot open [100%] Built target *****rpmsgtobetested_tests

Aucun commentaire:

Enregistrer un commentaire