How to use a static library (e.g. cocoapods library) on XCTest?

I work with Core Data, and as I get more complex, I need to make sure that the new changes that I make do not break my model unexpectedly in other parts.

I can create unit tests and run them every time I change something on my model. If something breaks, maybe something is wrong with my model, or at least I know that I need to change some queries in the main code / test.

I use MagicalRecord to access some convenient methods. I also use cocoapods for the same reason, convenience. The problem is that cocoapods creates a static library and links it to my target, but in Xcode new test targets are not automatically configured to link to the same libraries / frameworks referenced by the target links.

How can I link XCTest to a static library?

This is not only useful with MagicalRecord / Core Data, but when you use an external library, it is nice to have tests to make sure that the updates in the library do not violate your application.

+7
ios objective-c xcode cocoapods magicalrecord
source share
2 answers

If you use cocoapods, you can simply use link_with to enable your test target, but if you're using a static library not created by cocoapods, you can do the following:

(I will continue to use the cocoapods library for instructions, as well as what I am working with, but the idea is the same if you are not using the cocoapods library)

Once you have created a new test object, click on the root of the project node in the project navigator and select the target audience. Go to Build Settings and search for Header Search Paths . Double-click the Header Search Paths element and enter ${SRCROOT}/Pods/Headers and select recursive if you want to import all the headers of the cocoapods libraries or enter them separately: ${SRCROOT}/Pods/Headers/MagicalRecord non-recursive choice (although in in this case, it does not matter much).

Now find Linking and in Other Linker Flags add -ObjC

Now that your test target is still selected, go to Build Phases and in the Link Binary With Libraries click on + and add libPods.a or other libraries individually ( libPods-MagicalRecord.a )

You should be able to run XCTest using a static library.

Optional: I like to import headers that I know I will use in the -Prefix.pch file. You can go to the target test group in the Project Navigator. Go to the Supporting Files group and open the -Prefix.pch file. For MagicalRecord, I like to add:

 #define MR_SHORTHAND #import "CoreData+MagicalRecord.h" 

For more information:

+11
source share

After many battles, these steps worked for me:

1) Project> Information

In configurations, set the Test Target to share the same configuration file as your main project (created by Cocoapods).

enter image description here

Now you should start getting some errors, because the XCUnit framework is missing, but now your external libraries imported with CocoaPod are visible in your test project.

2) In the test target> Build options, find the header search paths by adding:

 $(DEVELOPER_DIR)/Platforms/iPhoneOS.platform/Developer/Library/Frameworks $(DEVELOPER_DIR)/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks 

The Unit Test structure is inside your Xcode application; these headers will make them publicly available later.

3) On the test target> Build Phases, add the SenTestingKit.framework parameter

enter image description here

And it should look like this:

enter image description here

From there, everything works for me. Good luck.

0
source share

All Articles