Even when it is defined in the "...Tests" target Info.plist, the identifier remains nil. I need it to have a valid value for a third party lib.
This is because unit tests don't load your application bundle or even the unit test bundle as the main bundle.
For more info on this see
Now we can hack around this by including a category on NSBundle in your unit test project. The Objective-C category hack also works for swift unit tests because Bundle is bridged to NSBundle, just make sure you have a bridged header for your unit test project.
You can then have it search for the correct main bundle, but if you just want a quick and dirty way to have a valid bundle identifier then use the following
@interface NSBundle (BundleIdentifier)
-(NSString *)bundleIdentifier;
@end
@implementation NSBundle (BundleIdentifier)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
-(NSString *)bundleIdentifier
{
return @"com.yourcompany.yourapp.unitTests";
}
#pragma clang diagnostic pop
@end
Now all that is left is to include the header file in your unit test so that the category loads up.