88

I have a few projects I'm trying to build with Xcode 6 Beta 2. The projects all have some type of library that uses XCTest (Kiwi/XCTest and Specta) that don't build in Xcode 6 because XCTest/XCTest.h cannot be found.

fatal error: 'XCTest/XCTest.h' file not found
#import <XCTest/XCTest.h>

I noticed that XCTest.framework is no longer in the "Link Libraries with Binaries" build phase list, but that's fine because when I create a new project with Xcode 6 it appears the library is linked in automatically.

Perhaps of some relevency, my XCTest-needing dependencies are all brought in via Cocoapods.

Is there anything I'm unaware of that I need to update with my project?

Erik Kerber
  • 5,646
  • 7
  • 38
  • 56

11 Answers11

143

Note: This may not be needed for any projects created in Xcode 7.

CocoaPods had a fix for this here and here


In order to fix this for any CocoaPod dependencies you need to add the following to FRAMEWORK_SEARCH_PATHS in any Pod target that requires XCTest (e.g. Kiwi, Specta, FBSnapshotTestCase, etc).

$(PLATFORM_DIR)/Developer/Library/Frameworks

screenshot

This will allow you to reference XCTest in any dependencies you may have. This may be fixed in a future update of CocoaPods, or the Pod you are referencing, so you may want to remove it later.

It is not detrimental to earlier versions of Xcode so should be safe to use.

