简体   繁体   English

如何在Xcode 4中获得测试和构建iPhone的代码覆盖率?

[英]How do I get code coverage for tests and build for iPhone in Xcode 4?

I have a couple of projects with unit tests where I want to measure coverage. 我有几个单元测试的项目,我想测量覆盖率。 So I follow the instructions detailed in the How-to for CoverStory and elsewhere on the web. 因此,我按照如何使用CoverStory和网络上的其他内容详细说明。

I add -fprofile-arcs and -ftest-coverage to my test target and the target under test and link to /Developer/usr/lib/libprofile_rt.dylib as specified. 我将-fprofile-arcs-ftest-coverage到我的测试目标和测试中的目标,并链接到指定的/Developer/usr/lib/libprofile_rt.dylib

And it works! 它的工作原理! When I run the tests scheme under the iPhone simulator, I get my code coverage, my tests and app build and run. 当我在iPhone模拟器下运行测试方案时,我得到了我的代码覆盖率,我的测试和应用程序构建和运行。

But then, when I go back to the Run scheme on actual iPhone hardware, I get a link error for libprofile_rt.dylib. 但是,当我回到实际iPhone硬件上的Run方案时,我发现了libprofile_rt.dylib的链接错误。 Specifically, I get 具体来说,我明白了

ld: library not found for -lprofile_rt ld:找不到-lprofile_rt的库

What I suspect here is that libprofile_rt is actually a library for Mac hardware and the iPhone obviously can't link to a binary built for Mac, in general. 我怀疑这里的libprofile_rt实际上是一个用于Mac硬件的库,iPhone显然无法链接到为Mac构建的二进制文件。

My question is How do run on iPhone hardware and get my code coverage? 我的问题是如何在iPhone硬件上运行获得我的代码覆盖率?

I suspect I can do this with a copy of my Debug target that is only ever used for testing, but that has the additional trouble of maintaining yet another target. 我怀疑我可以使用仅用于测试的Debug目标副本来执行此操作,但这还有另外一个维护目标的麻烦。 Is there any other way to get this working? 有没有其他方法让这个工作? Am I doing something silly? 我做傻事吗?

might a little bit late but actually you can. 可能有点迟,但实际上你可以。 it is explained in this presentation , slides 32 to 34. 本演示文稿中介绍了幻灯片32至34。

took me a fair amount of time to find this hidden gem. 花了我相当长的时间才找到这个隐藏的宝石。 hope this might help future visitor of this page. 希望这可能有助于此页面的未来访客。

The answer is: you can't run on your device AND get coverage at the same time. 答案是: 您无法在设备上运行并同时获得保险。 The only way to get coverage is on the simulator: make a copy of your debug target, setup coverage and what not on that target and run that target, with your tests, in the simulator. 获得覆盖的唯一方法是在模拟器上:制作调试目标的副本,设置覆盖范围以及不在该目标上的内容,并在模拟器中使用测试运行该目标。

I go through all the details in an answer to this question on stackoverflow: Code coverage on iOS Using Xcode 4.2 on Lion 我在stackoverflow上回答了这个问题的所有细节: iOS上的代码覆盖率在Lion上使用Xcode 4.2

Possible answer to this problem is here: https://stackoverflow.com/a/5140459 这个问题的可能答案如下: https//stackoverflow.com/a/5140459

The trick is to get the *.gcda files out of the App's Documents directory via Xcode organizer, since they will, of course, be generated on device. 诀窍是通过Xcode组织器将* .gcda文件从App的Documents目录中取出,因为它们当然会在设备上生成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM