Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a framework dependency to a test target?

I have created a framework that contains code useful for testing (a bunch of convenience initializers, for example). Now I would like to import that framework in a test target but there doesn't seem to be a way to do this in Xcode. Just importing it with import Dependency gives me the "Module not found" error when building.

The General tab of the test target does not have any settings for that (or any settings):

enter image description here

The Build Phase tab has a Dependencies section, but the framework I want to add does not show up in the list when I press the + button:

enter image description here

There's also a Link Binary with Libraries section, but when I try to select the framework there Xcode says that file is already being linked (where?):

enter image description here

I can import and use the framework if I add it in the Frameworks, Libraries and Embedded Content section of the main target, but I don't want to do this since the framework is meant to be used only in the test code. I don't see the reason to pollute the namespace with all the extra initializers. I could probably try to check if no XCTest classes are available at runtime and crash, but that seems like a workaround rather than a solution.

Any advice?

P.S. I'm using the latest version of Xcode (11.3.1).

like image 658
Andrii Chernenko Avatar asked Jan 26 '20 17:01

Andrii Chernenko


2 Answers

If you have a source code you are able to use Explicit dependency[About] with cross-project reference

  1. drag and drop dependency project inside a test target
  2. Build Phases -> Dependencies -> Plus(If you didn't include dependency into test target on previous step)
  3. Import

[Mixing Objective-C and Swift]

like image 165
yoAlex5 Avatar answered Oct 06 '22 23:10

yoAlex5


Xcode 12

Drag and drop the framework into folder where tests are located.

Go to YourAppTests target settings: Build phases -> Link Binary With Libraries – here your framework should be listed, if not tap on + button -> Add other (on the bottom) and choose your framework from project's test folder

Done!

PS: most likely works for Xcode 11 as well

like image 1
HotJard Avatar answered Oct 06 '22 22:10

HotJard