squarefrog
  • 4,750
  • 4
  • 35
  • 64
  • Thank you squarefrog. The relevant issue is [here](https://github.com/CocoaPods/CocoaPods/issues/2296) – fatuhoku Jul 28 '14 at 22:50
  • 1
    When is the next release scheduled? – fatuhoku Jul 29 '14 at 08:18
  • When it's ready :) If you are feeling brave you can always build it from source. This process is a little easier with [Rainforest](https://github.com/CocoaPods/Rainforest). – squarefrog Jul 30 '14 at 11:41
  • Yes that is what I ended up doing — I just installed the cutting-edge dev version of Cocoapods and aliased it to `pod-dev`. I use that to generate xcworkspace file now. – fatuhoku Jul 30 '14 at 12:38
  • See my answer, which doesn't require modifying Pod files or other recurring changes and works with older CocoaPod versions. – Steven Kramer Sep 08 '14 at 11:36
  • 6
    This does not seem to work with KIM in xcode 6 GM..? – Thomas Clowes Sep 14 '14 at 09:26
  • Me again. Our project refuses to run tests using this method unless we add the symlink, as I described in my answer. The error we get is `The bundle “Tests.octest” couldn’t be loaded because it is damaged or missing necessary resources. DevToolsBundleInjection environment: XCInjectDiagnostics:` – Steven Kramer Sep 25 '14 at 08:00
  • @StevenKramer I don't know what to suggest to you. All my tests in various project work fine using this method and Xcode 6.0.1. – squarefrog Sep 25 '14 at 09:00
  • Sure, no problem. Our project is large-ish and undoubtedly has some intricacies. I'm glad the problem is solved and if others run into the same problem we had, they can find the answer here now. – Steven Kramer Sep 25 '14 at 20:36
  • Thanks! I'm fixing this issue with [HTPressableButtons](https://github.com/herinkc/HTPressableButton) - @squarefrog remember us? :P – Thanakron Tandavas Oct 21 '14 at 16:25
  • @ThanakronTandavas of course I do - hope you're both well. – squarefrog Oct 22 '14 at 08:35
  • Still relevant for XCode 6.4. Thanks for saving me hours of frustration – mindbomb Jul 15 '15 at 23:55
  • You are awesome!! Had been banging my head over this!! Thanks!! – Divya Sep 28 '15 at 17:28
  • 3
    I disagree with "not relevant for Xcode 7". I had the problem in Xcode 7.1. The problem disappeared after I added that framework search path as directed above. I presume that a project started in Xcode 7 will not have the issue, but the question here is to build older projects in newer Xcodes. – Jerry Krinock Dec 08 '15 at 19:46
  • 1
    as of XCode 9 you should set the FRAMEWORK_SEARCH_PATHS build setting to $(DEVELOPER_FRAMEWORKS_DIR) and $(inherited) – berbie Apr 29 '18 at 10:28
44

I was moving files in a project around. All you have to do is select your test files xxxTests.m etc. and in file inspector select target as test and not as a regular target.

Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84
  • 1
    Exactly what happened to me. I did not notice that when I moved it the target for the file "changed." I should have thought of it based on how it was moved (delete in Xcode and re-add). Thanks. – chadbag Apr 26 '16 at 14:18
  • 2
    simple answer which worked for me. Had issues when I was renaming my files, and this was the resolution in my case. – Simon Jan 13 '18 at 11:19
18

I've noticed that XCTest is available to a test target only (in Xcode 6). If you are using using XCTest for any other target (for whatever reason), you will see the XCTest.h not found error.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Mustafa
  • 5,307
  • 1
  • 20
  • 19
12

This error comes up when you have added a file where XCTest is being used outside of a test target. To fix this in AppCode, you can right click on any suspected file and select 'Manage Targets' then make sure only the test target is checked.

Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
11

@squarefrog has the right answer but you'll have to keep doing that manually each time you update your pods :(

If you add this to your podfile it will automatically add the extra path for you. E.g. if you wanted to add $(PLATFORM_DIR)/Developer/Library/Frameworks to FRAMEWORK_SEARCH_PATHS for Specta:

post_install do |installer|
    target = installer.project.targets.find { |t| t.to_s == "Pods-Tests-Specta" }
    if (target)
        target.build_configurations.each do |config|
            s = config.build_settings['FRAMEWORK_SEARCH_PATHS']
            s = [ '$(inherited)' ] if s == nil;
            s.push('$(PLATFORM_DIR)/Developer/Library/Frameworks')
            config.build_settings['FRAMEWORK_SEARCH_PATHS'] = s
        end
    else
        puts "WARNING: Pods-Tests-Specta target not found"
    end
end
Community
  • 1
  • 1
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • Note that you may need to change `"Pods-Tests-Specta"` to match your project settings, or simply use the loop that @squarefrog suggested to iterate over every target. – pix0r Sep 25 '14 at 21:59
  • 1
    I misspoke- @squarefrog didn't suggest a loop. What I mean is replace lines 2-3 in @deanWombourne's solution with: `installer.project.targets.each do |target` to loop over all targets and apply the change. – pix0r Sep 25 '14 at 22:16
2

I have faced same issue after some time I have imported XCTest framework from build phases and solved the issue.

Build phases-> XCTest.Framework>clean and run. I hope it will be helpful to some one..

for your reference...Import XCTest in to existing project

Community
  • 1
  • 1
Narasimha Nallamsetty
  • 1,215
  • 14
  • 16
  • This worked for me, but I also had to go to the Target Settings for the new tests and on the General tab check the box for testing the app's APIs. – Fran K. Sep 23 '14 at 01:51
1

As of the time of writing, the latest version of Cocoapods (0.33.1) doesn't have a fix for the problem.

But the cutting edge version does.

Follow this guide to set the latest version of Cocoapods up from source. I call mine pod-dev (covered in the guide) to distinguish it from the gem-installed version of pods.

The benefit of this approach is that you don't need extra scripting in your Podfile. You just have to remember to do a pod-dev install instead of the usual pod install.

fatuhoku
  • 4,815
  • 3
  • 30
  • 70
1

The best way to have an XCTest is to add it from the Test Navigator (5th icon in the left pane).

Acting so, the new xx.m test file doesn't target (in the right pane) an app (in the left pane > Target, Wrapper Extension : app), but a bundle (Wrapper Extension: xctest)

  • the XCTest.framework remains in Red,
  • because of some modifications you can have the error 'XCTest/XCTest.h' file not found, it's because your file had got to target an app.
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
tontonCD
  • 320
  • 2
  • 6
0

This is the easiest work-around I've found. No project changes are necessary. And this change will persist until your next Xcode upgrade:

cd /Applications/Xcode6-Beta7.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Developer/Library/Frameworks
ln -s ../../../../../Library/Frameworks/XCTest.framework
Steven Kramer
  • 8,473
  • 2
  • 37
  • 43
  • The downside with this is if you share your project with others as you have to get them to perform this action, each time there is an Xcode update. This is an issue with Cocoapods implementation, therefore it makes sense to solve it using Cocoapods! – squarefrog Sep 08 '14 at 11:40
  • Agreed. And this is not perfect. But it's real easy and scales well with the number of pods you have (O(1)). The number of Xcode 6 versions had better be limited too :-) – Steven Kramer Sep 08 '14 at 19:54
  • Adding the path to the framework search paths doesn't seem to cut it: tests will build, but not run. Apparently due to the test bundle not being able to resolve the XCTest framework. – Steven Kramer Sep 25 '14 at 08:17
0

On a project without Cocoapods (so not directly answering the OP's question but perhaps helpful for me or others in the future), we had the same issue. It was solved by replacing our previous OCTest items with XCTest. For example, a file MyApp.xcodeproj/project.pbxproj has this diff (shortened) ; - path = MyAppUnitTests.octest; sourceTree = BUILT_PRODUCTS_DIR; }; + path = MyAppUnitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };

AnneTheAgile
  • 9,932
  • 6
  • 52
  • 48
0

Re-visiting my very old question, as of Xcode 11 you can set

ENABLE_TESTING_SEARCH_PATHS = YES

in your target

Erik Kerber
  • 5,646
  • 7
  • 38
  • 56