Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing in With A Static Library

Tags:

xcode

ios

ocunit

I have an XCode workspace with a user interface project (UI) and a core logic project (Core). I want OCUnit unit tests in the UI project, so I have added a new target for testing, as is commonly done.

I am able to run tests just fine until I put in import statements for classes in the main UI target which in turn reference the Core project.

The error I get is "Lexical or Preprocessor Issue 'xxx.h' file not found". I do not get this message when I build the main UI target directly.

It's as if the main UI target knows about Core when it is built, but when it is referenced from the test target it seems to know nothing about Core.

I took the step of adding a reference to the core project using the "Link Binaries with Libraries" The item in the list remains red. A clue? Maybe, but the red reference in the Link list doesn't keep the UI target from building and using core classes. I also made the main target a dependency of the test target.

like image 470
lipidfish Avatar asked May 16 '12 04:05

lipidfish


2 Answers

Make sure you check out the Apple sample code "Unit Tests": https://developer.apple.com/library/ios/#samplecode/UnitTests/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011742

Make sure your library project is set as a Dependancy in your OCUnit test target's build phases, AND it's linked as a library.

Open up your project in Xcode. In the File menu, go to Project Settings... (or Workspace Settings... if you are using a workspace). Click Advanced... and make sure Unique is checked. Clean and rebuild.

Check your BUILD_PRODUCTS_DIR to see if the headers for your library show up in there. If they don't, first check the build phases in your library target to make sure the headers you need are in the Public section (the Project section may work as well, but try Public and see if that solves your issue).

That covers the most common problems people seem to run into in your situation. When in doubt, check the target settings in the UnitTests sample against yours. Good luck!

like image 97
quellish Avatar answered Nov 12 '22 12:11

quellish


In addition to Jon Reid's answer, I had to do the following as well: In your test target, go to Build Settings. Set "Always Search User Paths" to YES

like image 36
Matthias Avatar answered Nov 12 '22 12:11

Matthias