xcode 为什么 gcov 不报告我的单元测试涵盖的任何行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1385568/
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 18:49:09  来源:igfitidea点击:

Why doesn't gcov report any lines being covered by my unit tests?

cocoaxcodecode-coverageocunitgcov

提问by

I am using Xcode 3.2 on 10.6, with the shipped version of gcov and default GCC compiler (both version 4.2.1). I have created a dependent Cocoa unit test bundle which is injected into my app, and followed Apple's documentation on setting up a gcov-instrumented build configuration- based on the Debug configuration which doesn't have any compiler optimisations enabled.

我在 10.6 上使用 Xcode 3.2,附带 gcov 版本和默认 GCC 编译器(均为 4.2.1 版)。我创建了一个依赖的 Cocoa 单元测试包,它被注入到我的应用程序中,并按照 Apple 的文档设置了 gcov-instrumented 构建配置- 基于没有启用任何编译器优化的调试配置。

When I build the test bundle with this 'Gcov-instrumented' configuration, the app launches and the tests are injected and run. Also the coverage statistics files are generated at:

当我使用这个“Gcov-instrumented”配置构建测试包时,应用程序启动,测试被注入并运行。此外,覆盖率统计文件在以下位置生成:

build/<AppTarget>.build/Gcov-instrumented/<AppTarget>.build/Objects-normal/x86_64/<object>.gcda

build/<AppTarget>.build/Gcov-instrumented/<AppTarget>.build/Objects-normal/x86_64/<object>.gcno

build/<AppTarget>.build/Gcov-instrumented/<AppTarget>.build/Objects-normal/x86_64/<object>.gcda

build/<AppTarget>.build/Gcov-instrumented/<AppTarget>.build/Objects-normal/x86_64/<object>.gcno

So far so good. I know the tests are really being run because if I insert failures then the test suite fails as expected. Unfortunately, gcov reports that no lines of the objects have been covered by the tests! Every line is reported as 0 coverage. I've searched here and at the Apple mailing list archives, and can't find anyone with a similar problem. I expect I'm missing something - but what is it?

到现在为止还挺好。我知道测试确实在运行,因为如果我插入失败,那么测试套件会按预期失败。不幸的是,gcov 报告说测试没有覆盖对象的任何行!每行都报告为 0 覆盖率。我在这里和 Apple 邮件列表档案中搜索过,但找不到任何有类似问题的人。我想我会遗漏一些东西 - 但它是什么?

采纳答案by danielpunkass

I have been running into this problem intermittently. I stumbled into a good length of lucky time where it was "mostly working" in my main project. By mostly working I mean I was able to get coverage to show up, but with a little more pain than on 10.5. I had to aggressively delete the coverage files and rebuild in order to get any updates, for instance. Even in my "barely working" state, the compiler/runtime doesn't seem willing to overwrite an existing coverage data file.

我一直在间歇性地遇到这个问题。我偶然发现了一段很长的幸运时间,它在我的主要项目中“主要工作”。主要工作是我的意思是我能够得到覆盖显示,但比 10.5 更痛苦。例如,我不得不积极删除覆盖文件并重建以获得任何更新。即使在我“勉强工作”的状态下,编译器/运行时似乎也不愿意覆盖现有的覆盖数据文件。

I think I've stumbled back on to a working scenario. Voodoo for sure, and less than ideal:

我想我已经跌跌撞撞地回到了一个工作场景。巫毒是肯定的,但不太理想:

  1. Make sure the SDK is 10.6.
  2. Make sure the deployment is set to gcc 4.0 (!).
  3. Do a full clean of the project.
  4. Rebuild.
  1. 确保 SDK 是 10.6。
  2. 确保部署设置为 gcc 4.0 (!)。
  3. 彻底清理项目。
  4. 重建。

Does this get you the expected coverage data?

这是否为您提供了预期的覆盖数据?

Granted, I would expect to be able to generate coverage files that work using gcc 4.2 or one of the LLVM compilers, but for the moment this seems to have me slightly less crippled than before.

当然,我希望能够生成使用 gcc 4.2 或 LLVM 编译器之一工作的覆盖文件,但就目前而言,这似乎让我的瘫痪程度比以前少了一些。

