Unit testing in a static library

I have an Xcode workspace with a user interface (UI) project and a core logic project (Core). I want to run OCUnit unit tests in a user interface project, so I added a new test target, as is usually done.

I can run the tests very well until I put in the import statements the classes in the main target of the user interface, which in turn refer to the main project.

The error I get is the "Lexical or preprocessing release" xxx.h "file not found." I do not receive this message when I directly create the main user interface object.

It is as if the main user interface object knows about Core when it is built, but when it refers to a test object, it seems to know nothing about Core.

I took the step of adding a link to the main project using Linking Binary Files to Libraries. The item in the list remains red. Key? Perhaps, but the red link in the list of links does not prevent the creation of a user interface and uses the main classes. I also made the main goal the dependence of the test goal.

+6
source share
3 answers

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

Make sure your library project is defined as a dependency in the phases of your target OCUnit testing phase, and it is linked as a library.

Open the 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 the Unique check box is selected. Clean and restore.

Check your BUILD_PRODUCTS_DIR to see if it has headers for your library. If they don’t do this, first check the build phases in your target library to make sure that the headers you need are in the “Publish” section (the “Project” section may also work, but try “Publish” and see if it allows it's your problem).

This covers the most common problems that people face in your situation. If in doubt, check the target settings in the UnitTests sample against yours. Good luck

+9
source

In addition to John Reid’s answer, I also had to do the following: For the test purpose, go to the “Parameter Settings” section. Set "Always search for user paths" to YES

+2
source

For the test goal, go to the "Build Settings" section. Add the path to the headers of the static library in the header search path.

0
source

Source: https://habr.com/ru/post/915871/


All Articles