8

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).

Andrii Chernenko
  • 8,920
  • 6
  • 65
  • 83
  • If you happen to know any good articles or videos on the topic, feel free to share. – Andrii Chernenko Jan 26 '20 at 17:45
  • I ran into this as well. I'm curious, are you trying to import a framework that has both ObjC and Swift, and is BUILD_LIBRARY_FOR_DISTRIBUTION set to YES? I suspect that such frameworks are not currently useable, because the new .swiftinterface file (Swift 5 "Module Stability") is conflicting with the modulemap for the ObjC. – Richard Venable Feb 26 '20 at 20:27
  • Hey! Were you able to find a solution for this? – learner Jun 06 '20 at 16:52
  • 1
    @learner, no, I wasn't. I'll post an anwer if I find a solution. – Andrii Chernenko Jun 08 '20 at 07:55
  • Have you tried adding @testable annotation to the import Dependency statement? – learner Jun 09 '20 at 05:05
  • 1
    @learner As far as I remember, it didn't make any difference, the module was not available either way (which makes sense since the containing framework is not linked). – Andrii Chernenko Jun 09 '20 at 07:34

2 Answers2

0

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

HotJard
  • 3,910
  • 2
  • 31
  • 31
0

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]

yoAlex5
  • 13,571
  • 5
  • 105
  • 98