Skip to content

Instantly share code, notes, and snippets.

@TonnyXu
Created January 29, 2012 08:49
Show Gist options
  • Save TonnyXu/1697924 to your computer and use it in GitHub Desktop.
Save TonnyXu/1697924 to your computer and use it in GitHub Desktop.
Use bundleForClass to get the unit test bundle's path
/*
This tips is extremely useful when you test your CoreData app in Unit Test.
*/
// iOS app is an executable bundle, means the `main` method is
// included inside app bundle, so `mainBundle` returns the app bundle
//
// See doc at https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003624
NSBundle *appBundle = [NSBundle mainBundle];
NSString *appBundlePath = [appBundle bundlePath];
// iOS unit test bundle is not an executable bundle, it is invoked by
// OCUnit, which is an executable bundle, so you can only get the bundle
// path by `bundleForClass`
//
// see doc at:
NSBundle *testBundle = [NSBundle bundleForClass:[YourTestClass class]];
NSString *testBundlePath = [testBundle bundlePath];
@TonnyXu
Copy link
Author

TonnyXu commented Jan 29, 2012

The link inside gist is not clickable, click it here: NSBundle Class Reference

@cutmail
Copy link

cutmail commented Feb 3, 2012

@7gano
Copy link

7gano commented Feb 3, 2012

NSBundle* bundle = [NSBundle bundleForClass:[OCTestTests class]];
NSString* testBundlePath = [bundle bundlePath];
NSString* path = [bundle pathForResource:@"Info" ofType:@"plist"];
NSLog(@"path = %@", path);    
// path = /Users/nagano/Library/Developer/Xcode/DerivedData/OCTest-edbutzmpyheqvyfvkyfooctdgljv/Build/Products/Debug-iphonesimulator/OCTestTests.octest/Info.plist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment