为什么代码覆盖率报告说我的图书馆没有被覆盖?

时间:2018-01-17 19:25:00

标签: c++ cmake code-coverage

我正在尝试为小型测试库生成代码覆盖率。该库只包含两个文件。

calculator.cpp:

#include "calculator.h"

Calculator::Calculator()
{

}

Calculator::~Calculator()
{

}

void Calculator::addNumbers(int x, int y)
{
  this->result = x + y;
}

calculator.h:

#ifndef CALCULATOR_H
#define CALCULATOR_H
class Calculator
{
public:
  Calculator();
  ~Calculator();
  void addNumbers(int x, int y);
};

#endif

我正在对正在执行的库进行单元测试。它包括图书馆,运行良好。我已将cmake_cxx_flags设置为在顶级CMakeLists.txt中包含-fprofile-arcs和-ftest-coverage。

cmake_minimum_required(VERSION 2.8.11)

set(CMAKE_CXX_FLAGS "-std=c++11")

include_directories("${PROJECT_BINARY_DIR}")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 --coverage")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 --coverage -ftest-coverage -fprofile-arcs")

add_subdirectory(src)
add_subdirectory(test)

我正在使用脚本获取在构建时生成的gcno和gcda文件,以生成人类可读的报告。

#!/bin/bash

OUTPUT_DIR="$1/bin/ExecutableTests/Coverage"
mkdir -p "$OUTPUT_DIR"

component=Calculator
dependency=calculator
script=test_calculator.sh
unit_test=unit_test_calculator

mkdir $OUTPUT_DIR/$component

cd "$1"/bin/

make clean || exit 1
make || exit 1
# Create baseline coverage
lcov -c -i -d "$1"/bin/src/"$component"/CMakeFiles/"$dependency".dir -o "$1/Coverage/$component"_"$dependency".coverage.base
# Run the test
$1/scripts/$script $1
# Create test coverage
lcov -c -d "$1"/bin/test/$component/CMakeFiles/"$unit_test".dir -o "$1/Coverage/$component"_"$dependency".coverage.run

lcov -d "$1/test/$component" -a "$1/Coverage/$component"_"$dependency".coverage.base -a "$1/Coverage/$component"_"$dependency".coverage.run -o "$1/Coverage/$component"_"$dependency".coverage.total

genhtml --branch-coverage -o "$OUTPUT_DIR/$component" "$1/Coverage/$component"_"$dependency".coverage.total
rm -f "$1/Coverage/$component"_"$dependency".coverage.base "$1/Coverage/$component"_"$dependency".coverage.run "$1/Coverage/$component"_"$dependency".coverage.total

我可以看到正在为库生成数据文件;但是,当我查看从脚本生成的报告时,它显示从未触及过库。如我的单元测试所示,这显然是错误的。

#include "lest_basic.hpp"
#include "calculator.h"
#include <memory>
#include <iostream>

int addResult(int x, int y)
{
  Calculator calc1;
  return calc1.addNumbers(x, y);
}

const lest::test specification[] =
{

  CASE( "Addition" )
  {
    std::cout << "Starting Addition testing..." << std::endl;
    std::cout << "Adding 1 and 2..." << std::endl;
    EXPECT(addResult(1, 2) == 3);
    std::cout << "Adding 2 and 8..." << std::endl;
    EXPECT(addResult(2, 8) > 1);
    std::cout << "Adding 7 and 4..." << std::endl;
    EXPECT(addResult(7, 4) < 12);
  },
};

int main(int argc, char **argv) {

  return lest::run( specification );
}

以下是我的单元测试的CMakeLists.txt:

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/ExecutableTests/)
include_directories(${CMAKE_SOURCE_DIR}/externalinclude/)
include_directories(${CMAKE_SOURCE_DIR}/src/Calculator/)
add_executable (unit_test_calculator test_calculator.cpp)
target_link_libraries(unit_test_calculator -Wl,--whole-archive calculator -Wl,--no-whole-archive)

我的问题是为什么报告说图书馆代码没有被涵盖?数据文件是问题吗?

1 个答案:

答案 0 :(得分:0)

我已解决了我的问题。问题是我在运行单元测试后没有对源代码运行lcov。我的目录错了。

lcov -c -d "$1"/bin/test/$component/CMakeFiles/"$unit_test".dir -o "$1/Coverage/$component"_"$dependency".coverage.run

需要:

lcov -c -d "$1"/bin/src/$component/CMakeFiles/"$dependency".dir -o "$1/Coverage/$component"_"$dependency".coverage.run