Code coverage on iOS Using Xcode 4.2 on Lion

流过昼夜 提交于 2019-12-01 21:34:54

On Lion you can symlink the dylib into /usr/lib to avoid that error

sudo ln -s /Developer/usr/lib/libprofile_rt.dylib /usr/lib/libprofile_rt.dylib

I cant guarantee that that wont bust something the future however. Remember that you have done it.

Create a new configuration called "Coverage" (or whatever).

In the Coverage config variant you have created go to Build Settings and...

  • Add -fprofile-arcs and -ftest-coverage to Other C Flags
  • Switch on Generate Test Coverage Files
  • Switch on Instrument Program Flow
  • In Other Linker flags add -lprofile_rt

The reason to do it this way with a config is that you can keep your normal build untouched. Also only works on Simulator. So dont try running this on device.

And from http://mattrajca.com/post/8749868513/llvm-code-coverage-and-xcode-4

Finally, open up the Intermediates/$TARGET.build/$CONFIG/$TARGET.build/Objects-normal/$ARCH subfolder. Inside, you’ll find the aforementioned gcda and gcov files

You can open that folder with CoverStory.

Note that coverage is not cumulative (unlike Coverity) so each run starts afresh. There might be a way to change that.

Update: In Xcode 4.3.2 (4E2002), just switch on Generate Test Coverage Files and Instrument Program Flow, the build system will link libprofile_rt for you. Xcode 4.3, and later, is a standard Mac app and the /Developer folder is gone. So, skip steps 5-7 below. You will also have to create a class file to workaround a bug in Apple's unix implementation as described here. You'll want it in the project under test.

For Xcode 4.2 (4C199) in Snow Leopard:

  1. Create a project, say MyProduct. Check Include Unit Tests.
  2. Once Xcode does its thing and loads the new project, you should have two targets, MyProduct and MyProductTests. Duplicate the MyProduct target.
  3. Select the MyProduct copy target.
  4. Go to Build Settings. Under Code Generation turn on Generate Test Coverage Files and Instrument Program Flow.
  5. Go to Build Phases. Expand Link Binary With Libraries. Click the +. Click Add Other.... Browse to /Developer/usr/lib. Choose libprofile_rt.dylib.
  6. Select the MyProductTests target.
  7. Repeat steps 4 and 5 for that target.
  8. Go to Build Settings again. Find Bundle Loader under Linking. Change the path to the MyProduct copy app. Something like $(BUILT_PRODUCTS_DIR)/MyProduct copy.app/MyProduct copy.
  9. Change your schemes so that the tests run under the MyProduct copy scheme and not MyProduct. If you're compiling clang on your own, you can figure out the details here.

That should work. Step 8 took me hours to figure out, it's the key. If you only see gcda files in the test build directory, that's the likely issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!