2017-07-06 184 views
0

我正試圖在我的GitHub倉庫上配置CodeCov和TravisCI。由於我的回購是在C++中,我使用的CMake了,我基本上是複製粘貼的this exampleafter_success標籤到我.travis.yml文件:如何在TravisCI中爲C++ w/CMake項目正確配置CodeCov?

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文件提供了USB Key和通知設置,但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/******** 

回答

1

問題在於Cmake編譯器和鏈接器標誌。爲了讓GCov(GCC的一部分,並由lcov使用)提供分析信息和覆蓋測試,必須設置一些標誌。

Code Coverage for C Unit Tests

具體來說,需要編譯器添加分析信息,以目標代碼,它是通過將標誌-fprofile-arcs-ftest-coverage到你的編譯命令來完成。

第一個標誌將邏輯添加到目標代碼以生成通用分析信息。這是關於每個基本塊被執行的頻率的信息。當你的程序運行時,所有這些信息將被保存到一個新的文件中,擴展名爲.da,位於.o文件旁邊。該文件中的數據不是覆蓋範圍特定的,但它將被gcov使用。

您傳遞給GCC的第二個標誌-ftest-coverage也將爲您的對象文件添加邏輯。這一次,目標是輸出特定於覆蓋範圍的信息。有兩個文件將被生成,一個.bb和一個.bbg。 .bb文件是從基本塊到行號的簡單映射文件。 .bbg文件列出了執行應用程序時運行的相應源文件中的每個弧。這個數據被gcov用來重建實際的程序流程圖,從中可以計算出所有的基本塊和弧的執行計數。

此外,來源需要與-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選項編譯的庫鏈接,則仍然需要添加鏈接器標誌。

+1

GCC的較新版本可以使用 - 覆蓋一切。請參閱https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html。 –

相關問題