简体   繁体   中英

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

I'm working with Core Data and, as I the model gets more complex, I need to make sure that the new changes I introduce don't 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, there might be something wrong with my model or at least I know I have to modify some queries in the main code/tests.

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

How can I have a XCTest link against a static library?

This is not only helpful with MagicalRecord/Core Data, but when you're using an external library it's a good idea to have tests to make sure that updates on the library don't break your App.

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

(I will still use a cocoapods library for the instructions, as that's what I'm working with, but the idea is the same if you're not using a cocoapods library)

Once you have created a new Test Target, click on the project root node in the project navigator and select your test target. Go to Build Settings and search for Header Search Paths . Double click on the Header Search Paths item and enter ${SRCROOT}/Pods/Headers and select recursive if you want to import all of your cocoapods libraries headers or enter them individually: ${SRCROOT}/Pods/Headers/MagicalRecord leaving non-recursive selected (although in this case it doesn't really matter).

Now search for Linking and in Other Linker Flags add -ObjC

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

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

Optional: I like to import the headers I know I'm going to use in the -Prefix.pch file. You can go to your 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:

After a lot of fighting, these steps worked for me:

1) Project> Info

On configurations, set the Test Target to share the same Configuration File as your main project (generated by Cocoapods).

在此输入图像描述

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

2) On the Test Target>Build Settings look for Header Search Paths, once there add:

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

The Unit Test framework is inside your Xcode App, this headers will make them public to be added later.

3) On the Test Target> Build Phases add the SenTestingKit.framework

在此输入图像描述

And it should look like this

在此输入图像描述

From there, everything seems to work for me. Good Luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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