douxu5845 2018-05-08 08:15
浏览 141
已采纳

如何获得文件夹的全局测试覆盖率?

When showing code coverage, go test show code coverage for each package (in percentage).

Is there a way to show a summary for a folder that is taking all subfolder (subpackage) into account?

What I want is a global code coverage percentage for the full project, one number that show code coverage of the folder and all subfolders.

  • 写回答

2条回答 默认 最新

  • dougu1952 2018-05-08 16:25
    关注

    I found a solution to my problem.

    I first run the test on all package and store the test result in a file :

    go test --coverprofile=coverage.out ./...
    

    I then run a bash script to calculate my result

    #!/usr/bin/env bash
    
    covered=0
    total=0
    while IFS='' read -r line || [[ -n "$line" ]]; do
        IFS=' ' read -r -a array <<< "$line"
        total=$(($total+${array[1]}))
        if [ "${array[2]}" = "1" ]; then
            covered=$(($covered+${array[1]}))
        fi 
    done < "$1"
    echo $(awk "BEGIN { pc=100*${covered}/${total}; i=int(pc); print (pc-i<0.5)?i:i+1 }")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?