NOTE: "Use GHUnit" is not an acceptable answer to this question. I know most think GHUnit is better than the Xcode4 OCUnit, but that's not what I'm asking about. I'll evaluate that separately.
I have an Xcode project that I created in Xcode4 from scratch, with the "Include unit tests" checkbox checked during creation. I have also included some libraries I developed in a previous project. They were added to the project via the "Add Files to x..." dialog and only added to the application target (not the testing target). They work fine when running the application, so I think they're set up correctly. I also have a number of various classes written for this project.
My testing files are set up in the standard way, named [AppName]Tests.h and .m.
Code for header:
#import < SenTestingKit/SenTestingKit.h >
@interface [AppName]Tests : SenTestCase {
@private
}
@end
Code for implementation:
#import "[AppName]Tests.h"
@implementation [AppName]Tests
- (void)setUp
{
[super setUp];
// Set-up code here.
}
- (void)tearDown
{
// Tear-down code here.
[super tearDown];
}
// Test methods go here
@end
Which is just the basic skeleton. It works fine in my other project, and in this project as long as I don't import any other files. When I do import another file from this project and use it, I see the following error in the Xcode output log:
The test bundle at /Users/[Me]/Library/Developer/Xcode/DerivedData/[AppName]-dwuuuwcpmdqxqmgxomoniplwhlpb/Build/Products/Debug-iphonesimulator/[AppName]Tests.octest could not be loaded because a link error occurred. It is likely that dyld cannot locate a framework framework or library that the the test bundle was linked against, possibly because the framework or library had an incorrect install path at link time.
I've already verified that:
- All the frameworks I use have been added to "Link Binary with Libraries" for both the app and test targets.
- The test target has been configured to build correctly and that all my test methods show up in Edit Scheme...->Test->Tests
- Every issue but this one has been resolved and there are no compiler errors.
- All the settings discussed here are set up correctly and identical to my other project that tests correctly.
Any thoughts on what might be causing this?
Came across this error running Xcode 4.5.2 - it's Nov 2012 - none of the above worked. It seems that setting the bundle loader and test host is supposed to fill in all the dependencies from your project for you - or run the tests in your app's environment or something, but unfortunately wasn't working for me. What it did do was prevent the specific Xcode warnings about which files/libraries were missing.
What worked for me was adding a new target: cocoa touch unit test, (making sure the bundle loader and test host build settings were empty), watching the build errors and manually adding in the missing dependencies - one by one all the source files from my project that were needed and then the Frameworks. Not very elegant but I was just happy to get it working. Not sure why I haven't tried this GHUnit library yet.
Make sure that the test target has the app target configured as a dependency (Build Phases -> Target Dependencies).
I'm aware that this question is quite old, but I just struggled with identical issue for some time and finally managed to solve it, so let me share what I found.
I've been porting an app from iOS to Mac, the project itself was created for iOS so both the project and main target had iOS in supported platforms. Now, when I started porting I created new target for Mac, and changed supported platforms to OSX only for that target. After that I created another target for unit tests, but forgot to change the supported platforms from iOS to Mac. I think you already should know what the problem was, basically the unit tests target is by default linked with Cocoa framework so since the supported platforms for this target was only iOS the cocoa framework was never built and wasn't properly linked. Changing supported platforms to OSX for the test target fixed the problem.
I'm aware that this may not be very helpful for iOS test targets, but at least go to your test target Link Binary With Libraries section and see if there are any red libraries. This gave me the idea, maybe it will also help some of you.
I've run into this after several merges lately where a coworker and I have both added files to the project, and not all of the implementation files are a member of the unit testing target.
The solution:
.m
files (use "Filter in Navigator" (cmd-opt-j) to search for.m
)Note: if the test target checkbox shows
-
instead of+
or being unchecked, it means that some, but not all, of the selected files are a member of the target.I've tried doing more intelligent things at merge time with the
.xcodeproj
'sproject.pbxproj
file, but I've given up out of frustration with its inscrutability every time and fallen back on this method.(Another note: I only select a few files at a time for two reasons:
I had to set the "Test Host" property on the unit test target to
$(BUNDLE_LOADER)
. That solved my problem!I did the following:
http://twobitlabs.com/2011/06/adding-ocunit-to-an-existing-ios-project-with-xcode-4/
Take a deeper look at Test_Hosts = $(BUNDLE_LOADER)
Setting the Test_Hosts fixed exactly the same issue!