Take the 2-minute tour ×

I'm using XCode 4.

Can someone please explain to me why when I have written a unit test (under the test target) that it can't find my local file resource? I'm trying to load a plist file from the NSBundle in a unit test but it doesn't work in the unit test target. I've tried throwing it under the Classes area of the Test target.

Here's my code:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"urlMappings.plist"];
self.urlMappings  = [NSDictionary dictionaryWithContentsOfFile:finalPath];

self.urlMappings is empty for some reason even though urlMappings.plist definately exists in my project. Here is the source to urlMappings.plist to prove it is in fact a dictionary.

<plist version="1.0">

Any ideas? Thankyou

share|improve this question
Building off of MikeSimmons's solution, I came up with the following Xcode: TEST vs DEBUG preprocessor macros. –  MattDiPasquale Jul 20 '11 at 14:40
add comment

2 Answers

up vote 3 down vote accepted

Under the test target, you shouldn't use [NSBundle mainBundle], because it is the main bundle, not the test bundle. you should use [NSBundle bundleForClass:[self class]] replace.

share|improve this answer
Makes perfect sense. I've awarded you the solution. Thankyou! –  Mike S Aug 26 '13 at 6:21
add comment

The solution (which doesn't make sense) is it wasn't in the bundle resources of the main target. It has to be in the bundle resources of BOTH targets even though I'm only using it in test.

Anyone know why this is the case?

share|improve this answer
add comment

Your Answer


