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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5852002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:57:26  来源:igfitidea点击:

How do I get code coverage for Xcode for my Objective-C app?

iphoneobjective-cxcodeunit-testingcode-coverage

提问by aryaxt

I need code coverage for my iPhone app.

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

How do I get code coverage for Xcode 4?

如何获得 Xcode 4 的代码覆盖率?

回答by user706638

These steps will help.

这些步骤会有所帮助。

  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

  1. 创建一个新的构建配置(“Coverage”),从“调试”配置复制。

  2. 打开主目标的构建设置,确保选择了新配置,然后:

    Enable “Generate Test Coverage Files”
    Enable “Instrument Program Flow”
    Add “-lgcov” to “Other Linker Flags”
    
  3. 使用 Coverage 模式编译应用程序。

  4. 检查应用程序包文件夹中的 .gcno 文件。

    覆盖范围-iphonesimulator/applicationname.build/Objects-normal

    使用 CoverStory 打开 .gcno 文件。从http://code.google.com/p/coverstory/downloads/list下载 CoverStory

Reference Sites

参考站点

回答by zekel

I couldn't find a good example of this, so hopefully this will help someone else.

我找不到一个很好的例子,所以希望这会帮助别人。

If you want to generate HTML from your code coverage (once you get your .gcda files generated), you can install lcovand use these commands:

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

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

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