我试图使用TravisCI在GitHub存储库上配置CodeCov。由于我的存储库是C++,并且已经使用了CMake,因此我基本上将this exampleafter_success标签复制粘贴到了 .travis.yml 文件中:

after_success:
    # Creating report
  - cd ${TRAVIS_BUILD_DIR}
  - lcov --directory . --capture --output-file coverage.info coverage info
  - lcov --remove coverage.info '/usr/*' --output-file coverage.info
  - lcov --list coverage.info
  # Uploading report to CodeCov
  - bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports"

我推送了一个自定义 .codecov.yml 文件以提供我的 token 和通知设置,但是after_success只是示例中建议的一个。我真的不熟悉lcov,所以我不知道是否还有其他缺失或这些调用的目的。

查看CodeCov的文档,我找不到可能会丢失的内容,但是覆盖率报告为空,因为lcov找不到一些神秘的.gcda文件(我想)。 CodeCov的文档在任何地方都没有提到它,因此我想我在配置中缺少了重要的一步(或者可能由于某种原因找不到文件...?)

这是TravisCI的after_success阶段的输出:
$ lcov --directory . --capture --output-file coverage.info
Capturing coverage data from .
Found gcov version: 4.8.4
Scanning . for .gcda files ...
geninfo: ERROR: no .gcda files found in .!

$ lcov --remove coverage.info '/usr/*' --output-file coverage.info
Reading tracefile coverage.info
lcov: ERROR: no valid records found in tracefile coverage.info

$ lcov --list coverage.info
Reading tracefile coverage.info
lcov: ERROR: no valid records found in tracefile coverage.info

$ bash <(curl -s https://codecov.io/bash) || \
  echo "Codecov did not collect coverage reports"
  _____          _
 / ____|        | |
| |     ___   __| | ___  ___ _____   __
| |    / _ \ / _` |/ _ \/ __/ _ \ \ / /
| |___| (_) | (_| |  __/ (_| (_) \ V /
 \_____\___/ \__,_|\___|\___\___/ \_/
                              Bash-8fda091
==> Travis CI detected.
    project root: .
--> token set from env
    ...
==> Running gcov in . (disable via -X gcov)
==> Python coveragepy not found
==> Searching for coverage reports in:
    + .
    -> Found 3 reports
==> Detecting git/mercurial file structure
==> Appending build variables
    + TRAVIS_OS_NAME
==> Reading reports
    - Skipping empty file ./coverage.info
    + ./doc/****.zip bytes=337165           -----> That's not a report.
    + ./doc/****.pdf bytes=353614           -----> That's not a report.
==> Appending adjustments
    http://docs.codecov.io/docs/fixing-reports
    + Found adjustments
==> Uploading reports
    url: https://codecov.io
    query: branch=codecov&commit=*****...
    -> Pinging Codecov
    -> Uploading to S3 https://codecov.s3.amazonaws.com
    -> View reports at https://codecov.io/github/********

最佳答案

问题出在Cmake编译器和链接器标志上。为了使GCov(GCC的一部分,被lcov使用)提供性能分析信息和覆盖率测试,必须设置一些标志。
来自Code Coverage for C Unit Tests:

同样,源需要与-lgcov --coverage链接。就我而言,由于我使用的是Cmake,因此需要使用set_target_properties函数指定它们:

add_executable(dss-sim dss-sim.cpp)
target_link_libraries(dss-sim
    list
    of
    my
    static
    libs
)
# The libs above also need to be compiled with the same flags.
set_target_properties(dss-sim
    PROPERTIES
    COMPILE_FLAGS "-ftest-coverage -fprofile-arcs"
    LINK_FLAGS    "-lgcov --coverage"
)
最后,由于通常不希望将单元测试包括在覆盖率报告中,因此您将而不是为其定义编译器标志。但是请注意,如果将单元测试与使用gcov选项编译的库链接,则仍然需要添加链接器标志。

关于c++ - 如何在TravisCI中为带有CMake项目的C++正确配置CodeCov?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44955539/

10-15 02:43