XCTest/XCTest.h not found on old projects built in Xcode 6
Asked Answered
A

11

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?

Apocarp answered 18/6, 2014 at 0:48 Comment(0)
A
0

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

ENABLE_TESTING_SEARCH_PATHS = YES

in your target

Apocarp answered 18/3, 2022 at 14:59 Comment(0)
S
144

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.

Scalable answered 9/7, 2014 at 10:51 Comment(15)
Thank you squarefrog. The relevant issue is hereStrangeness
When is the next release scheduled?Strangeness
When it's ready :) If you are feeling brave you can always build it from source. This process is a little easier with Rainforest.Scalable
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.Strangeness
See my answer, which doesn't require modifying Pod files or other recurring changes and works with older CocoaPod versions.Greenlaw
This does not seem to work with KIM in xcode 6 GM..?Impersonal
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:Greenlaw
@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.Scalable
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.Greenlaw
Thanks! I'm fixing this issue with HTPressableButtons - @Scalable remember us? :PMatri
@ThanakronTandavas of course I do - hope you're both well.Scalable
Still relevant for XCode 6.4. Thanks for saving me hours of frustrationJaundiced
You are awesome!! Had been banging my head over this!! Thanks!!Dewees
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.Hogweed
as of XCode 9 you should set the FRAMEWORK_SEARCH_PATHS build setting to $(DEVELOPER_FRAMEWORKS_DIR) and $(inherited)Saeger
R
45

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.

Resnatron answered 18/5, 2015 at 13:15 Comment(2)
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.Aluminiferous
simple answer which worked for me. Had issues when I was renaming my files, and this was the resolution in my case.Christiansand
M
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.

Mummy answered 23/6, 2014 at 19:11 Comment(4)
That's it. Cocoapods creates static library projects/targets. Good find!Apocarp
You will most likely need to move all the XCTest related code to a test target.Mummy
Which is difficult if you are using CocoaPods to include Kiwi or Specta in your project.Scalable
So... what's the fix?Strangeness
G
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.

Glennglenna answered 18/2, 2015 at 9:54 Comment(2)
I've run into this problem more than once.Old
This happened to me after moving files around in a project and re-adding them under the wrong target.Imitable
E
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
Ebony answered 1/8, 2014 at 11:3 Comment(2)
Note that you may need to change "Pods-Tests-Specta" to match your project settings, or simply use the loop that @Scalable suggested to iterate over every target.Zootomy
I misspoke- @Scalable 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.Zootomy
A
3

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

Achondrite answered 6/8, 2014 at 20:0 Comment(1)
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.Dekker
S
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.

Strangeness answered 20/8, 2014 at 19:0 Comment(0)
O
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.
Organism answered 31/10, 2017 at 15:14 Comment(0)
G
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
Greenlaw answered 5/9, 2014 at 13:25 Comment(3)
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!Scalable
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 :-)Greenlaw
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.Greenlaw
D
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; };

Decide answered 30/3, 2015 at 20:9 Comment(0)
A
0

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

ENABLE_TESTING_SEARCH_PATHS = YES

in your target

Apocarp answered 18/3, 2022 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.