如何为我的Objective-C应用程序在Xcode中获得代码覆盖率?

22

我需要为我的iPhone应用程序获取代码覆盖率。

我如何在Xcode 4中获取代码覆盖率?


Clang/LLVM 不支持 gcov,也不提供代码覆盖率功能。您可能可以在使用 GCC 构建程序的情况下使用 gcov。请参阅LLVM 的此错误报告,并考虑提交功能请求雷达 - user557219
您已在info.plist中将UIApplicationExitsOnSusspend设置为YES。 - aryaxt
1
@Bavarious:根据您的错误报告,clang现在支持覆盖率了? - Johannes Rudolph
1
@Joh Yup,nlewycky已经在LLVM主干中添加了gcov支持,而且不,这个错误报告不是我的。;-) 我不确定苹果公司是否会通过Xcode提供它,但如果您在本地构建LLVM,则可能可以使用它。 - user557219
2个回答

9
这些步骤会有所帮助。
  1. Create a new build configuration (‘Coverage’), duplicated from the ‘Debug’ configuration.

  2. Open up build settings for the main target, make sure your new configuration is selected, and:

    Enable “Generate Test Coverage Files”
    Enable “Instrument Program Flow”
    Add “-lgcov” to “Other Linker Flags”
    
  3. Compile application with Coverage mode.

  4. Check .gcno files from your application bundle folder.

    Coverage-iphonesimulator/applicationname.build/Objects-normal

    open .gcno files with CoverStory. Download CoverStory from
    http://code.google.com/p/coverstory/downloads/list

参考网站

这里是一些与Xcode相关的网站,涵盖了单元测试和代码覆盖率。

3

我找不到一个好的例子,希望这个能帮助其他人。

如果你想要从你的代码覆盖率生成HTML(一旦你生成了你的.gcda文件),你可以安装lcov并使用以下命令:

function generate-codecoverage-html() {
    if [[ $1 == "-h" || ! $# -eq 2 ]]; then
        echo "    usage: $0 path/to/codecoverage/dir/ path/to/htmldir/"
        return
    fi

    timestamp=$(date)
    tmpfile="/tmp/codecoverage.info-$date"
    lcov --no-checksum --directory "$1" --capture --output-file "$tmpfile"
    genhtml --output-directory  "$2" "$tmpfile"
}

“lcov”在哪里? - AnneTheAgile
找到了;https://github.com/linux-test-project/lcov,在https://github.com/jonreid/XcodeCoverage中有引用。 - AnneTheAgile

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接