Another clue: when I observe the coverage data (using Google's Cover Story), I get warnings like:

另一个线索:当我观察覆盖数据时(使用 Google 的Cover Story),我收到如下警告:

/BUILDRESULTS/MarsEdit.build/Code Coverage/MarsEditDataModelTests.build/Objects-normal/i386/MEDataItem.gcno:version '400A', prefer '402*'

/BUILDRESULTS/MarsEdit.build/Code Coverage/MarsEditDataModelTests.build/Objects-normal/i386/MEDataItem.gcno:version '400A',更喜欢'402*'

But the coverage information is still presented. So it seems it's balking at an older coverage format version, but if I use the newer compiler (4.2) which would presumably generate the newer format .gcnofiles, the 0% coverage problem plagues me as it does Graham.

但仍会显示覆盖信息。因此,它似乎对较旧的覆盖格式版本犹豫不决,但是如果我使用较新的编译器 (4.2),这可能会生成较新的格式.gcno文件,那么 0% 覆盖率问题就会像 Graham 一样困扰着我。

回答by danielpunkass

In Xcode 4.6, coverage reports just seem to work. Turn on "Generate Test Coverage Files" and "Instrument Program Flow" for the app target, run the tests, then point the coverage tool at the output. That's in ~/Library/Developer/Xcode/Derived Data/<Project>-<hash>/Build/Intermediates/<Target>.build/Debug-iphonesimulator/<Target>.build; opening that folder in CoverStory shows me the results I expected.

在 Xcode 4.6 中,覆盖率报告似乎有效。为应用程序目标打开“生成测试覆盖文件”和“检测程序流”,运行测试,然后将覆盖工具指向输出。那是在~/Library/Developer/Xcode/Derived Data/<Project>-<hash>/Build/Intermediates/<Target>.build/Debug-iphonesimulator/<Target>.build; 在 CoverStory 中打开该文件夹会显示我预期的结果。

This GitHub repositorycontains scripts that can automatically discover the output and get lcov to visualise it; I'm happy with CoverStory for now.

这个 GitHub 存储库包含可以自动发现输出并获取 lcov 以对其进行可视化的脚本;我现在对 CoverStory 很满意。

回答by dmaclach

You can get code coverage working with gcc 4.2.1. Details here:

您可以获得使用 gcc 4.2.1 的代码覆盖率。详情在这里:

SnowLeopardGCov

雪豹GCov

回答by Sangavi

I have been trying to get the Code coverage working for iPhone simulator and always get a 0% coverage. Below are the configuration details and the steps that I have tried.

我一直在尝试使代码覆盖率适用于 iPhone 模拟器,并且始终获得 0% 的覆盖率。以下是配置详细信息和我尝试过的步骤。

Configuration

配置

Xcode 3.2.5/iOS 4.1 and iOS 4.2/Mac 10.6/GCC 4.2 Application UICatalog

Xcode 3.2.5/iOS 4.1 和 iOS 4.2/Mac 10.6/GCC 4.2 应用程序 UICatalog

References

参考

http://www.cubiclemuses.com/cm/articles/2009/05/14/coverstory-on-the-iphone/

http://www.cubiclemuses.com/cm/articles/2009/05/14/coverstory-on-the-iphone/

http://developer.apple.com/library/mac/#qa/qa2007/qa1514.html

http://developer.apple.com/library/mac/#qa/qa2007/qa1514.html

Steps

脚步

  • Enable “Generate Test Coverage Files”
  • Enable “Instrument Program Flow”
  • Add “-lgcov” to “Other Linker Flags”
  • UIApplicationExitsOnSuspend flag in Info.plist is set to true
  • 启用“生成测试覆盖文件”
  • 启用“仪器程序流程”
  • 将“-lgcov”添加到“其他链接器标志”
  • Info.plist 中的 UIApplicationExitsOnSuspend 标志设置为 true

Result

结果

I have the .gcda files generated but the coverage always show 0%.

我生成了 .gcda 文件,但覆盖率始终显示为 0%。

Settings tried

设置尝试

  1. Changing GCC to 4.0 and 4.2. When I try to change the GCC to 4.0 I get 26 build errors.
  2. Set environment variables:

    const char *prefix = "GCOV_PREFIX";
    const char *prefixValue = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] cStringUsingEncoding:NSASCIIStringEncoding]; // This gets the filepath to the app's Documents directory
    const char *prefixStrip = "GCOV_PREFIX_STRIP";
    const char *prefixStripValue = "1";
    setenv(prefix, prefixValue, 1); // This sets an environment variable which tells gcov where to put the .gcda files.
    setenv(prefixStrip, prefixStripValue, 1); // This tells gcov to strip the default prefix, and use the filepath that we just declared.)
    
  3. GCC Optimization set to None (-O0) and unchecked the precompiled prefix header file flag.
  1. 将 GCC 更改为 4.0 和 4.2。当我尝试将 GCC 更改为 4.0 时,出现 26 个构建错误。
  2. 设置环境变量:

    const char *prefix = "GCOV_PREFIX";
    const char *prefixValue = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] cStringUsingEncoding:NSASCIIStringEncoding]; // This gets the filepath to the app's Documents directory
    const char *prefixStrip = "GCOV_PREFIX_STRIP";
    const char *prefixStripValue = "1";
    setenv(prefix, prefixValue, 1); // This sets an environment variable which tells gcov where to put the .gcda files.
    setenv(prefixStrip, prefixStripValue, 1); // This tells gcov to strip the default prefix, and use the filepath that we just declared.)
    
  3. GCC 优化设置为无 (-O0) 并取消选中预编译前缀头文件标志。

回答by Vanuan

I was getting 0 coverage when *.gcno files for static library was overwritten by those for shared library.

当静态库的 *.gcno 文件被共享库的文件覆盖时,我的覆盖率为 0。