XCTestTarget - After upgrading to Xcode 8.2 [[NSFileManager defaultManager] currentDirectoryPath] returns /private/tmp

107 Views Asked by At

After upgrading to macOS sierra 10.12.2 and Xcode 8.2 from OS X 10.10.2 and Xcode 6.2 [[NSFileManager defaultManager] currentDirectoryPath] returns /private/tmp instead of project directory. In Xcode 6.2 it returns project directory. Are there any settings which need to be set manually?

Problem is only with test target, I created a sample project and output of

NSLog(@"currentDirectoryPath is %@",[[NSFileManager defaultManager] currentDirectoryPath]); is working directory for actual target but for test target it is always /private/tmp.

1

There are 1 best solutions below

0
On

Working with currentDirectoryPath() is not your best option here (with Xcode8). Inside your test class you should use:

[[NSBundle bundleForClass:[self class]] bundlePath];

or

[[NSBundle bundleForClass:[self class]] bundleURL];

This will return a NSString / NSURL pointing to the install location of the bundle. For simulator builds the paths will be as follows:

  • Logic Tests Targets: $BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME
  • Application Test Targets: $BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/PlugIns/$TEST_TARGET_PRODUCT_NAME.xctest

Knowing this you can load resources from your test bundle e.g. via

[[NSBundle bundleForClass:[self class]] pathForResource:[filename stringByDeletingPathExtension] ofType:[filename pathExtension]]